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/generic_response_model.dart';
import 'package:mohem_flutter_app/models/get_approves_list_model.dart';
import 'package:mohem_flutter_app/models/get_eit_dff_structure_list_model.dart';
import 'package:mohem_flutter_app/models/get_eit_transaction_list_model.dart';
import 'package:mohem_flutter_app/models/start_eit_approval_process_model.dart';
import 'package:mohem_flutter_app/models/submit_eit_transaction_list_model.dart';
import 'package:mohem_flutter_app/models/worklist/resubmit_eit_response_model.dart';

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

  MyAttendanceApiClient._internal();

  factory MyAttendanceApiClient() => _instance;

  Future<List<GetEITTransactionList>?> getEitTransaction(String pFunctionName) async {
    String url = "${ApiConsts.erpRest}GET_EIT_TRANSACTIONS";
    Map<String, dynamic> postParams = {"P_PAGE_LIMIT": 50, "P_PAGE_NUM": 1, "P_SELECTED_RESP_ID": -999, "P_MENU_TYPE": "E", "P_FUNCTION_NAME": pFunctionName};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getEITTransactionList ?? [];
    }, url, postParams);
  }

  Future<GenericResponseModel> getEitDffStructure(String pFunctionName) async {
    String url = "${ApiConsts.erpRest}GET_EIT_DFF_STRUCTURE";
    Map<String, dynamic> postParams = {"P_SELECTED_RESP_ID": -999, "P_MENU_TYPE": "E", "P_FUNCTION_NAME": pFunctionName};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData;
    }, url, postParams);
  }

  Future<List<ESERVICESVS>> getValueSetValues(String pSegmentName, String pDescFlexContextCode, String pDescFlexName, List<Map<String, dynamic>> list) async {
    String url = "${ApiConsts.erpRest}GET_VALUE_SET_VALUES";
    Map<String, dynamic> postParams = {
      "P_SELECTED_RESP_ID": -999,
      "P_MENU_TYPE": "E",
      "P_PAGE_LIMIT": 1000,
      "P_PAGE_NUM": 1,
      "P_PARENT_VALUE": null,
      "P_SEGMENT_NAME": pSegmentName,
      "P_DESC_FLEX_CONTEXT_CODE": pDescFlexContextCode,
      "P_DESC_FLEX_NAME": pDescFlexName,
      "GetValueSetValuesTBL": list,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getValueSetValuesList ?? [];
    }, url, postParams);
  }

  Future<ESERVICESDV> getDefaultValue(String pSegmentName, String pDescFlexContextCode, String pDescFlexName, List<Map<String, dynamic>> list) async {
    String url = "${ApiConsts.erpRest}GET_DEFAULT_VALUE";
    Map<String, dynamic> postParams = {
      "P_SELECTED_RESP_ID": -999,
      "P_MENU_TYPE": "E",
      "P_PARENT_VALUE": null,
      "P_SEGMENT_NAME": pSegmentName,
      "P_DESC_FLEX_CONTEXT_CODE": pDescFlexContextCode,
      "P_DESC_FLEX_NAME": pDescFlexName,
      "GetValueSetValuesTBL": list,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return ESERVICESDV.fromJson(responseData.getDefaultValueList!.toJson());
    }, url, postParams);
  }

  Future<GenericResponseModel?> validateEitTransaction(String pDescFlexContextCode, String pFunctionName, List<Map<String, dynamic>> list) async {
    String url = "${ApiConsts.erpRest}VALIDATE_EIT_TRANSACTION";
    Map<String, dynamic> postParams = {
      "P_SELECTED_RESP_ID": -999,
      "P_MENU_TYPE": "E",
      "P_DESC_FLEX_CONTEXT_CODE": pDescFlexContextCode,
      "P_FUNCTION_NAME": pFunctionName,
      "EITTransactionTBL": list,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData; //ESERVICESDV.fromJson(responseData.getDefaultValueList!.toJson());
    }, url, postParams);
  }

  Future<SubmitEITTransactionList> submitEitTransaction(String pDescFlexContextCode, String pFunctionName, List<Map<String, dynamic>> list) async {
    String url = "${ApiConsts.erpRest}SUBMIT_EIT_TRANSACTION";
    Map<String, dynamic> postParams = {
      "P_SELECTED_RESP_ID": -999,
      "P_MENU_TYPE": "E",
      "P_DESC_FLEX_CONTEXT_CODE": pDescFlexContextCode,
      "P_FUNCTION_NAME": pFunctionName,
      "EITTransactionTBL": list,
      "EITTransactionTBLModel": list,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.submitEITTransactionList!; //ESERVICESDV.fromJson(responseData.getDefaultValueList!.toJson());
    }, url, postParams);
  }

  Future<ResubmitEITRequestResponse> reSubmitEitTransaction(String itemKey, var notifID, List<Map<String, dynamic>> list) async {
    String url = "${ApiConsts.erpRest}RESUBMIT_EIT_TRANSACTION";
    Map<String, dynamic> postParams = {
      "P_NOTIFICATION_ID": notifID,
      "P_ITEM_KEY": itemKey,
      "P_EMAIL_ADDRESS": AppState().memberInformationList!.eMPLOYEEEMAILADDRESS,
      "EITTransactionTBL": list
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.resubmitEITTransactionList!; //ESERVICESDV.fromJson(responseData.getDefaultValueList!.toJson());
    }, url, postParams);
  }

  Future<List<GetApprovesList>> getApproversList(String pAmeTransactionType, int pTransactionId) async {
    String url = "${ApiConsts.erpRest}GET_APPROVERS_LIST";
    Map<String, dynamic> postParams = {
      "P_AME_TRANSACTION_TYPE": pAmeTransactionType,
      "P_PAGE_LIMIT": 1000,
      "P_PAGE_NUM": 1,
      "P_TRANSACTION_ID": pTransactionId,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getApprovesList ?? [];
    }, url, postParams);
  }

  Future<List<GetApprovesList>> addAttachment(List<Map<String, dynamic>> list) async {
    String url = "${ApiConsts.erpRest}ADD_ATTACHMENT";
    Map<String, dynamic> postParams = {"AddAttachmentList": list};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getApprovesList ?? [];
    }, url, postParams);
  }

  Future<StartEitApprovalProcess?> startEitApprovalProcess(String action, String comments, String itemKey, int transactionId) async {
    String url = "${ApiConsts.erpRest}START_EIT_APPROVAL_PROCESS";
    Map<String, dynamic> postParams = {
      "P_SELECTED_RESP_ID": -999,
      "P_MENU_TYPE": "E",
      "P_ACTION_MODE": action,
      "P_COMMENTS": comments,
      "P_ITEM_KEY": itemKey,
      "P_TRANSACTION_ID": transactionId,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.startEitApprovalProcess;
    }, url, postParams);
  }
}
