import 'dart:convert';

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/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/common_widgets/app_filled_button.dart';
import 'package:test_sa/providers/loading_list_notifier.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 RequestGasRefill extends StatefulWidget {
  static const String id = "/request-gas-refill";
  final GasRefillModel? gasRefillModel;

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

  @override
  State<RequestGasRefill> createState() => _RequestGasRefillState();
}

class _RequestGasRefillState extends State<RequestGasRefill> {
  final bool _isLoading = false;
  bool _validate = false;
  Uint8List? _engineerSignature;
  Uint8List? _nurseSignature;

  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(gazRefillDetails: []);
  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();
    if (widget.gasRefillModel != null) {
      _formModel.fromGasRefillModel(widget.gasRefillModel!);
      _commentController.text = _formModel.comment ?? "";
      try {
        _deliveredQuantity = deliveredQuantity.singleWhere((element) => element.value == _formModel.gazRefillDetails![0].deliverdQty);
        _currentDetails.deliverdQty = _deliveredQuantity!.value;
      } catch (ex) {}
    }
  }

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

  _onSubmit(BuildContext context) async {
    if (_formModel.status == null) {
      await Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.status}");
      return false;
    }
    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 (_formModel.gazRefillDetails?.isNotEmpty ?? false) {
      if (!(await _addNewModel(context))) return;
    }

    setState(() {});
    _formKey.currentState!.save();
    _formModel.comment = _commentController.text;
    int? status = widget.gasRefillModel == null
        ? 0 /*await _gasRefillProvider.createModel(
            user: _userProvider.user,
            model: _formModel,
          )*/
        : await _gasRefillProvider!.updateModel(
            user: _userProvider.user!,
            host: _settingProvider.host!,
            oldModel: widget.gasRefillModel!,
            newModel: _formModel,
          );
    //_isLoading = false;
    setState(() {});
    if (status >= 200 && status < 300) {
      Fluttertoast.showToast(
        msg: context.translation.successfulRequestMessage,
      );
      Navigator.of(context).pop(_formModel);
      setState(() {});
    } else {
      String errorMessage = HttpStatusManger.getStatusMessage(status: status, subtitle: context.translation);
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(
        content: Text(errorMessage),
      ));
    }
  }

  bool _addNewModel(BuildContext context)  {
    _validate = true;
    if (_currentDetails.deliverdQty == null) {
      Fluttertoast.showToast(msg: "${context.translation.youHaveToSelect} ${context.translation.quantity}");
      setState(() {});
      return false;
    }
    // if (!_formKey.currentState!.validate()) {
    //   setState(() {});
    //   return false;
    // }
    _formKey.currentState!.save();
    _currentDetails.gasType = _formModel.gazRefillDetails![0].gasType;
    _currentDetails.cylinderSize = _formModel.gazRefillDetails![0].cylinderSize;
    _currentDetails.cylinderType = _formModel.gazRefillDetails![0].cylinderType;
    _currentDetails.requestedQty = _formModel.gazRefillDetails![0].requestedQty;
    if (!( _currentDetails.validate(context))) {
      setState(() {});
      return false;
    }
    _formModel.gazRefillDetails![0].deliverdQty = _currentDetails.deliverdQty;
    _validate = false;
    // Scrollable.ensureVisible(_DetailsKey.currentContext);
    _deliveredQuantityController.clear();
    _workingHoursController.clear();
    //_commentController.clear();
    _currentDetails = GasRefillDetails();
    setState(() {});
    return true;
  }

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

  @override
  Widget build(BuildContext context) {

    _userProvider = Provider.of<UserProvider>(context);
    _settingProvider = Provider.of<SettingProvider>(context);
    _gasRefillProvider ??= Provider.of<GasRefillProvider>(context, listen: false);
    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.updateRequest),
      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!.gazRefillDetails![0].gasType!.name}'.bodyText(context),
                            '${context.translation.cylinderType}: ${widget.gasRefillModel!.gazRefillDetails![0].cylinderType!.name}'.bodyText(context),
                            '${context.translation.cylinderSize}: ${widget.gasRefillModel!.gazRefillDetails![0].cylinderSize!.name}'.bodyText(context),
                            '${context.translation.quantity}: ${widget.gasRefillModel!.gazRefillDetails![0].requestedQty ?? 0}'.bodyText(context),
                            '${context.translation.site}: ${widget.gasRefillModel!.site?.name}'.bodyText(context),
                          ],
                        ).toShadowContainer(context),
                        8.height,
                        AppTimer(
                          label: context.translation.workingHours,
                          timer: _formModel.timer,
                          enabled: _formModel.endDate == null,
                          onChange: (timer) async {
                            _formModel.timer = timer;
                            return true;
                          },
                        ),
                        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,
                        SingleItemDropDownMenu<Lookup, NullableLoadingProvider>(
                          context: context,
                          title: context.translation.quantity,
                          initialValue: _deliveredQuantity,
                          staticData: deliveredQuantity,
                          onSelect: (value) {
                            _deliveredQuantity = value;
                            _currentDetails.deliverdQty = value!.value;
                          },
                        ),
                        // 8.height,
                        // AppTextFormField(
                        //   initialValue: _formModel.gazRefillDetails?[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,

                        /// TBD
                        AppTextFormField(
                          labelText: context.translation.comments,
                          textInputType: TextInputType.multiline,
                          alignLabelWithHint: true,
                          controller: _commentController,
                          onSaved: (value) {},
                        ),
                        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);
                          },
                        ),
                      ],
                    ),
                  ).expanded,
                  AppFilledButton(
                    label: widget.gasRefillModel == null ? context.translation.submit : context.translation.update,
                    onPressed: () async {
                      _onSubmit.call(context);
                    },
                  ).paddingAll(16),
                ],
              )),
        ),
      ),
    );
  }
}
