import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/profile_api_client.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/classes/utils.dart';
import 'package:mohem_flutter_app/config/routes.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/models/my_documents/employee_documents_list_model.dart';
import 'package:mohem_flutter_app/ui/my_attendance/dynamic_screens/dynamic_listview_screen.dart';
import 'package:mohem_flutter_app/ui/screens/my_documents/my_document_detail_screen.dart';
import 'package:mohem_flutter_app/widgets/my_document_item.dart';

class MyDocumentsFragment extends StatefulWidget {
  List<EmployeeDocumentsList>? list;

  MyDocumentsFragment(this.list, {Key? key}) : super(key: key);

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

class _MyDocumentsFragmentState extends State<MyDocumentsFragment> {
  int selectedIndex = 0;
  List<EmployeeDocumentsList>? documentsList;

  @override
  void initState() {
    super.initState();
    documentsList = widget.list;
  }

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

  @override
  void didUpdateWidget(covariant MyDocumentsFragment oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.list != widget.list) {
      documentsList = widget.list;
    }
  }

  @override
  Widget build(BuildContext context) {
    List<EmployeeDocumentsList> documentfilteredList = getTagBySelectedTab(selectedIndex);
    return Column(
      children: [
        GridView.count(
          crossAxisSpacing: 6,
          crossAxisCount: 4,
          childAspectRatio: 79 / 79,
          shrinkWrap: true,
          physics: const NeverScrollableScrollPhysics(),
          padding: const EdgeInsets.only(left: 21, right: 21, bottom: 11, top: 21),
          children: [
            gridViewItem(LocaleKeys.allDocuments.tr(), getTagBySelectedTab(0).length.toString(), 0, MyColors.darkTextColor),
            gridViewItem(LocaleKeys.expiredDocuments.tr(), getTagBySelectedTab(1).length.toString(), 1, MyColors.redA3Color),
            gridViewItem(LocaleKeys.missingDocuments.tr(), getTagBySelectedTab(2).length.toString(), 2, MyColors.yellowColor00),
            gridViewItem(LocaleKeys.uploadedDocuments.tr(), getTagBySelectedTab(3).length.toString(), 3, MyColors.greenColor),
          ],
        ),
        documentsList == null
            ? const SizedBox()
            : (documentfilteredList.isNotEmpty
                    ? ListView.separated(
                        physics: const BouncingScrollPhysics(),
                        padding: const EdgeInsets.only(left: 21, right: 21, bottom: 21, top: 11),
                        itemBuilder: (cxt, index) {
                          return MyDocumentItem(documentfilteredList[index], getColorByDocumentStatus(documentfilteredList[index].dOCUMENTSTATUS!)).onPress(() {
                            if (documentfilteredList[index].eNTITLEDTOAPPLYFLAG != 'Y') return;
                            Navigator.pushNamed(
                              context,
                              AppRoutes.addDynamicInput,
                              arguments: DynamicListViewParams(documentfilteredList[index].dOCUMENTTYPENAME!, documentfilteredList[index].fUNCTIONNAME!,
                                  selectedEmp: AppState().getUserName, isAttachmentMandatory: true),
                            );
                          });
                        },
                        separatorBuilder: (cxt, index) => 12.height,
                        itemCount: documentfilteredList.length)
                    : Utils.getNoDataWidget(context).center)
                .expanded,
      ],
    );
  }

  Widget gridViewItem(String title, String value, int index, Color color) {
    return Container(
      padding: const EdgeInsets.symmetric(vertical: 10, horizontal: 8),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(15),
        boxShadow: [
          BoxShadow(
            color: const Color(0xff000000).withOpacity(.05),
            blurRadius: 26,
            offset: const Offset(0, 0),
          ),
        ],
        border: Border.all(color: selectedIndex == index ? MyColors.selectedBorderColor : Colors.white, width: 2),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          title.toText10(),
          value.toText18(isBold: true, color: color),
        ],
      ),
    ).onPress(() {
      setState(() {
        selectedIndex = index;
      });
    });
  }

  Color getColorByDocumentStatus(String status) {
    Color _color;
    switch (status) {
      case "Expired":
        _color = MyColors.redA3Color;
        break;
      case "Exist":
        _color = MyColors.greenColor;
        break;
      case "Missing":
        _color = MyColors.yellowColor00;
        break;
      case "Not Exist":
        _color = MyColors.redA3Color;
        break;
      default:
        _color = MyColors.darkTextColor;
        break;
    }
    return _color;
  }

  List<EmployeeDocumentsList> getTagBySelectedTab(int index) {
    List<EmployeeDocumentsList> list = [];
    switch (index) {
      case 1:
        list = documentsList?.where((element) => element.dOCUMENTSTATUS == "Expired").toList() ?? [];
        break;
      case 2:
        list = documentsList?.where((element) => element.dOCUMENTSTATUS == "Missing").toList() ?? [];
        break;
      case 3:
        list = documentsList?.where((element) => element.dOCUMENTSTATUS == "Exist").toList() ?? [];
        break;
      default:
        list = documentsList ?? [];
        break;
    }
    return list;
  }
}
