import 'dart:async';
import 'dart:convert';

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_contact_dff_structure_list.dart';
import 'package:mohem_flutter_app/models/get_eit_dff_structure_list_model.dart';
import 'package:mohem_flutter_app/models/get_employee_address_model.dart';
import 'package:mohem_flutter_app/models/get_employee_basic_details.model.dart';
import 'package:mohem_flutter_app/models/get_employee_contacts.model.dart';
import 'package:mohem_flutter_app/models/get_employee_phones_model.dart';
import 'package:mohem_flutter_app/models/performance.dart';
import 'package:mohem_flutter_app/models/profile/basic_details_dff_structure.dart';
import 'package:mohem_flutter_app/models/profile/get_contact_clos_structure_list.dart';
import 'package:mohem_flutter_app/models/profile/get_contact_details_list.dart';
import 'package:mohem_flutter_app/models/profile/get_countries_list_model.dart';
import 'package:mohem_flutter_app/models/profile/phone_number_types_model.dart';
import 'package:mohem_flutter_app/models/profile/start_address_approval_process_model.dart';
import 'package:mohem_flutter_app/models/profile/submit_address_transaction.dart';
import 'package:mohem_flutter_app/models/profile/submit_basic_details_transaction_model.dart';
import 'package:mohem_flutter_app/models/profile/submit_contact_transaction_list_model.dart';
import 'package:mohem_flutter_app/models/profile/submit_phone_transactions.dart';
import 'package:mohem_flutter_app/models/start_phone_approval_process_model.dart';

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

  ProfileApiClient._internal();

  factory ProfileApiClient() => _instance;

  Future<List<GetEmployeeContactsList>> getEmployeeContacts() async {
    String url = "${ApiConsts.erpRest}GET_EMPLOYEE_CONTACTS";
    Map<String, dynamic> postParams = {
      "P_MENU_TYPE": "E",
      "P_SELECTED_RESP_ID": -999,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      print(responseData);
      return responseData.getEmployeeContactsList ?? [];
    }, url, postParams);
  }

  Future<List<GetEmployeeBasicDetailsList>> getEmployeeBasicDetails() async {
    String url = "${ApiConsts.erpRest}GET_EMPLOYEE_BASIC_DETAILS";
    Map<String, dynamic> postParams = {
      "P_MENU_TYPE": "E",
      "P_SELECTED_RESP_ID": -999,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getEmployeeBasicDetailsList ?? [];
    }, url, postParams);
  }

  Future<List<GetEmployeePhonesList>> getEmployeePhones() async {
    String url = "${ApiConsts.erpRest}GET_EMPLOYEE_PHONES";
    Map<String, dynamic> postParams = {
      "P_MENU_TYPE": "E",
      "P_SELECTED_RESP_ID": -999,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getEmployeePhonesList ?? [];
    }, url, postParams);
  }

  Future<List<GetEmployeeAddressList>> getEmployeeAddress() async {
    String url = "${ApiConsts.erpRest}GET_EMPLOYEE_ADDRESS";
    Map<String, dynamic> postParams = {
      "P_MENU_TYPE": "E",
      "P_SELECTED_RESP_ID": -999,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getEmployeeAddressList ?? [];
    }, url, postParams);
  }

  Future updateEmpImage(img) async {
    String url = "${ApiConsts.erpRest}UPDATE_EMPLOYEE_IMAGE";
    Map<String, dynamic> postParams = {"P_MENU_TYPE": "E", "P_SELECTED_RESP_ID": -999, "P_IMAGE": img};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      // GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return json['UpdateEmployeeImageList'];
    }, url, postParams);
  }

  Future<GenericResponseModel> getDffStructure(String pFunctionName, String uRL, String requestType) async {
    String url = ApiConsts.erpRest + uRL;
    Map<String, dynamic> postParams = {"P_SELECTED_RESP_ID": -999, "P_MENU_TYPE": "E", "P_REQUEST_TYPE": pFunctionName, "P_COUNTRY_CODE": "SA"};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData;
    }, url, postParams);
  }

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

  Future<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!.first;
    }, url, postParams);
  }

  Future<List<GetPhoneNumberTypesModel>> getPhoneNumberTypes() async {
    String url = "${ApiConsts.erpRest}GET_OBJECT_VALUES";
    Map<String, dynamic> postParams = {"P_MENU_TYPE": "E", "P_SELECTED_RESP_ID": -999, "P_OBJECT_NAME": "PHONE_TYPE", "P_OBJECT_TYPE": "LOOKUP"};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getObjectValuesList ?? [];
    }, url, postParams);
  }

  Future<SubmitContactTransactionList?> submetConatctTransaction(
    String pFunctionName,
    String actionType,
    int contactRelationId,
    String date,
  ) async {
    String url = "${ApiConsts.erpRest}SUBMIT_CONTACT_TRANSACTION";
    Map<String, dynamic> postParams = {
      "P_MENU_TYPE": "E",
      "P_SELECTED_RESP_ID": -999,
      "P_FUNCTION_NAME": pFunctionName,
      "EITTransactionTBL": [
        {
          "TRANSACTION_NUMBER": 1,
          "NAME": "DATE_END",
          "VARCHAR2_VALUE": null,
          "NUMBER_VALUE": null,
          "DATE_VALUE": date,
        }
      ],
      "P_CONTACT_RELATIONSHIP_ID": contactRelationId,
      "P_ACTION": actionType,
      "PayrollCodeStr": "CS",
      "LegislationCodeStr": "SA",
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.submitContactTransactionList;
    }, url, postParams);
  }

  Future<GetApprovesList?> getApproves(String action, String comments, int transactionId, String itemKey) async {
    String url = "${ApiConsts.erpRest}GET_APPROVERS_LIST";
    Map<String, dynamic> postParams = {
      "P_AME_TRANSACTION_TYPE": "SSHRMS",
      "P_PAGE_LIMIT": 1000,
      "P_PAGE_NUM": 1,
      "P_TRANSACTION_ID": transactionId,
      "P_ACTION_MODE": action,
      "P_COMMENTS": comments,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      // return responseData.getApprovesList;
      return (responseData.getApprovesList?.length ?? 0) > 0 ? responseData.getApprovesList!.first : null;
    }, url, postParams);
  }

  Future<SubmitPhonesTransactionList> submitPhoneNumbers(List<GetEmployeePhonesList> empList) async {
    String url = "${ApiConsts.erpRest}SUBMIT_PHONES_TRANSACTION";
    Map<String, dynamic> postParams = {
      "P_MENU_TYPE": "E",
      "P_SELECTED_RESP_ID": -999,
      "P_FUNCTION_NAME": "HR_PERINFO_SS",
      "P_MBL_PHONES_TBL": empList.map((element) {
        return element.toJson();
      }).toList()
    };

    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return SubmitPhonesTransactionList.fromJson(responseData.submitPhonesTransactionList ?? {});
    }, url, postParams);
  }

  Future<StartPhoneApprovalProcess?> startPhoneApprovalProcess(String action, String comments, String itemKey, int transactionId) async {
    String url = "${ApiConsts.erpRest}START_PHONES_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.startPhonesApprovalProcessList;
    }, url, postParams);
  }

  Future<StartAddressApprovalProcess?> startAddressApprovalProcess(String action, String comments, String itemKey, int transactionId) async {
    String url = "${ApiConsts.erpRest}START_ADDRESS_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.startAddressApprovalProcessList;
    }, url, postParams);
  }

  Future<SubmitBasicDetailsTransactionList> submitBasicDetailsTransaction(String pDescFlexContextCode, String pFunctionName, List<Map<String, dynamic>> list, int correctOrNew) async {
    String url = "${ApiConsts.erpRest}SUBMIT_BASIC_DET_TRANSACTION";
    Map<String, dynamic> postParams = {
      "P_SELECTED_RESP_ID": -999,
      "P_MENU_TYPE": "E",
      "P_FUNCTION_NAME": pFunctionName,
      "P_ACTION_TYPE": correctOrNew == 1 ? 'CORRECT' : 'CHANGE',
      //"EITTransactionTBL": list,
      // "EITTransactionTBLModel": list,
    };
    postParams.addAll(AppState().postParamsJson);
    print("postParams:$postParams");
    postParams["EITTransactionTBL"] = list;
    postParams["EITTransactionTBLModel"] = list;
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.submitBasicDetTransactionList!; //ESERVICESDV.fromJson(responseData.getDefaultValueList!.toJson());
    }, url, postParams);
  }

  Future<StartAddressApprovalProcess?> startBasicDetailsApprovalProcess(String action, String comments, String itemKey, int transactionId) async {
    String url = "${ApiConsts.erpRest}START_BASIC_DET_APPR_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.startAddressApprovalProcessList;
    }, url, postParams);
  }

  Future<SubmitAddressTransaction> submitAddressTransaction(
      String pDescFlexContextCode, String pFunctionName, List<Map<String, dynamic>> list, int correctOrNew, String countryCode, String effectiveDate) async {
    String url = "${ApiConsts.erpRest}SUBMIT_ADDRESS_TRANSACTION";
    Map<String, dynamic> postParams = {
      "P_SELECTED_RESP_ID": -999,
      "P_MENU_TYPE": "E",
      "P_COUNTRY_CODE": countryCode, //"SA",
      "P_DESC_FLEX_CONTEXT_CODE": "SA",
      "P_FUNCTION_NAME": pFunctionName,
      "P_ACTION": correctOrNew == 1 ? 'CORRECT' : 'NEW',
      "P_EFFECTIVE_DATE": effectiveDate,

      //"EITTransactionTBL": list,
      // "EITTransactionTBLModel": list,
    };
    postParams.addAll(AppState().postParamsJson);
    print("postParams:$postParams");
    postParams["EITTransactionTBL"] = list;
    // postParams["EITTransactionTBLModel"] = list;
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel responseData = GenericResponseModel.fromJson(json);
      return responseData.submitAddressTransactionList!; //ESERVICESDV.fromJson(responseData.getDefaultValueList!.toJson());
    }, url, postParams);
  }

  Future<List<GetCountriesListModel>> getCountriesList() async {
    String url = "${ApiConsts.erpRest}GET_COUNTRIES";
    Map<String, dynamic> postParams = {
      "P_MENU_TYPE": "E",
      "P_SELECTED_RESP_ID": -999,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getCountriesList ?? [];
    }, url, postParams);
  }

  // add & update family member
  Future<List<GetBasicDetDffStructureList>> getBasicDetDffStructure() async {
    String url = "${ApiConsts.erpRest}GET_BASIC_DET_DFF_STRUCTURE";
    Map<String, dynamic> postParams = {"P_FUNCTION_NAME": "HR_PERINFO_SS", "P_MENU_TYPE": "E", "P_REQUEST_TYPE": "CONTACT", "P_SELECTED_RESP_ID": -999};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getBasicDetDffStructureList ?? [];
    }, url, postParams);
  }

  // add & update family member
  Future<List<GetContactColsStructureList>> getContactColsStructureList(String actionType) async {
    String url = "${ApiConsts.erpRest}GET_CONTACT_COLS_STRUCTURE";
    Map<String, dynamic> postParams = {
      "P_ACTION": actionType,
      "P_FUNCTION_NAME": "HR_PERINFO_SS",
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getContactColsStructureList ?? [];
    }, url, postParams);
  }

  // update family member
  Future<List<GetContactDetailsList>> getContactDetailsList(int contactRelationId) async {
    String url = "${ApiConsts.erpRest}GET_CONTACT_DETAILS";
    Map<String, dynamic> postParams = {
      "P_MENU_TYPE": "E",
      "P_SELECTED_RESP_ID": -999,
      "P_CONTACT_RELATIONSHIP_ID": contactRelationId,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getContactDetailsList ?? [];
    }, url, postParams);
  }

  // add & update family member
  Future<List<GetContactDffStructureList>> getContactDffStructureList() async {
    String url = "${ApiConsts.erpRest}GET_CONTACT_DFF_STRUCTURE";
    Map<String, dynamic> postParams = {
      "P_DESC_FLEX_CONTEXT_CODE": "HMG_ADD_CONTACT_DETAILS",
      "P_FUNCTION_NAME": "HR_PERINFO_SS",
      "P_MENU_TYPE": "E",
      "P_SELECTED_RESP_ID": -999,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getContactDffStructureList ?? [];
    }, url, postParams);
  }

  Future<SubmitContactTransactionList?> submitContactTransactionAddAndUpdate(String actionType, relationId, List<Map<String, dynamic>> list) async {
    String url = "${ApiConsts.erpRest}SUBMIT_CONTACT_TRANSACTION";
    Map<String, dynamic> postParams = {
      "P_MENU_TYPE": "E",
      "P_SELECTED_RESP_ID": -999,
      "P_FUNCTION_NAME": "HR_PERINFO_SS",
      "P_ACTION": actionType,
      "P_CONTACT_RELATIONSHIP_ID": relationId,
    };

    postParams.addAll(AppState().postParamsJson);
    print("postParam:${json.encode(postParams)}");
    postParams["EITTransactionTBL"] = list;
    list.forEach((element) { 
      print(json.encode(element));
      
    });
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.submitContactTransactionList;
    }, url, postParams);
  }

  Future<List<GetPerformanceAppraisalList>> getPerformanceAppraisal() async {
    String url = "${ApiConsts.erpRest}GET_Performance_Appraisal";
    Map<String, dynamic> postParams = {};

    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getPerformanceAppraisalList ?? [];
    }, url, postParams);
  }
}
