import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/date_uitl.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/extensions/int_extensions.dart';
import 'package:mohem_flutter_app/extensions/string_extensions.dart';
import 'package:mohem_flutter_app/extensions/widget_extensions.dart';
import 'package:mohem_flutter_app/generated/locale_keys.g.dart';
import 'package:mohem_flutter_app/widgets/button/default_button.dart';
import 'package:mohem_flutter_app/widgets/dynamic_forms/dynamic_textfield_widget.dart';

class WorkListAdvanceSearch extends StatefulWidget {
  Function(int, String, String, String) onSearch;

  WorkListAdvanceSearch(this.onSearch, {Key? key}) : super(key: key);

  @override
  _WorkListAdvanceSearchState createState() {
    return _WorkListAdvanceSearchState();
  }
}

class _WorkListAdvanceSearchState extends State<WorkListAdvanceSearch> {
  final Map<int, String> advancedSearchViews = {};
  final Map<int, String> advancedSearchSearchBy = {};
  final Map<String, String> advancedSearchItemType = {};

  int selectedViewID = 1;
  String? selectedViewName;

  int selectedSearchByID = 0;
  String? selectedSearchByName;

  String selectedItemTypeID = "";
  String? selectedItemTypeName;

  String searchByInput = "";
  String searchByDate = "";

  DateTime selectedDate = DateTime.now();

  @override
  void initState() {
    super.initState();
    advancedSearchViews.addAll({1: LocaleKeys.openNot.tr(), 2: LocaleKeys.fyi.tr(), 3: LocaleKeys.toDo.tr(), 4: LocaleKeys.all.tr(), 5: LocaleKeys.meNot.tr()});
  }

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

  @override
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            LocaleKeys.advancedSearch.tr().toText18(),
            IconButton(
              icon: const Icon(Icons.close, color: MyColors.darkIconColor),
              onPressed: () => Navigator.pop(context),
            ),
          ],
        ),
        12.height,
        PopupMenuButton(
          child: DynamicTextFieldWidget(
            LocaleKeys.view.tr(),
            selectedViewName != null ? selectedViewName! : LocaleKeys.selectTypeT.tr(),
            isEnable: false,
            isPopup: true,
            isInputTypeNum: true,
            isReadOnly: false,
          ),
          itemBuilder: (_) => <PopupMenuItem<int>>[
            PopupMenuItem<int>(value: 1, child: Text(LocaleKeys.openNot.tr())),
            PopupMenuItem<int>(value: 2, child: Text(LocaleKeys.fyi.tr())),
            PopupMenuItem<int>(value: 3, child: Text(LocaleKeys.toDo.tr())),
            PopupMenuItem<int>(value: 4, child: Text(LocaleKeys.all.tr())),
            PopupMenuItem<int>(value: 5, child: Text(LocaleKeys.meNot.tr())),
          ],
          onSelected: (int popupIndex) {
            selectedViewID = popupIndex;
            selectedViewName = getSelectedViewName(popupIndex);
            setState(() {});
          },
        ),
        12.height,
        PopupMenuButton(
          child: DynamicTextFieldWidget(
            LocaleKeys.searchBy.tr(),
            selectedSearchByName != null ? selectedSearchByName! : LocaleKeys.selectTypeT.tr(),
            isEnable: false,
            isPopup: true,
            isInputTypeNum: true,
            isReadOnly: false,
          ),
          itemBuilder: (_) => <PopupMenuItem<int>>[
            PopupMenuItem<int>(value: 1, child: Text(LocaleKeys.fromUserName.tr())),
            PopupMenuItem<int>(value: 2, child: Text(LocaleKeys.subject.tr())),
            PopupMenuItem<int>(value: 3, child: Text(LocaleKeys.sentDate.tr())),
            PopupMenuItem<int>(value: 4, child: Text(LocaleKeys.itemTypeDisplayName.tr())),
            PopupMenuItem<int>(value: 5, child: Text(LocaleKeys.none.tr())),
          ],
          onSelected: (int popupIndex) {
            selectedSearchByID = popupIndex;
            selectedSearchByName = getSelectedSearchByName(popupIndex);
            setState(() {});
          },
        ),
        12.height,
        if (selectedSearchByID == 1 || selectedSearchByID == 2 || selectedSearchByID == 4)
          DynamicTextFieldWidget(
            LocaleKeys.searchBy.tr(),
            LocaleKeys.searchBy.tr(),
            isEnable: true,
            isPopup: false,
            lines: 1,
            isInputTypeNum: false,
            isReadOnly: false,
            onChange: (String value) {
              searchByInput = value;
            },
          ),
        if (selectedSearchByID == 3)
          DynamicTextFieldWidget(
            LocaleKeys.searchBy.tr(),
            searchByDate.isEmpty ? LocaleKeys.sentDate.tr() : searchByDate,
            suffixIconData: Icons.calendar_today,
            isEnable: false,
            onTap: () async {
              selectedDate = await Utils.selectDate(context, DateTime.now());
              searchByDate = selectedDate.day.toString() + "-" + DateUtil.getMonth(selectedDate.month) + "-" + selectedDate.year.toString();
              setState(() {});
            },
          ),
        12.height,
        PopupMenuButton(
          child: DynamicTextFieldWidget(
            LocaleKeys.itemType.tr(),
            selectedItemTypeName != null ? selectedItemTypeName! : LocaleKeys.selectTypeT.tr(),
            isEnable: false,
            isPopup: true,
            isInputTypeNum: true,
            isReadOnly: false,
          ),
          itemBuilder: (_) => <PopupMenuItem<String>>[
            const PopupMenuItem<String>(value: "HRSSA", child: Text("HR")),
            const PopupMenuItem<String>(value: "POAPPRV", child: Text("PO")),
            const PopupMenuItem<String>(value: "REQAPPRV", child: Text("PR")),
            const PopupMenuItem<String>(value: "INVMOA", child: Text("MR")),
            const PopupMenuItem<String>(value: "INVITEM", child: Text("IC")),
            const PopupMenuItem<String>(value: "STAMP", child: Text("STAMP")),
          ],
          onSelected: (String popupIndex) {
            selectedItemTypeID = popupIndex;
            selectedItemTypeName = getSelectedItemType(popupIndex);
            setState(() {});
          },
        ),
        20.height,
        DefaultButton(LocaleKeys.search.tr(), () async {
          Navigator.pop(context);
          widget.onSearch(selectedViewID, selectedItemTypeID, searchByInput, searchByDate);
        })
      ],
    ).paddingOnly(left: 21, right: 21, bottom: 21);
  }

  String getSelectedViewName(int index) {
    String returnVal = "";
    switch (index) {
      case 1:
        returnVal = LocaleKeys.openNot.tr();
        break;
      case 2:
        returnVal = LocaleKeys.fyi.tr();
        break;
      case 3:
        returnVal = LocaleKeys.toDo.tr();
        break;
      case 4:
        returnVal = LocaleKeys.all.tr();
        break;
      case 5:
        returnVal = LocaleKeys.meNot.tr();
        break;
    }
    return returnVal;
  }

  String getSelectedSearchByName(int index) {
    String returnVal = "";
    switch (index) {
      case 1:
        returnVal = LocaleKeys.fromUserName.tr();
        break;
      case 2:
        returnVal = LocaleKeys.subject.tr();
        break;
      case 3:
        returnVal = LocaleKeys.sentDate.tr();
        break;
      case 4:
        returnVal = LocaleKeys.itemType.tr();
        break;
      case 5:
        returnVal = LocaleKeys.none.tr();
        break;
    }
    return returnVal;
  }

  String getSelectedItemType(String index) {
    String returnVal = "";
    switch (index) {
      case "HRSSA":
        returnVal = "HR";
        break;
      case "POAPPRV":
        returnVal = "PO";
        break;
      case "REQAPPRV":
        returnVal = "PR";
        break;
      case "INVMOA":
        returnVal = "MR";
        break;
      case "INVITEM":
        returnVal = "IC";
        break;
      case "STAMP":
        returnVal = "STAMP";
        break;
    }
    return returnVal;
  }
}
