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

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/controllers/api_routes/http_status_manger.dart';
import 'package:test_sa/controllers/providers/api/all_requests_provider.dart';
import 'package:test_sa/controllers/providers/api/gas_refill_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/controllers/validator/validator.dart';
import 'package:test_sa/extensions/context_extension.dart';
import 'package:test_sa/extensions/int_extensions.dart';
import 'package:test_sa/extensions/string_extensions.dart';
import 'package:test_sa/extensions/widget_extensions.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/models/timer_model.dart';
import 'package:test_sa/new_views/app_style/app_color.dart';
import 'package:test_sa/new_views/common_widgets/app_filled_button.dart';
import 'package:test_sa/new_views/common_widgets/app_lazy_loading.dart';
import 'package:test_sa/providers/loading_list_notifier.dart';
import 'package:test_sa/service_request_latest/utilities/service_request_utils.dart';
import 'package:test_sa/service_request_latest/views/components/action_button/footer_action_button.dart';
import 'package:test_sa/views/widgets/images/multi_image_picker.dart';
import 'package:test_sa/views/widgets/loaders/loading_manager.dart';

import '../../../../controllers/providers/api/hospitals_provider.dart';
import '../../../../extensions/text_extensions.dart';
import '../../../../models/new_models/gas_refill_model.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/gas_request_providers/gas_status_provider.dart';
import '../../../widgets/e_signature/e_signature.dart';
import '../../../widgets/timer/app_timer.dart';

class GasRefillForm extends StatefulWidget {
  static const String id = "/gas-refill-form";
  final GasRefillModel? gasRefillModel;

  const GasRefillForm({this.gasRefillModel, Key? key}) : super(key: key);

  @override
  State<GasRefillForm> createState() => _GasRefillFormState();
}

class _GasRefillFormState extends State<GasRefillForm> {
  final bool _isLoading = false;
  bool _validate = false;
  Uint8List? _engineerSignature;
  Uint8List? _nurseSignature;
  double totalWorkingHours = 0.0;

  late UserProvider _userProvider;
  late SettingProvider _settingProvider;
  GasRefillProvider? _gasRefillProvider;
  GasRefillDetails _currentDetails = GasRefillDetails();
  final TextEditingController _deliveredQuantityController = TextEditingController();
  final TextEditingController _commentController = TextEditingController();
  final TextEditingController _workingHoursController = TextEditingController();

  final GasRefillModel _formModel = GasRefillModel(gasRefillDetails: []);
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final GlobalKey _DetailsKey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  bool _firstTime = true;

  Lookup? _deliveredQuantity;

  static List<Lookup> deliveredQuantity = [
    Lookup(name: "1", id: 1, value: 1),
    Lookup(name: "2", id: 2, value: 2),
    Lookup(name: "3", id: 3, value: 3),
    Lookup(name: "4", id: 4, value: 4),
    Lookup(name: "5", id: 5, value: 5)
  ];

  @override
  void initState() {
    super.initState();
    _gasRefillProvider ??= Provider.of<GasRefillProvider>(context, listen: false);

    if (widget.gasRefillModel != null) {
      _formModel.fromGasRefillModel(widget.gasRefillModel!);

      // totalWorkingHours =
      //     _formModel?.fold(0.0, (sum, item) => (sum ?? 0) + DateTime.parse(item.endDateTime!).difference(DateTime.parse(item.startDateTime!)).inSeconds) ?? 0;

      _commentController.text = _formModel.techComment ?? "";
      try {
        _deliveredQuantity = deliveredQuantity.singleWhere((element) => element.value == _formModel.gasRefillDetails![0].deliverdQty);
        _currentDetails.deliverdQty = _deliveredQuantity!.value;
      } catch (ex) {}
    }
    if (_formModel.gasRefillAttachments != null && _formModel.gasRefillAttachments!.isNotEmpty) {
      _gasRefillProvider?.gasRefillAttachments = [];
      _gasRefillProvider?.gasRefillAttachments.addAll(_formModel.gasRefillAttachments!.map((e) => File(e.attachmentName!)).toList());
    }
  }

  @override
  void setState(VoidCallback fn) {
    if (mounted) super.setState(() {});
  }

  _onSubmit(BuildContext context,int status) async {

    if (_formModel.timer?.startAt == null) {
      await Fluttertoast.showToast(msg: "Working Hours Required");
      return false;
    }
    if (_formModel.timer?.endAt == null) {
      await Fluttertoast.showToast(msg: "Please Stop The Timer");
      return false;
    }
    if(_currentDetails.deliverdQty==null){
      await Fluttertoast.showToast(msg: "Delivered Quantity  is Required");
      return false;
    }
    _formModel.gasRefillDetails=[];
    _formModel.gasRefillDetails?.add(_currentDetails);

    showDialog(context: context, barrierDismissible: false, builder: (context) => const AppLazyLoading());
    _formModel.gasRefillTimers = _formModel.gasRefillTimers ?? [];

    _formModel.timerModelList?.forEach((timer) {
      int durationInSecond = timer.endAt!.difference(timer.startAt!).inSeconds;
      _formModel.gasRefillTimers?.add(
        GasRefillTimer(
          id: 0,
          startDate: timer.startAt!.toIso8601String(), // Handle potential null
          endDate: timer.endAt?.toIso8601String(), // Handle potential null
          workingHours: ((durationInSecond) / 60 / 60),
        ),
      );
    });
    _formModel.gasRefillAttachments = [];
    for (var item in _gasRefillProvider!.gasRefillAttachments) {
      _formModel.gasRefillAttachments
          ?.add(GasRefillAttachments(id: 0,gasRefillId: _formModel.id??0, attachmentName: ServiceRequestUtils.isLocalUrl(item.path) ? "${item.path.split("/").last}|${base64Encode(item.readAsBytesSync())}" : item.path));
    }
    await _gasRefillProvider
        ?.updateGasRefill(
        status: status,
        model: _formModel
    )
        .whenComplete(() {
      if (status == 1) {
        AllRequestsProvider allRequestsProvider = Provider.of<AllRequestsProvider>(context,listen: false);
        // when click complete then this request remove from the list and status changes to closed..
        _gasRefillProvider?.reset();
        allRequestsProvider.getAllRequests(context, typeTransaction: 2);
      }
      Navigator.pop(context);
      Navigator.pop(context);
    });

  }


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

  void updateTimer({TimerModel? timer}) {
    _formModel.timer = timer;
    if (timer?.startAt != null && timer?.endAt != null) {
      _formModel.timerModelList = _formModel.timerModelList ?? [];
      _formModel.timerModelList!.add(timer!);
    }
    // notifyListeners();
  }

  @override
  Widget build(BuildContext context) {
    _userProvider = Provider.of<UserProvider>(context);
    _settingProvider = Provider.of<SettingProvider>(context);
    if (_firstTime) {
      String? clientName;
      if (widget.gasRefillModel != null) {
        //_formModel.status = widget.gasRefillModel?.status ?? Lookup(value: 0);
        _gasRefillProvider!.expectedDateTime = DateTime.tryParse(_formModel.expectedDate ?? "");
        _formModel.timer = TimerModel(startAt: DateTime.tryParse(widget.gasRefillModel?.startDate ?? ""), endAt: DateTime.tryParse(widget.gasRefillModel?.endDate ?? ""));
        clientName = _formModel.site?.custName;
      } else {
        _formModel.timer = null;
        clientName = _userProvider.user?.clientName;
      }

      // HospitalsProvider().getHospitalsListByVal(searchVal: clientName ?? '').then((value) {
      //   _gasRefillProvider!.hospital = value.firstWhere((element) => element.name == clientName, orElse: null);
      //   _gasRefillProvider!.building = _gasRefillProvider!.hospital?.buildings?.firstWhere((element) => element.name == widget.gasRefillModel?.building?.name, orElse: null);
      //   _gasRefillProvider!.floor = _gasRefillProvider!.building?.floors?.firstWhere((element) => element.name == widget.gasRefillModel?.floor?.name, orElse: null);
      //   _gasRefillProvider!.department = _gasRefillProvider!.floor?.departments?.firstWhere((element) => element.name == widget.gasRefillModel?.department?.departmentName, orElse: null);
      //   _firstTime = false;
      //   setState(() {});
      // });
    }
    return Scaffold(
      appBar: DefaultAppBar(title: context.translation.gasRefill),
      key: _scaffoldKey,
      body: Form(
        key: _formKey,
        child: SafeArea(
          child: LoadingManager(
              isLoading: _isLoading,
              isFailedLoading: false,
              stateCode: 200,
              onRefresh: () async {},
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.stretch,
                children: [
                  SingleChildScrollView(
                    padding: const EdgeInsets.all(16),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.stretch,
                      children: [
                        // Column(
                        //   crossAxisAlignment: CrossAxisAlignment.start,
                        //   children: [
                        //     context.translation.gasRefill.heading5(context),
                        //     8.height,
                        //     '${context.translation.gasRequest}: ${widget.gasRefillModel!.gasRefillDetails![0].gasType!.name}'.bodyText(context),
                        //     '${context.translation.cylinderType}: ${widget.gasRefillModel!.gasRefillDetails![0].cylinderType!.name}'.bodyText(context),
                        //     '${context.translation.cylinderSize}: ${widget.gasRefillModel!.gasRefillDetails![0].cylinderSize!.name}'.bodyText(context),
                        //     '${context.translation.quantity}: ${widget.gasRefillModel!.gasRefillDetails![0].requestedQty ?? 0}'.bodyText(context),
                        //     '${context.translation.site}: ${widget.gasRefillModel!.site?.name}'.bodyText(context),
                        //   ],
                        // ).toShadowContainer(context),
                        8.height,
                        SingleItemDropDownMenu<Lookup, NullableLoadingProvider>(
                          context: context,
                          title: context.translation.quantity,
                          backgroundColor: AppColor.neutral100,
                          showShadow: false,
                          initialValue: _deliveredQuantity,
                          staticData: deliveredQuantity,
                          onSelect: (value) {
                            _deliveredQuantity = value;
                            _currentDetails.deliverdQty = value!.value;
                          },
                        ),
                        8.height,
                        // AppTextFormField(
                        //   initialValue: _formModel.gasRefillDetails?[0].deliverdQty?.toString() ?? "",
                        //   labelText: context.translation.deliveredQuantity,
                        //   onSaved: (value) {
                        //     _currentDetails.deliverdQty = double.tryParse(value);
                        //   },
                        //   textInputType: TextInputType.number,
                        //   controller: _deliveredQuantityController,
                        //   validator: (value) => value == null || value.isEmpty
                        //       ? context.translation.requiredField
                        //       : Validator.isNumeric(value)
                        //       ? null
                        //       : context.translation.onlyNumbers,
                        // ),
                        // 8.height,
                        _timerWidget(context, _formModel.workingHours ?? 0),
                        8.height,
                        // SingleItemDropDownMenu<Lookup, GasStatusProvider>(
                        //   context: context,
                        //   title: context.translation.requestStatus,
                        //   initialValue: _formModel.status,
                        //   onSelect: (value) {
                        //     if (value?.value == 0) {
                        //       "Status cannot be change to ${value?.name}.".addTranslation.showToast;
                        //       setState(() {});
                        //       return;
                        //     }
                        //     if (value != null) {
                        //       _formModel.status = value;
                        //     }
                        //   },
                        // ),
                        // 8.height,

                        // 8.height,


                        /// TBD
                        AppTextFormField(
                          labelText: context.translation.technicalComment,
                          textInputType: TextInputType.multiline,
                          alignLabelWithHint: true,
                          backgroundColor: AppColor.neutral100,
                          showShadow: false,
                          controller: _commentController,
                          onChange: (value){
                            _formModel.techComment = value;
                          },
                          onSaved: (value) {},
                        ),
                        16.height,
                        MultiFilesPicker(
                          label: context.translation.attachFiles,
                          files: _gasRefillProvider!.gasRefillAttachments,
                          buttonColor: AppColor.black10,
                          onlyImages: false,
                          buttonIcon: 'image-plus'.toSvgAsset(color: AppColor.neutral120),
                        ),
                        8.height,
                        // ESignature(
                        //   title: "Engineer Signature",
                        //   oldSignature: widget.gasRefillModel?.engSignature,
                        //   newSignature: _engineerSignature,
                        //   onSaved: (signature) {
                        //     _engineerSignature = signature;
                        //     if (signature == null || signature.isEmpty) return;
                        //     _formModel.engSignature = "${DateTime.now().toIso8601String()}.png|${base64Encode(signature)}";
                        //     //base64Encode(signature);
                        //   },
                        // ),
                        // 8.height,
                        // ESignature(
                        //   title: "Nurse Signature",
                        //   oldSignature: widget.gasRefillModel?.nurseSignature,
                        //   newSignature: _nurseSignature,
                        //   onSaved: (signature) {
                        //     _nurseSignature = signature;
                        //     if (signature == null || signature.isEmpty) return;
                        //     _formModel.nurseSignature = "${DateTime.now().toIso8601String()}.png|${base64Encode(signature)}";
                        //     //base64Encode(signature);
                        //   },
                        // ),
                      ],
                    ).toShadowContainer(context),
                  ).expanded,
                  FooterActionButton.footerContainer(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceAround,
                      children: [
                        AppFilledButton(
                          label: context.translation.save,
                          buttonColor: AppColor.white60,
                          textColor: AppColor.black10,
                          onPressed: () => _onSubmit(context, 0),
                        ).expanded,
                        12.width,
                        AppFilledButton(
                          label: context.translation.complete,
                          buttonColor: AppColor.primary10,
                          onPressed: () => _onSubmit(context, 1),
                        ).expanded,
                      ],
                    ),
                  ),

                ],
              )),
        ),
      ),
    );
  }

  Widget _timerWidget(BuildContext context, double totalWorkingHours) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        AppTimer(
          label: context.translation.workingHours,
          timer: _formModel.timer,
          //TODO need to fix this..
          // enabled: _formModel.endDate == null,
          width: double.infinity,
          decoration: BoxDecoration(
            color: AppColor.neutral100,
            borderRadius: BorderRadius.circular(10),
          ),
          timerProgress: (isRunning) {},
          onChange: (timer) async {
            updateTimer(timer: timer);
            return true;
          },
        ),
        if (totalWorkingHours > 0.0) ...[
          12.height,
          Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.start,
            children: [
              'Total Working Time:'.bodyText2(context).custom(color: AppColor.neutral120),
              8.width,
              Text(
                ServiceRequestUtils.formatTimerDuration(totalWorkingHours.round()),
                style: AppTextStyles.bodyText.copyWith(color: AppColor.neutral50, fontWeight: FontWeight.w600),
              ),
            ],
          ),
        ],
      ],
    );
  }
}
