import 'package:easy_localization/src/public_ext.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/classes/date_uitl.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/get_absence_collection_notification_body_list_model.dart';
import 'package:mohem_flutter_app/models/get_item_creation_ntf_body_list_model.dart';
import 'package:mohem_flutter_app/models/get_po_notification_body_list_model.dart';
import 'package:mohem_flutter_app/models/get_stamp_ms_notification_body_list_model.dart';
import 'package:mohem_flutter_app/models/get_stamp_ns_notification_body_list_model.dart';
import 'package:mohem_flutter_app/models/worklist/hr/eit_otification_body_model.dart';
import 'package:mohem_flutter_app/models/worklist/hr/get_basic_det_ntf_body_list_model.dart';
import 'package:mohem_flutter_app/models/worklist/hr/get_contact_notification_body_list_model.dart';
import 'package:mohem_flutter_app/models/worklist/hr/get_phones_notification_body_list_model.dart';
import 'package:mohem_flutter_app/models/worklist_response_model.dart';
import 'package:mohem_flutter_app/widgets/item_detail_view_widget.dart';

class InfoFragment extends StatelessWidget {
  WorkListResponseModel? workListData;
  List<POHeader> poHeaderList;
  List<GetStampMsNotificationBodyList>? getStampMsNotifications;
  List<GetStampNsNotificationBodyList>? getStampNsNotifications;
  List<ItemCreationHeader> itemCreationHeader;
  List<GetEitCollectionNotificationBodyList>? getEitCollectionNotificationBodyList;
  List<GetPhonesNotificationBodyList>? getPhonesNotificationBodyList;
  List<GetBasicDetNtfBodyList>? getBasicDetNtfBodyList;
  List<GetAbsenceCollectionNotificationBodyList>? getAbsenceCollectionNotificationBodyList;
  GetContactNotificationBodyList? getContactNotificationBodyList;

  InfoFragment({
    this.workListData,
    this.poHeaderList = const <POHeader>[],
    this.itemCreationHeader = const <ItemCreationHeader>[],
    this.getStampMsNotifications,
    this.getStampNsNotifications,
    this.getEitCollectionNotificationBodyList,
    this.getPhonesNotificationBodyList,
    this.getBasicDetNtfBodyList,
    this.getAbsenceCollectionNotificationBodyList,
    this.getContactNotificationBodyList,
  });

  @override
  Widget build(BuildContext context) {
    List<Widget> uiList = [
      if ((workListData?.iTEMTYPE ?? "") == "INVMOA")
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            ItemDetailView(LocaleKeys.from.tr(), workListData!.fROMUSER ?? ""),
            ItemDetailView(LocaleKeys.to.tr(), workListData!.tOUSER ?? ""),
            ItemDetailView(LocaleKeys.sent.tr(), workListData!.bEGINDATE ?? ""),
            ItemDetailView(LocaleKeys.closed.tr(), workListData!.eNDDATE ?? ""),
            ItemDetailView(LocaleKeys.id.tr(), workListData!.nOTIFICATIONID?.toString() ?? ""),
            ItemDetailView(LocaleKeys.responder.tr(), workListData!.rESPONDER ?? ""),
            ItemDetailView(LocaleKeys.subject.tr(), workListData!.sUBJECT ?? ""),
          ],
        ).objectContainerView(),
      if (getStampMsNotifications?.isNotEmpty ?? false) getStampMsNotificationsListView(getStampMsNotifications ?? []).objectContainerView(title: "Stamp Notifications"),
      if (getStampNsNotifications?.isNotEmpty ?? false) getStampNsNotificationsListView(getStampNsNotifications ?? []).objectContainerView(title: "Stamp Notifications"),
      if (poHeaderList.isNotEmpty) getPoNotificationsListView(),
      if (itemCreationHeader.isNotEmpty) getItemCreationHeaderView(),
      if (getEitCollectionNotificationBodyList?.isNotEmpty ?? false) getEitNotificationsListView(getEitCollectionNotificationBodyList ?? []),
      if (getPhonesNotificationBodyList?.isNotEmpty ?? false) getPhonesNotificationBodyListWidget(getPhonesNotificationBodyList ?? []).objectContainerView(title: "Mobile Numbers"),
      if (getBasicDetNtfBodyList?.isNotEmpty ?? false) getBasicDetNtfBodyListWidget(getBasicDetNtfBodyList ?? []).objectContainerView(title: "Basic Information"),
      if (getAbsenceCollectionNotificationBodyList?.isNotEmpty ?? false) getAbsenceCollectionNotificationBodyListWidget(getAbsenceCollectionNotificationBodyList ?? []),
      if (getContactNotificationBodyList != null) getContactNotificationBodyListWidget(getContactNotificationBodyList ?? GetContactNotificationBodyList()).objectContainerView(),
    ];
    return Container(
      width: double.infinity,
      height: double.infinity,
      child: uiList.isEmpty
          ? LocaleKeys.noDataAvailable.tr().toText16().center
          : ListView(
              padding: const EdgeInsets.all(21),
              children: uiList,
            ),
    );
  }

  Widget getPoNotificationsListView() {
    return ListView.separated(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (cxt, index) => Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisSize: MainAxisSize.min,
              children: [
                ItemDetailView(LocaleKeys.description.tr(), workListData!.fROMUSER ?? ""),
                ItemDetailView(LocaleKeys.from.tr(), workListData!.fROMUSER ?? ""),
                ItemDetailView(LocaleKeys.to.tr(), workListData!.tOUSER ?? ""),
                ItemDetailView(LocaleKeys.sent.tr(), workListData!.bEGINDATE ?? ""),
                ItemDetailView(LocaleKeys.closed.tr(), workListData!.eNDDATE ?? ""),
                ItemDetailView(LocaleKeys.id.tr(), workListData!.nOTIFICATIONID?.toString() ?? ""),
                ItemDetailView(LocaleKeys.supplier.tr(), poHeaderList[index].vENDORNAME ?? ""),
                ItemDetailView(LocaleKeys.site.tr(), poHeaderList[index].vENDORSITECODE ?? ""),
                ItemDetailView(LocaleKeys.buyer.tr(), poHeaderList[index].bUYER ?? ""),
                ItemDetailView(LocaleKeys.preparer.tr(), poHeaderList[index].pREPARER ?? ""),
                ItemDetailView(LocaleKeys.creationDate.tr(), poHeaderList[index].cREATIONDATE ?? ""),
                ItemDetailView(LocaleKeys.shipToLocation.tr(), poHeaderList[index].sHIPTOLOCATIONNAME ?? ""),
                ItemDetailView(LocaleKeys.quotationNumber.tr(), poHeaderList[index].qUOTATIONNUMBER ?? ""),
                ItemDetailView(LocaleKeys.quotationDate.tr(), poHeaderList[index].qUOTATIONDATE ?? ""),
                ItemDetailView(LocaleKeys.paymentTerms.tr(), poHeaderList[index].pAYMENTTERMS ?? ""),
                ItemDetailView(LocaleKeys.currency.tr(), poHeaderList[index].cURRENCYNAME ?? ""),
                ItemDetailView(LocaleKeys.grossAmount.tr(), poHeaderList[index].gROSSAMOUNT?.toString() ?? ""),
                ItemDetailView(LocaleKeys.discountAmount.tr(), poHeaderList[index].dISCOUNTAMOUNT?.toString() ?? ""),
                ItemDetailView(LocaleKeys.customDuty.tr(), poHeaderList[index].cUSTOMDUTY?.toString() ?? ""),
                ItemDetailView(LocaleKeys.shipHandle.tr(), poHeaderList[index].sHIPHANDLE?.toString() ?? ""),
                ItemDetailView(LocaleKeys.otherCharges.tr(), poHeaderList[index].oTHERCHARGES?.toString() ?? ""),
                ItemDetailView(LocaleKeys.totalPOAmountWithVAT.tr(), poHeaderList[index].qUOTATIONDATE ?? ""),
                ItemDetailView(LocaleKeys.totalPOAmountInWords.tr(), poHeaderList[index].tOTPOAMTWORD ?? ""),
              ],
            ).objectContainerView(),
        separatorBuilder: (cxt, index) => 4.height,
        itemCount: poHeaderList.length);
  }

  // Widget getAbsenceCollectionNotificationsListView(List<GetAbsenceCollectionNotificationBodyList> list) {
  //   List<CollectionNotificationAbsence> dataList = list.isEmpty ? [] : (list.first.collectionNotification ?? []);
  //   return ListView.separated(
  //       shrinkWrap: true,
  //       physics: const NeverScrollableScrollPhysics(),
  //       itemBuilder: (cxt, index) => ItemDetailView(dataList[index].sEGMENTPROMPT!, dataList[index].sEGMENTVALUEDSP!),
  //       separatorBuilder: (cxt, index) => 4.height,
  //       itemCount: dataList.length);
  // }

  Widget getStampMsNotificationsListView(List<GetStampMsNotificationBodyList> list) {
    return ListView.separated(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (cxt, index) => Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                ItemDetailView(LocaleKeys.employeeNumber.tr(), list[index].eMPLOYEENUMBER.toString()),
                ItemDetailView(LocaleKeys.assignmentNumber.tr(), list[index].aSSIGNMENTNUMBER.toString()),
                ItemDetailView(LocaleKeys.employeeName.tr(), list[index].eMPLOYEENAME.toString()),
                ItemDetailView(LocaleKeys.scheduleDate.tr(), DateUtil.formatDateToDate(DateUtil.convertStringToDate(list[index].sCHEDULEDATE.toString()), false)),
                ItemDetailView(LocaleKeys.shiftType.tr(), list[index].sHTTYPEDESC.toString()),
                ItemDetailView(LocaleKeys.shift.tr(), list[index].sHTNAME.toString()),
                ItemDetailView(LocaleKeys.breakText.tr(), list[index].bREAKNAME.toString()),
                ItemDetailView(LocaleKeys.actualSwipeStart.tr(), list[index].sHTACTUALSTARTTIME.toString()),
                ItemDetailView(LocaleKeys.actualSwipeEnd.tr(), list[index].sHTACTUALENDTIME.toString()),
                ItemDetailView(LocaleKeys.approvedSwipeStart.tr(), list[index].aPPROVEDSTARTTIME.toString()),
                ItemDetailView(LocaleKeys.approvedSwipeStartReason.tr(), list[index].aPPROVEDSTARTREASON.toString()),
                ItemDetailView(LocaleKeys.approvedSwipeEnd.tr(), ""),
                ItemDetailView(LocaleKeys.approvedSwipeEndReason.tr(), list[index].aPPROVEDENDREASONDESC.toString()),
              ],
            ),
        separatorBuilder: (cxt, index) => 18.height,
        itemCount: list.length);
  }

  Widget getStampNsNotificationsListView(List<GetStampNsNotificationBodyList> list) {
    return ListView.separated(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (cxt, index) => Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                ItemDetailView(LocaleKeys.employeeNumber.tr(), list[index].eMPLOYEENUMBER.toString()),
                ItemDetailView(LocaleKeys.assignmentNumber.tr(), list[index].aSSIGNMENTNUMBER.toString()),
                ItemDetailView(LocaleKeys.employeeName.tr(), list[index].eMPLOYEENAME.toString()),
                ItemDetailView(LocaleKeys.scheduleDate.tr(), DateUtil.formatDateToDate(DateUtil.convertStringToDate(list[index].sCHEDULEDATE.toString()), false)),
              ],
            ),
        separatorBuilder: (cxt, index) => 18.height,
        itemCount: list.length);
  }

  Widget getItemCreationHeaderView() {
    return ListView.separated(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (cxt, index) => Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                ItemDetailView(LocaleKeys.operatingUnit.tr(), itemCreationHeader[index].oPERATINGUNIT?.toString() ?? ""),
                ItemDetailView(LocaleKeys.category.tr(), itemCreationHeader[index].cATEGORY?.toString() ?? ""),
                ItemDetailView(LocaleKeys.requester.tr(), itemCreationHeader[index].rEQUESTER?.toString() ?? ""),
                ItemDetailView(LocaleKeys.analyzedBy.tr(), itemCreationHeader[index].aNALYZEDBY?.toString() ?? ""),
                ItemDetailView(LocaleKeys.approvedDate.tr(), itemCreationHeader[index].aPPROVEDDATE?.toString() ?? ""),
                ItemDetailView(LocaleKeys.itemType.tr(), itemCreationHeader[index].iTEMTYPE?.toString() ?? ""),
                ItemDetailView(LocaleKeys.relatedTo.tr(), itemCreationHeader[index].rELATEDTO?.toString() ?? ""),
                ItemDetailView(LocaleKeys.requestDate.tr(), DateUtil.formatDateToDate(DateUtil.convertStringToDate(itemCreationHeader[index].rEQUESTDATE.toString()), false)),
                ItemDetailView(LocaleKeys.analyzedDate.tr(), itemCreationHeader[index].aNALYZEDDATE?.toString() ?? ""),
                ItemDetailView(LocaleKeys.urgent.tr(), itemCreationHeader[index].uRGENTFLAGDISP?.toString() ?? ""),
              ],
            ).objectContainerView(),
        separatorBuilder: (cxt, index) => 18.height,
        itemCount: itemCreationHeader.length);
  }

  //HR multiple type requests
  Widget getEitNotificationsListView(List<GetEitCollectionNotificationBodyList> list) {
    return ListView.separated(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      itemBuilder: (cxt, index) {
        List<CollectionNotificationEit> dataList = list.isEmpty ? [] : (list[index].collectionNotification ?? []);
        return ListView.separated(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                itemBuilder: (cxt, index) => dataList[index].displayFlag == "Y" ? ItemDetailView(dataList[index].segmentPrompt!, dataList[index].segmentValueDsp!) : Container(),
                separatorBuilder: (cxt, index) => dataList[index].displayFlag == "Y" ? 4.height : 0.height,
                itemCount: dataList.length)
            .objectContainerView();
      },
      separatorBuilder: (cxt, index) => 12.height,
      itemCount: list.length,
    );
  }

  Widget getPhonesNotificationBodyListWidget(List<GetPhonesNotificationBodyList> list) {
    return ListView.separated(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (cxt, index) => ItemDetailView(LocaleKeys.subject.tr(), list[index].proposedPhoneNumber ?? ""),
        separatorBuilder: (cxt, index) => 4.height,
        itemCount: list.length);
  }

  Widget getBasicDetNtfBodyListWidget(List<GetBasicDetNtfBodyList> list) {
    return ListView.separated(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (cxt, index) => ItemDetailView(list[index].segmentPrompt!, list[index].segmentValueDsp!),
        separatorBuilder: (cxt, index) => 4.height,
        itemCount: list.length);
  }

  Widget getAbsenceCollectionNotificationBodyListWidget(List<GetAbsenceCollectionNotificationBodyList> list) {
    return ListView.separated(
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      itemBuilder: (cxt, index) {
        List<CollectionNotificationAbsence> dataList = list.isEmpty ? [] : (list[index].collectionNotification ?? []);
        return ListView.separated(
                shrinkWrap: true,
                physics: const NeverScrollableScrollPhysics(),
                itemBuilder: (cxt, index) => dataList[index].dISPLAYFLAG == "Y" ? ItemDetailView(dataList[index].sEGMENTPROMPT!, dataList[index].sEGMENTVALUEDSP!) : Container(),
                separatorBuilder: (cxt, index) => dataList[index].dISPLAYFLAG == "Y" ? 4.height : 0.height,
                itemCount: dataList.length)
            .objectContainerView();
      },
      separatorBuilder: (cxt, index) => 12.height,
      itemCount: list.length,
    );
  }

  Widget getContactNotificationBodyListWidget(GetContactNotificationBodyList data) {
    return ListView.separated(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemBuilder: (cxt, index) => ItemDetailView(data.contactNotificationBody![index].segmentPrompt!, data.contactNotificationBody![index].segmentValueDsp!),
        separatorBuilder: (cxt, index) => 4.height,
        itemCount: data.contactNotificationBody!.length);
  }
}
