import 'dart:async';

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_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/profile/phone_number_types_modek.dart';
import 'api_client.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);
      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_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<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,
    };
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getObjectValuesList ?? [];
    }, url, postParams);
  }
}
