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/leave_balance/calculate_absence_duration_model.dart';
import 'package:mohem_flutter_app/models/leave_balance/cancel_hr_transaction_list_model.dart';
import 'package:mohem_flutter_app/models/leave_balance/get_absence_attendance_types_list_model.dart';
import 'package:mohem_flutter_app/models/leave_balance/get_absence_dff_structure_list_model.dart';
import 'package:mohem_flutter_app/models/leave_balance/get_absence_transaction_list_model.dart';
import 'package:mohem_flutter_app/models/leave_balance/start_absence_approval_proccess_model.dart';
import 'package:mohem_flutter_app/models/leave_balance/sumbit_absence_transaction_list_model.dart';

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

  LeaveBalanceApiClient._internal();

  factory LeaveBalanceApiClient() => _instance;

  Future<List<GetAbsenceTransactionList>> getAbsenceTransactions(int pSelectedResopID, String? empID) async {
    String url = "${ApiConsts.erpRest}GET_ABSENCE_TRANSACTIONS";
    Map<String, dynamic> postParams = {"P_PAGE_LIMIT": 50, "P_PAGE_NUM": 1, "P_MENU_TYPE": "E", "P_SELECTED_RESP_ID": pSelectedResopID};
    postParams.addAll(AppState().postParamsJson);
    if (empID != null) postParams["P_SELECTED_EMPLOYEE_NUMBER"] = empID;
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getAbsenceTransactionList ?? [];
    }, url, postParams);
  }

  Future<List<GetAbsenceAttendanceTypesList>> getAbsenceAttendanceTypes({String? empID}) async {
    String url = "${ApiConsts.erpRest}GET_ABSENCE_ATTENDANCE_TYPES";
    Map<String, dynamic> postParams = {};
    postParams.addAll(AppState().postParamsJson);
    if (empID != null) postParams["P_SELECTED_EMPLOYEE_NUMBER"] = empID;

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

  Future<CalculateAbsenceDuration> calculateAbsenceDuration(int pAbsenceAttendanceTypeID, String pDateStart, String pDateEnd, int pSelectedResopID, {String? empID}) async {
    String url = "${ApiConsts.erpRest}CALCULATE_ABSENCE_DURATION";
    Map<String, dynamic> postParams = {
      "P_ABSENCE_ATTENDANCE_TYPE_ID": pAbsenceAttendanceTypeID,
      "P_DATE_START": pDateStart,
      "P_DATE_END": pDateEnd,
      "P_SELECTED_RESP_ID": pSelectedResopID,
      "P_MENU_TYPE": "E",
      "P_TIME_END": null,
      "P_TIME_START": null
    };
    postParams.addAll(AppState().postParamsJson);
    if (empID != null) postParams["P_SELECTED_EMPLOYEE_NUMBER"] = empID;
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.calculateAbsenceDuration!;
    }, url, postParams);
  }

  Future<List<GetAbsenceDffStructureList>> getAbsenceDffStructure(String pDescFlexContextCode, String pFunctionName, int pSelectedResopID, {String? empID}) async {
    String url = "${ApiConsts.erpRest}GET_ABSENCE_DFF_STRUCTURE";
    Map<String, dynamic> postParams = {
      "P_DESC_FLEX_CONTEXT_CODE": pDescFlexContextCode,
      "P_FUNCTION_NAME": pFunctionName,
      "P_MENU_TYPE": "E",
      "P_SELECTED_RESP_ID": pSelectedResopID,
    };
    postParams.addAll(AppState().postParamsJson);
    if (empID != null) postParams["P_SELECTED_EMPLOYEE_NUMBER"] = empID;
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.getAbsenceDffStructureList ?? [];
    }, url, postParams);
  }

  Future<GenericResponseModel> validateAbsenceTransaction(
      String pDescFlexContextCode, String pFunctionName, int pAbsenceAttendanceTypeID, String pReplacementUserName, String pDateStart, String pDateEnd, int pSelectedResopID, Map<String, String?> data,
      {String comments = "", String? empID}) async {
    String url = "${ApiConsts.erpRest}VALIDATE_ABSENCE_TRANSACTION";
    Map<String, dynamic> postParams = {
      "P_DESC_FLEX_CONTEXT_CODE": pDescFlexContextCode,
      "P_FUNCTION_NAME": pFunctionName,
      "P_REPLACEMENT_USER_NAME": pReplacementUserName,
      "P_ABSENCE_ACTION": "CREATE",
      "P_ABSENCE_COMMENTS": comments,
      "P_ABSENCE_ATTENDANCE_ID": pAbsenceAttendanceTypeID,
      "P_ABSENCE_ATTENDANCE_TYPE_ID": pAbsenceAttendanceTypeID,
      "P_DATE_START": pDateStart,
      "P_DATE_END": pDateEnd, //"29-Sep-2022",
      "P_SELECTED_RESP_ID": pSelectedResopID,
      "P_MENU_TYPE": "E",
      "P_TIME_END": null,
      "P_TIME_START": null
    };
    postParams.addAll(data);
    postParams.addAll(AppState().postParamsJson);
    if (empID != null) postParams["P_SELECTED_EMPLOYEE_NUMBER"] = empID;
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData;
    }, url, postParams);
  }

  Future<SumbitAbsenceTransactionList> submitAbsenceTransaction(
      String pDescFlexContextCode, String pFunctionName, int pAbsenceAttendanceTypeID, String pReplacementUserName, String pDateStart, String pDateEnd, int pSelectedResopID, Map<String, String?> data,
      {String comments = "", String? empID}) async {
    String url = "${ApiConsts.erpRest}SUBMIT_ABSENCE_TRANSACTION";
    Map<String, dynamic> postParams = {
      "P_DESC_FLEX_CONTEXT_CODE": pDescFlexContextCode,
      "P_FUNCTION_NAME": pFunctionName,
      "P_REPLACEMENT_USER_NAME": pReplacementUserName,
      "P_ABSENCE_ACTION": "CREATE",
      "P_ABSENCE_COMMENTS": comments,
      "P_ABSENCE_ATTENDANCE_ID": pAbsenceAttendanceTypeID,
      "P_ABSENCE_ATTENDANCE_TYPE_ID": pAbsenceAttendanceTypeID,
      "P_DATE_START": pDateStart,
      "P_DATE_END": pDateEnd, //"29-Sep-2022",
      "P_SELECTED_RESP_ID": pSelectedResopID,
      "P_MENU_TYPE": "E",
      "P_TIME_END": null,
      "P_TIME_START": null
    };
    postParams.addAll(data);
    postParams.addAll(AppState().postParamsJson);
    if (empID != null) postParams["P_SELECTED_EMPLOYEE_NUMBER"] = empID;
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.sumbitAbsenceTransactionList!;
    }, url, postParams);
  }

  Future<CancelHRTransactionLIst> cancelHrTransaction(int pTransactionID) async {
    String url = "${ApiConsts.erpRest}CANCEL_HR_TRANSACTION";
    Map<String, dynamic> postParams = {"P_TRANSACTION_ID": pTransactionID};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.cancelHRTransactionLIst!;
    }, url, postParams);
  }

  Future<StartAbsenceApprovalProccess> startAbsenceApprovalProcess(int pTransactionID, String comments, int pSelectedResopID, {String? empID}) async {
    String url = "${ApiConsts.erpRest}START_ABSENCE_APPROVAL_PROCESS";
    Map<String, dynamic> postParams = {"P_TRANSACTION_ID": pTransactionID, "P_SELECTED_RESP_ID": pSelectedResopID, "P_COMMENTS": comments, "P_MENU_TYPE": "E"};
    postParams.addAll(AppState().postParamsJson);
    if (empID != null) postParams["P_SELECTED_EMPLOYEE_NUMBER"] = empID;
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.startAbsenceApprovalProccess!;
    }, url, postParams);
  }
}
