import 'dart:convert';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/providers/api/service_requests_provider.dart';
import 'package:test_sa/controllers/providers/api/user_provider.dart';
import 'package:test_sa/controllers/providers/settings/setting_provider.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/text_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/service_request/service_request.dart';
import 'package:test_sa/new_views/common_widgets/app_filled_button.dart';
import 'package:test_sa/providers/service_request_providers/requested_through_provider.dart';
import 'package:test_sa/views/widgets/equipment/pick_asset.dart';
import 'package:test_sa/views/widgets/images/multi_image_picker.dart';
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';
import 'package:test_sa/views/widgets/sound/record_sound.dart';

import '../../../../models/lookup.dart';
import '../../../../new_views/app_style/app_color.dart';
import '../../../../new_views/common_widgets/app_text_form_field.dart';
import '../../../../new_views/common_widgets/default_app_bar.dart';
import '../../../../new_views/common_widgets/single_item_drop_down_menu.dart';
import '../../../../providers/service_request_providers/equipment_status_provider.dart';
import '../../../../providers/service_request_providers/priority_provider.dart';
import '../../../../providers/service_request_providers/type_of_request_provider.dart';

class CreateServiceRequestPage extends StatefulWidget {
  static const String id = "/create-request";
  final ServiceRequest serviceRequest;

  const CreateServiceRequestPage({this.serviceRequest, Key key}) : super(key: key);

  @override
  CreateServiceRequestPageState createState() => CreateServiceRequestPageState();
}

class CreateServiceRequestPageState extends State<CreateServiceRequestPage> {
  TextEditingController _commentController;

  double _height;
  UserProvider _userProvider;
  SettingProvider _settingProvider;
  ServiceRequestsProvider _serviceRequestsProvider;
  ServiceRequest _serviceRequest;
  final List<File> _deviceImages = [];
  bool _isLoading = false;
  bool _showDatePicker = false;
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  DateTime _dateTime;

  bool isFirstActionSubmitted = false;

  @override
  void initState() {
    _commentController = TextEditingController();
    if (widget.serviceRequest != null) {
      _serviceRequest = widget.serviceRequest;
      _deviceImages.addAll(_serviceRequest.devicePhotos.map((e) => File(e)).toList());
      _showDatePicker = _serviceRequest.firstAction != null && _serviceRequest.firstAction.name == "Need a visit";
      if (_showDatePicker && _serviceRequest.visitDate != null) {
        _dateTime = DateTime.tryParse(_serviceRequest.visitDate);
      }
    } else {
      _serviceRequest = ServiceRequest();
    }
    isFirstActionSubmitted = _serviceRequest?.firstAction != null;
    super.initState();
  }

  //
  // getServiceRequestById(String id) async {
  //   try {
  //     ServiceRequest request = await _serviceRequestsProvider.getServiceRequestObjectById(requestId: id) ?? "";
  //     _serviceRequest = request;
  //     _device = _serviceRequest.device;
  //     _deviceImages.addAll(_serviceRequest.devicePhotos.map((e) {
  //       return File(e);
  //     }).toList());
  //     _showDatePicker = _serviceRequest.firstAction != null && _serviceRequest.firstAction.name == "Need a visit";
  //     if (_showDatePicker && _serviceRequest.visitDate != null) {
  //       _dateTime = DateTime.tryParse(_serviceRequest.visitDate);
  //     }
  //     _isLoading = false;
  //   } catch (ex) {
  //     _isLoading = false;
  //   }
  //   setState(() {});
  // }

  @override
  void dispose() {
    _commentController.dispose();
    super.dispose();
  }

  bool _isLocalUrl(String url) {
    if (url?.isEmpty != false) return false;
    return url.startsWith("/") || url.startsWith("file://") || url.substring(1).startsWith(':\\');
  }

  @override
  Widget build(BuildContext context) {
    _height = MediaQuery.of(context).size.height;
    _userProvider = Provider.of<UserProvider>(context);
    _settingProvider = Provider.of<SettingProvider>(context);

    _serviceRequestsProvider = Provider.of<ServiceRequestsProvider>(context);

    return Scaffold(
      key: _scaffoldKey,
      appBar: DefaultAppBar(title: context.translation.newServiceRequest),
      body: SafeArea(
        child: LoadingManager(
          isLoading: _isLoading,
          isFailedLoading: false,
          stateCode: 200,
          onRefresh: () async {},
          child: Form(
            key: _formKey,
            child: Column(
              children: [
                SingleChildScrollView(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      PickAsset(
                        device: _serviceRequest.device,
                        onPickAsset: (asset) {
                          _serviceRequest.device = asset;
                          setState(() {});
                        },
                      ),
                      8.height,
                      SingleItemDropDownMenu<Lookup, PriorityProvider>(
                        context: context,
                        title: context.translation.priority,
                        initialValue: _serviceRequest?.priority,
                        onSelect: (value) {
                          _serviceRequest.priority = value;
                        },
                      ),
                      8.height,
                      SingleItemDropDownMenu<Lookup, EquipmentStatusProvider>(
                        context: context,
                        title: context.translation.equipmentStatus,
                        initialValue: _serviceRequest?.defectType,
                        onSelect: (value) {
                          _serviceRequest.defectType = value;
                        },
                      ),
                      8.height,
                      Consumer<RequestedThroughProvider>(builder: (context, snapshot, _) {
                        return SingleItemDropDownMenu<Lookup, RequestedThroughProvider>(
                          context: context,
                          enabled: false,
                          title: context.translation.source,
                          initialValue: snapshot.items?.firstWhere((element) => element.value == 3, orElse: () => null),
                        );
                      }),
                      8.height,
                      Consumer<TypeOfRequestProvider>(builder: (context, snapshot, _){
                        return SingleItemDropDownMenu<Lookup, TypeOfRequestProvider>(
                        context: context,
                        title: context.translation.requestType,
                        enabled: false,
                          initialValue: snapshot.items?.firstWhere((element) => element.value == 1, orElse: () => null),
                        // onSelect: (value) {
                        //   _serviceRequest.type = value;
                        // },
                      );}),
                      8.height,
                      MultiFilesPicker(label: context.translation.attachImage, files: _deviceImages),
                      ((_serviceRequest.devicePhotos?.isNotEmpty ?? false) ? 16 : 8).height,
                      Align(
                        alignment: AlignmentDirectional.centerStart,
                        child: context.translation.callComments.heading5(context),
                      ),
                      8.height,
                      AppTextFormField(
                        controller: _commentController,
                        labelText: context.translation.problemDesc,
                        suffixIcon: "warning".toSvgAsset(color: context.isDark ? AppColor.neutral10 : AppColor.neutral20, width: 24).paddingOnly(end: 16),
                        initialValue: _serviceRequest.callComments,
                        onSaved: (text) {
                          _serviceRequest.callComments = text;
                        },
                      ),
                      8.height,
                      RecordSound(
                        onRecord: (audio) {
                          _serviceRequest.audio = audio;
                        },
                        enabled: widget.serviceRequest == null ? true : false,
                      ),
                      16.height,
                    ],
                  ),
                ).expanded,
                AppFilledButton(
                  onPressed: _submit,
                  label: context.translation.submitRequest,
                ),
              ],
            ),
          ).paddingOnly(start: 16, end: 16, bottom: 24, top: 16),
        ),
      ),
    );
  }

  Future<void> _submit() async {
    _serviceRequest?.requestedThrough = Provider.of<RequestedThroughProvider>(context, listen: false).items?.firstWhere((element) => element.value == 3, orElse: () => null);
    _serviceRequest?.type=Provider.of<TypeOfRequestProvider>(context, listen: false).items?.firstWhere((element) => element.value == 1, orElse: () => null);
    if (_formKey.currentState.validate() && await _serviceRequest.validateNewRequest(context)) {
      _formKey.currentState.save();
      _serviceRequest.devicePhotos = _deviceImages.map((e) => _isLocalUrl(e.path) ? "${e.path.split("/").last}|${base64Encode(e.readAsBytesSync())}" : e.path).toList();
      if (_serviceRequest.audio != null) {
        if (_isLocalUrl(_serviceRequest.audio)) {
          final File file = File(_serviceRequest.audio);
          _serviceRequest.audio = "${file.path.split("/").last}|${base64Encode(file.readAsBytesSync())}";
        }
      }
      await _serviceRequestsProvider.createRequest(
        context: context,
        user: _userProvider.user,
        host: _settingProvider.host,
        serviceRequest: _serviceRequest,
      );
    }
  }
}
