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/submit_term_transaction_list_model.dart';
import 'package:mohem_flutter_app/models/termination/get_term_cols_structure_list_model.dart';
import 'package:mohem_flutter_app/models/termination/get_term_dff_structure_list_model.dart';
import 'package:mohem_flutter_app/start_term_approval_process_list_model.dart';

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

  TerminationDffApiClient._internal();

  factory TerminationDffApiClient() => _instance;

  Future<List<GetTermColsStructureList>?> getTermColsStructure() async {
    String url = "${ApiConsts.erpRest}GET_TERM_COLS_STRUCTURE";
    Map<String, dynamic> postParams = {};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel genericResponseModel = GenericResponseModel.fromJson(json);
      return genericResponseModel.getTermColsStructureList ?? [];
    }, url, postParams);
  }

  Future<List<GetTermDffStructureList>?> getTermDffStructure(String functionName, String? pDescFlexContextCode) async {
    String url = "${ApiConsts.erpRest}GET_TERM_DFF_STRUCTURE";
    Map<String, dynamic> postParams = {'P_FUNCTION_NAME': functionName, "P_DESC_FLEX_CONTEXT_CODE": pDescFlexContextCode};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel genericResponseModel = GenericResponseModel.fromJson(json);
      return genericResponseModel.getTermDffStructureList ?? [];
    }, url, postParams);
  }

  Future<SubmitTermTransactionList> submitTermTransaction(String functionName, List<Map<String, dynamic>> list) async {
    String url = "${ApiConsts.erpRest}SUBMIT_TERM_TRANSACTION";
    Map<String, dynamic> postParams = {
      "P_SELECTED_RESP_ID": -999,
      "P_MENU_TYPE": "E",
      "P_FUNCTION_NAME": functionName,
      "EITTransactionTBL": list,
    };
    postParams.addAll(AppState().postParamsJson);

    for (var abc in list) {
      print(abc);
    }
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel genericResponseModel = GenericResponseModel.fromJson(json);
      return genericResponseModel.submitTermTransactionList!;
    }, url, postParams);
  }

  Future<StartTermApprovalProcessList> startTermApprovalProcess(String action, String comments, String itemKey, int transactionId) async {
    String url = "${ApiConsts.erpRest}START_TERM_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 genericResponseModel = GenericResponseModel.fromJson(json);
      return genericResponseModel.startTermApprovalProcessList!;
    }, url, postParams);
  }
}
