import 'dart:async';

import 'package:mohem_flutter_app/api/api_client.dart';
import 'package:mohem_flutter_app/app_state/app_state.dart';
import 'package:mohem_flutter_app/classes/consts.dart';
import 'package:mohem_flutter_app/models/dashboard/itg_forms_model.dart';
import 'package:mohem_flutter_app/models/generic_response_model.dart';
import 'package:mohem_flutter_app/models/get_absence_collection_notification_body_list_model.dart';
import 'package:mohem_flutter_app/models/get_action_history_list_model.dart';
import 'package:mohem_flutter_app/models/get_attachement_list_model.dart';
import 'package:mohem_flutter_app/models/get_item_creation_ntf_body_list_model.dart';
import 'package:mohem_flutter_app/models/get_mo_Item_history_list_model.dart';
import 'package:mohem_flutter_app/models/get_mo_notification_body_list_model.dart';
import 'package:mohem_flutter_app/models/get_notification_buttons_list_model.dart';
import 'package:mohem_flutter_app/models/get_po_Item_history_list_model.dart';
import 'package:mohem_flutter_app/models/get_po_notification_body_list_model.dart';
import 'package:mohem_flutter_app/models/get_quotation_analysis_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/itg_forms_models/itg_request_model.dart';
import 'package:mohem_flutter_app/models/member_information_list_model.dart';
import 'package:mohem_flutter_app/models/notification_get_respond_attributes_list_model.dart';
import 'package:mohem_flutter_app/models/worklist_response_model.dart';

class WorkListApiClient {
  static final WorkListApiClient _instance = WorkListApiClient._internal();

  WorkListApiClient._internal();

  factory WorkListApiClient() => _instance;

  Future<List<WorkListResponseModel>?> getWorkList(int pPageNum, String pItemType) async {
    String url = "${ApiConsts.erpRest}GET_WORKLIST";
    Map<String, dynamic> postParams = {
      "P_NOTIFICATION_TYPE": "1",
      "P_PAGE_NUM": pPageNum,
      "P_PAGE_LIMIT": 50,
      "P_ITEM_TYPE": pItemType,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getWorkList;
    }, url, postParams);
  }

  Future<ItgFormsModel?> getITGTaskCountRequestType() async {
    String url = "${ApiConsts.cocRest}ITGGetTaskCountRequestType";
    Map<String, dynamic> postParams = {};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      ItgFormsModel responseData = ItgFormsModel.fromJson(json);
      return responseData;
    }, url, postParams);
  }

  Future<GenericResponseModel> getSubordinatesLeaves(String fromDate, String toDate) async {
    String url = "${ApiConsts.erpRest}GET_SUBORDINATES_LEAVES";
    Map<String, dynamic> postParams = {"P_DATE_FROM": "/Date(1639861200000+0300)/", "P_DATE_TO": "/Date(1640120400000+0300)/"};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData;
    }, url, postParams);
  }

  Future<List<GetAttachementList>> getAttachments(int pNotificationID) async {
    String url = "${ApiConsts.erpRest}GET_ATTACHMENTS";
    Map<String, dynamic> postParams = {"P_NOTIFICATION_ID": pNotificationID};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.getAttachementList ?? [];
    }, url, postParams);
  }

  Future<List<GetActionHistoryList>> getActionHistory(int pNotificationID) async {
    String url = "${ApiConsts.erpRest}GET_ACTION_HISTORY";
    Map<String, dynamic> postParams = {
      "P_NOTIFICATION_ID": pNotificationID,
      "P_PAGE_LIMIT": 100,
      "P_PAGE_NUM": 1,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.getActionHistoryList ?? [];
    }, url, postParams);
  }

  Future<List<GetNotificationButtonsList>> getNotificationButtons(int pNotificationID) async {
    String url = "${ApiConsts.erpRest}GET_NOTIFICATION_BUTTONS";
    Map<String, dynamic> postParams = {"P_NOTIFICATION_ID": pNotificationID};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.getNotificationButtonsList ?? [];
    }, url, postParams);
  }

  Future<List<NotificationGetRespondAttributesList>> notificationGetRespondAttributes(int pNotificationID) async {
    String url = "${ApiConsts.erpRest}NOTIFICATION_GET_RESPOND_ATTRIBUTES";
    Map<String, dynamic> postParams = {"P_NOTIFICATION_ID": pNotificationID};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.notificationGetRespondAttributesList ?? [];
    }, url, postParams);
  }

  Future<GenericResponseModel> getBasicDetNTFBody(int pNotificationID, int pTransactionID) async {
    String url = "${ApiConsts.erpRest}GET_BASIC_DET_NTF_BODY";
    Map<String, dynamic> postParams = {
      "P_NOTIFICATION_ID": pNotificationID,
      "P_TRANSACTION_ID": pTransactionID,
      "P_PAGE_LIMIT": 100,
      "P_PAGE_NUM": 1,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData;
    }, url, postParams);
  }

  Future<MemberInformationListModel> getUserInformation(int pSelectedResopID, String selectedEmployeeNumber) async {
    String url = "${ApiConsts.erpRest}Get_UserInformation";
    Map<String, dynamic> postParams = {
      "P_SELECTED_RESP_ID": pSelectedResopID,
      "P_PAGE_LIMIT": 100,
      "P_PAGE_NUM": 1,
    };
    postParams.addAll(AppState().postParamsJson);
    postParams["P_SELECTED_EMPLOYEE_NUMBER"] = selectedEmployeeNumber;
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.memberInformationList![0];
    }, url, postParams);
  }

  Future<List<GetStampMsNotificationBodyList>> getStampMsNotificationBody(int pNotificationID, int pTransactionID) async {
    String url = "${ApiConsts.erpRest}GET_STAMP_MS_NOTIFICATION_BODY";
    Map<String, dynamic> postParams = {
      "P_NOTIFICATION_ID": pNotificationID,
      "P_TRANSACTION_ID": pTransactionID,
      "P_PAGE_LIMIT": 100,
      "P_PAGE_NUM": 1,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.getStampMsNotificationBodyList ?? [];
    }, url, postParams);
  }

  Future<List<GetStampNsNotificationBodyList>> getStampNsNotificationBody(int pNotificationID, int pTransactionID) async {
    String url = "${ApiConsts.erpRest}GET_STAMP_NS_NOTIFICATION_BODY";
    Map<String, dynamic> postParams = {
      "P_NOTIFICATION_ID": pNotificationID,
      "P_TRANSACTION_ID": pTransactionID,
      "P_PAGE_LIMIT": 100,
      "P_PAGE_NUM": 1,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.getStampNsNotificationBodyList ?? [];
    }, url, postParams);
  }

  Future<List<GetAbsenceCollectionNotificationBodyList>> getAbsenceNotificationBody(int pNotificationID, int pTransactionID) async {
    String url = "${ApiConsts.erpRest}GET_ABSENCE_NOTIFICATION_BODY";
    Map<String, dynamic> postParams = {
      "P_NOTIFICATION_ID": pNotificationID,
      "P_TRANSACTION_ID": pTransactionID,
      "P_PAGE_LIMIT": 100,
      "P_PAGE_NUM": 1,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.getAbsenceCollectionNotificationBodyList ?? [];
    }, url, postParams);
  }

  Future<GenericResponseModel> postNotificationActions(Map<String, dynamic> postParams) async {
    String url = "${ApiConsts.erpRest}NOTIFICATION_ACTIONS";
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData;
    }, url, postParams);
  }

  Future<List<GetMoNotificationBodyList>> getMoNotificationBody(int pNotificationID, int pTransactionID) async {
    String url = "${ApiConsts.erpRest}GET_MO_NOTIFICATION_BODY";
    Map<String, dynamic> postParams = {
      "P_NOTIFICATION_ID": pNotificationID,
      "P_TRANSACTION_ID": pTransactionID,
      "P_PAGE_LIMIT": 100,
      "P_PAGE_NUM": 1,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.getMoNotificationBodyList ?? [];
    }, url, postParams);
  }

  Future<GetPoNotificationBodyList?> getPoNotificationBody(int pNotificationID, int pTransactionID) async {
    String url = "${ApiConsts.erpRest}GET_PO_NOTIFICATION_BODY";
    Map<String, dynamic> postParams = {
      "P_NOTIFICATION_ID": pNotificationID,
      "P_TRANSACTION_ID": pTransactionID,
      "P_PAGE_LIMIT": 100,
      "P_PAGE_NUM": 1,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.getPoNotificationBodyList;
    }, url, postParams);
  }

  Future<List<GetMoItemHistoryList>> getMoItemHistory(int pItemID, int pOrgID) async {
    String url = "${ApiConsts.erpRest}GET_MO_ITEM_HISTORY";
    Map<String, dynamic> postParams = {
      "P_ITEM_ID": pItemID,
      "P_PAGE_LIMIT": 100,
      "P_PAGE_NUM": 1,
      "P_ORG_ID": pOrgID,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.getMoItemHistoryList ?? [];
    }, url, postParams);
  }

  Future<List<GetPoItemHistoryList>> getPoItemHistory(int pItemID) async {
    String url = "${ApiConsts.erpRest}GET_PO_ITEM_HISTORY";
    Map<String, dynamic> postParams = {
      "P_ITEM_ID": pItemID,
      "P_PAGE_LIMIT": 100,
      "P_PAGE_NUM": 1,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.getPoItemHistoryList ?? [];
    }, url, postParams);
  }

  Future<List<GetQuotationAnalysisList>> getQuotationAnalysis(int pItemID, int pPoHeaderId) async {
    String url = "${ApiConsts.erpRest}GET_QUOTATION_ANALYSIS";
    Map<String, dynamic> postParams = {
      "P_ITEM_ID": pItemID,
      "P_PAGE_LIMIT": 100,
      "P_PAGE_NUM": 1,
      "P_PO_HEADER_ID": pPoHeaderId,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.getQuotationAnalysisList ?? [];
    }, url, postParams);
  }

  Future<GetItemCreationNtfBodyList?> getItemCreationNtfBody(int pNotificationID, int pTransactionID) async {
    String url = "${ApiConsts.erpRest}GET_ITEM_CREATION_NTF_BODY";
    Map<String, dynamic> postParams = {
      "P_NOTIFICATION_ID": pNotificationID,
      "P_TRANSACTION_ID": pTransactionID,
      "P_PAGE_LIMIT": 100,
      "P_PAGE_NUM": 1,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.getItemCreationNtfBodyList;
    }, url, postParams);
  }

  Future<ITGRequest?> getITGFormDetails(String requestType, int taskId, int itemId, String employeeNumber) async {
    String url = "${ApiConsts.cocRest}ITGGetFormDetials";
    Map<String, dynamic> postParams = {
      "RequestType": requestType,
      "TaskID": taskId,
      "ItemID": itemId,
      "EmployeeNumber": "15153",
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      ItgFormsModel responseData = ItgFormsModel.fromJson(json);
      return responseData.itgRequest;
    }, url, postParams);
  }
}
