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

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:test_sa/dashboard_latest/dashboard_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/device/asset.dart';
import 'package:test_sa/models/enums/user_types.dart';
import 'package:test_sa/models/generic_attachment_model.dart';
import 'package:test_sa/models/lookup.dart';
import 'package:test_sa/models/new_models/mapped_sites.dart';
import 'package:test_sa/models/new_models/room_model.dart';
import 'package:test_sa/models/new_models/task_request/task_request_model.dart';
import 'package:test_sa/models/new_models/task_request/task_type_model.dart';
import 'package:test_sa/models/service_request/pending_service_request_model.dart';
import 'package:test_sa/modules/cm_module/utilities/service_request_utils.dart';
import 'package:test_sa/modules/cm_module/views/components/action_button/footer_action_button.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_text_form_field.dart';
import 'package:test_sa/new_views/common_widgets/single_item_drop_down_menu.dart';
import 'package:test_sa/providers/gas_request_providers/site_provider.dart';
import 'package:test_sa/providers/loading_list_notifier.dart';
import 'package:test_sa/providers/task_request_provider/task_job_provider.dart';
import 'package:test_sa/providers/task_request_provider/task_request_provider.dart';
import 'package:test_sa/views/widgets/date_and_time/date_picker.dart';
import 'package:test_sa/views/widgets/equipment/asset_picker.dart';
import 'package:test_sa/views/widgets/images/multi_image_picker.dart';

import '../../../../../../new_views/common_widgets/default_app_bar.dart';

class CreateTaskView extends StatefulWidget {
  static const String id = "/create-task";

  const CreateTaskView({Key? key}) : super(key: key);

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

class _CreateTaskViewState extends State<CreateTaskView> with TickerProviderStateMixin {
  final List<GenericAttachmentModel> attachments = [];
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
  List<Asset> _deviceList = [];
  Asset? device;
  PendingAssetServiceRequest? pendingAssetServiceRequest;
  TaskType? selectedType;
  AddTaskModel? _addTaskModel = AddTaskModel(id: 0);
  TextEditingController commentController = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    Provider.of<TaskTypeProvider>(context, listen: false).reset();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      appBar: DefaultAppBar(title: context.translation.createTaskRequest),
      body: SafeArea(
        child: Form(
          key: _formKey,
          child: Column(
            children: [
              SingleChildScrollView(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    SingleItemDropDownMenu<TaskType, TaskTypeProvider>(
                      context: context,
                      height: 56.toScreenHeight,
                      title: context.translation.taskType,
                      showShadow: false,
                      backgroundColor: AppColor.neutral100,
                      showAsBottomSheet: true,
                      initialValue: selectedType,
                      onSelect: (type) {
                        if (type != null) {
                          selectedType = type;
                          _addTaskModel = AddTaskModel(id: 0);
                          commentController.clear();
                          _addTaskModel?.taskTypeId = selectedType?.id;
                          setState(() {});
                        }
                      },
                    ),
                    8.height,
                    taskTypeWidget(selectedType?.relatedTo?.value),
                    8.height,
                    if (selectedType != null && selectedType!.isRecallAndAlert!) ...[
                      recallAndAlert(),
                    ],
                    8.height,
                    AppTextFormField(
                      initialValue: _addTaskModel?.callComment,
                      controller: commentController,
                      labelText: context.translation.callComments,
                      backgroundColor: AppColor.neutral100,
                      showShadow: false,
                      labelStyle: AppTextStyles.textFieldLabelStyle,
                      alignLabelWithHint: true,
                      textInputType: TextInputType.multiline,
                      onChange: (value) {
                        _addTaskModel?.callComment = value;
                      },
                    ),
                    23.height,
                    AttachmentPicker(
                      label: context.translation.attachImage,
                      attachment: attachments,
                      buttonColor: AppColor.black10,
                      onlyImages: false,
                      buttonIcon: 'image-plus'.toSvgAsset(color: AppColor.neutral120),
                      //verify this if not required delete this ..
                      onChange: (attachments) {
                        attachments = attachments;
                        setState(() {});
                      },
                    ),
                  ],
                ).toShadowContainer(context).paddingAll(16),
              ).expanded,
              FooterActionButton.footerContainer(
                context: context,
                child: AppFilledButton(
                  buttonColor: AppColor.primary10,
                  label: context.translation.submitRequest,
                  onPressed: _submit,
                  // buttonColor: AppColor.primary10,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget taskTypeWidget(int? type) {
    switch (type) {
      case 1:
        return scanAssetButton();
      case 2:
        return relatedToLocation();
      default:
        return const SizedBox();
    }
  }

  Widget relatedToLocation() {
    return Column(
      children: [
        8.height,
        SingleItemDropDownMenu<MappedSite, MappedSiteProvider>(
          context: context,
          title: context.translation.destinationSite,
          initialValue: _addTaskModel?.site,
          showAsBottomSheet: true,
          backgroundColor: AppColor.neutral100,
          showShadow: false,
          onSelect: (value) {
            _addTaskModel?.site = value;
            _addTaskModel?.building = null;
            _addTaskModel?.floor = null;
            _addTaskModel?.department = null;
            setState(() {});
          },
        ),
        8.height,
        SingleItemDropDownMenu<MappedBuilding, NullableLoadingProvider>(
          context: context,
          title: context.translation.building,
          backgroundColor: AppColor.neutral100,
          showAsBottomSheet: true,
          showShadow: false,
          initialValue: _addTaskModel?.building,
          enabled: _addTaskModel?.site?.buildings?.isNotEmpty ?? false,
          staticData: _addTaskModel?.site?.buildings ?? [],
          onSelect: (value) {
            _addTaskModel?.building = value;
            _addTaskModel?.floor = null;
            _addTaskModel?.department = null;
            setState(() {});
          },
        ),
        8.height,
        SingleItemDropDownMenu<MappedFloor, NullableLoadingProvider>(
          context: context,
          showAsBottomSheet: true,
          backgroundColor: AppColor.neutral100,
          showShadow: false,
          title: context.translation.floor,
          initialValue: _addTaskModel?.floor,
          enabled: _addTaskModel?.building?.floors?.isNotEmpty ?? false,
          staticData: _addTaskModel?.building?.floors ?? [],
          onSelect: (value) {
            _addTaskModel?.floor = value;
            _addTaskModel?.department = null;
            setState(() {});
          },
        ),
        8.height,
        SingleItemDropDownMenu<MappedDepartment, NullableLoadingProvider>(
          context: context,
          title: context.translation.department,
          backgroundColor: AppColor.neutral100,
          showAsBottomSheet: true,
          showShadow: false,
          initialValue: _addTaskModel?.department,
          enabled: _addTaskModel?.floor?.departments?.isNotEmpty ?? false,
          staticData: _addTaskModel?.floor?.departments ?? [],
          onSelect: (value) {
            _addTaskModel?.department = value;
            _addTaskModel?.room = null;
            setState(() {});
          },
        ),
        8.height,
        SingleItemDropDownMenu<Rooms, NullableLoadingProvider>(
          context: context,
          title: context.translation.room,
          backgroundColor: AppColor.neutral100,
          showShadow: false,
          showAsBottomSheet: true,
          initialValue: _addTaskModel?.room,
          enabled: _addTaskModel?.department?.rooms?.isNotEmpty ?? false,
          staticData: _addTaskModel?.department?.rooms ?? [],
          onSelect: (value) {
            _addTaskModel?.room = value;
            setState(() {});
          },
        ),
        16.height,
      ],
    );
  }

  Widget recallAndAlert() {
    return Column(
      children: [
        SingleItemDropDownMenu<Lookup, TaskJobTypeOfAlertProvider>(
          context: context,
          height: 56.toScreenHeight,
          // title: context.translation.typeofAlert,
          title: 'Type Of Alert',
          showShadow: false,
          backgroundColor: AppColor.neutral100,
          showAsBottomSheet: true,
          initialValue: _addTaskModel?.typeOfAlert,
          onSelect: (status) {
            if (status != null) {
              _addTaskModel?.typeOfAlert = status;
              setState(() {});
            }
          },
        ),
        8.height,
        SingleItemDropDownMenu<Lookup, TaskJobRiskLevelProvider>(
          context: context,
          height: 56.toScreenHeight,
          // title: context.translation.typeofAlert,
          title: 'Risk Level',
          showShadow: false,
          backgroundColor: AppColor.neutral100,
          showAsBottomSheet: true,
          initialValue: _addTaskModel?.riskLevel,
          onSelect: (status) {
            if (status != null) {
              _addTaskModel?.riskLevel = status;
              setState(() {});
            }
          },
        ),
        8.height,
        SingleItemDropDownMenu<Lookup, TaskJobResourceProvider>(
          context: context,
          height: 56.toScreenHeight,
          // title: context.translation.typeofAlert,
          title: 'Resources',
          showShadow: false,
          backgroundColor: AppColor.neutral100,
          showAsBottomSheet: true,
          initialValue: _addTaskModel?.resource,
          onSelect: (status) {
            if (status != null) {
              _addTaskModel?.resource = status;
              setState(() {});
            }
          },
        ),
        8.height,
        if (_addTaskModel?.typeOfAlert?.value != 1) ...[
          SingleItemDropDownMenu<Lookup, TaskJobActionNeededProvider>(
            context: context,
            height: 56.toScreenHeight,
            title: 'Action Needed',
            showShadow: false,
            backgroundColor: AppColor.neutral100,
            showAsBottomSheet: true,
            initialValue: _addTaskModel?.actionNeeded,
            onSelect: (status) {
              if (status != null) {
                _addTaskModel?.actionNeeded = status;
                setState(() {});
              }
            },
          ),
        ],
        8.height,
        SingleItemDropDownMenu<TaskEvaluatorUser, TaskEvaluatorUserProvider>(
          context: context,
          height: 56.toScreenHeight,
          // title: context.translation.typeofAlert,
          title: 'Evaluator User',
          showShadow: false,
          backgroundColor: AppColor.neutral100,
          showAsBottomSheet: true,
          initialValue: _addTaskModel?.taskEvaluatorUser,
          onSelect: (status) {
            if (status != null) {
              _addTaskModel?.taskEvaluatorUser = status;
              setState(() {});
            }
          },
        ),
        8.height,
        if (_addTaskModel?.typeOfAlert?.value != 1) ...[
          ADatePicker(
            label: "Estimated delivery date",
            // label: context.translation.returnToService,
            hideShadow: true,
            backgroundColor: AppColor.neutral100,
            date: DateTime.tryParse(_addTaskModel?.estimationDeliveryDate ?? ""),
            formatDateWithTime: true,
            onDatePicker: (selectedDate) {
              showTimePicker(
                context: context,
                initialTime: TimeOfDay.now(),
              ).then((selectedTime) {
                // Handle the selected date and time here.
                if (selectedTime != null) {
                  DateTime? selectedDateTime = DateTime(
                    selectedDate.year,
                    selectedDate.month,
                    selectedDate.day,
                    selectedTime.hour,
                    selectedTime.minute,
                  );
                  setState(() {
                    _addTaskModel?.estimationDeliveryDate = selectedDateTime.toIso8601String();
                  });
                }
              });
            },
          ),
        ],
        8.height,
        AppTextFormField(
          // labelText: context.translation.travelingHours,
          labelText: 'Reason Of FSCA',
          backgroundColor: AppColor.neutral100,
          showShadow: false,
          labelStyle: AppTextStyles.textFieldLabelStyle,
          initialValue: _addTaskModel?.reasonOfFSCA ?? '',
          textInputType: TextInputType.text,
          onChange: (value) {
            _addTaskModel?.reasonOfFSCA = value;
          },
        ),
        8.height,
        AppTextFormField(
          // labelText: context.translation.travelingHours,
          labelText: 'Corrective Action Description',
          backgroundColor: AppColor.neutral100,
          showShadow: false,
          labelStyle: AppTextStyles.textFieldLabelStyle,
          initialValue: _addTaskModel?.correctiveActionDescription ?? '',
          textInputType: TextInputType.text,
          onChange: (value) {
            _addTaskModel?.correctiveActionDescription = value;
          },
        ),
        8.height,
        AppTextFormField(
          // labelText: context.translation.travelingHours,
          labelText: 'Alert No',
          backgroundColor: AppColor.neutral100,
          showShadow: false,
          labelStyle: AppTextStyles.textFieldLabelStyle,
          initialValue: _addTaskModel?.alertNo ?? '',
          textInputType: TextInputType.text,
          onChange: (value) {
            _addTaskModel?.alertNo = value;
          },
        ),
        16.height
      ],
    );
  }

  Widget scanAssetButton() {
    return selectedType?.linkWithMultiAssets == true
        ? AssetPicker(
            deviceList: _deviceList,
            showLoading: false,
            borderColor: AppColor.black20,
            buttonColor: AppColor.white936,
            multiSelection: true,
            onAssetRemove: (itemId) {
              _deviceList.removeWhere((asset) => asset.id?.toInt() == itemId);
              setState(() {});
            },
            onMultiAssetPick: (assetList) {
              _deviceList.addAll(assetList);
              final seenIds = <num?>{};
              _deviceList = _deviceList.where((item) => seenIds.add(item.id)).toList();

              setState(() {});
            },
          )
        : AssetPicker(
            device: device,
            showLoading: false,
            borderColor: AppColor.black20,
            onPick: (asset) async {
              device = asset;
              setState(() {});
            },
          );
  }

  Future<void> _submit() async {
    if (_formKey.currentState!.validate()) {
      _formKey.currentState!.save();

      _addTaskModel?.attachments = [];
      _addTaskModel?.assetIds = [];
      if (selectedType?.linkWithMultiAssets == true) {
        for (var element in _deviceList) {
          _addTaskModel!.assetIds?.add(int.parse(element.id.toString()));
        }
      } else {
        if (device != null) {
          _addTaskModel!.assetIds?.add(int.parse(device!.id.toString()));
        }
      }
      for (var item in attachments) {
        String fileName = ServiceRequestUtils.isLocalUrl(item.name ?? '') ? ("${item.name ?? ''.split("/").last}|${base64Encode(File(item.name ?? '').readAsBytesSync())}") : item.name ?? '';
        _addTaskModel?.attachments?.add(TaskJobAttachment(id: item.id, name: fileName));
      }
      TaskRequestProvider taskRequestProvider = Provider.of<TaskRequestProvider>(context, listen: false);
      await taskRequestProvider.addTask(context: context, task: _addTaskModel!);
      if (taskRequestProvider.stateCode == 200) {
        DashBoardProvider dashBoardProvider = Provider.of<DashBoardProvider>(context, listen: false);
        dashBoardProvider.refreshDashboard(context: context, userType: UsersTypes.nurse);
      }
    }
  }
}
