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_announcement_details.dart';
import 'package:mohem_flutter_app/models/pending_transactions/get_pending_transactions_details.dart';
import 'package:mohem_flutter_app/models/pending_transactions/get_req_functions.dart';

// todo '@haroon' kindly format code

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

  PendingTransactionsApiClient._internal();

  factory PendingTransactionsApiClient() => _instance;

  Future<List<GetPendingTransactionsFunctions>> getPendingReqFunctions() async {
    String url = "${ApiConsts.erpRest}GET_PENDING_REQ_FUNCTIONS";
    Map<String, dynamic> postParams = {};

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

  Future<List<GetPendingTransactionsDetails>> getPendingTransactionsDetails(String functionID, String dateFrom, String dateTo) async {
    String url = "${ApiConsts.erpRest}GET_PENDING_REQ_DETAILS";
    Map<String, dynamic> postParams = {"P_FUNCTION_ID": functionID, "P_PAGE_LIMIT": 20, "P_PAGE_NUM": 1, "P_CREATION_DATE_FROM": dateFrom, "P_CREATION_DATE_TO": dateTo};

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

  Future<String> getAnnouncements(int itgAwarenessID, int itgPageNo, int itgRowID) async {
    String url = "${ApiConsts.cocRest}GetAnnouncementDiscountsConfigData";
    Map<String, dynamic> postParams = {"EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER.toString(), "ItgAwarenessID": itgAwarenessID, "ItgPageNo": itgPageNo, "ItgPageSize": 5, "ItgRowID": itgRowID};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      return responseData.mohemmITGResponseItem ?? "";
    }, url, postParams);
  }

  Future<GetAnnouncementDetails> getAnnouncementDetails(int itgAwarenessID, int itgPageNo, int itgRowID) async {
    String url = "${ApiConsts.cocRest}GetAnnouncementDiscountsConfigData";
    Map<String, dynamic> postParams = {"EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER.toString(), "ItgAwarenessID": itgAwarenessID, "ItgPageNo": itgPageNo, "ItgPageSize": 5, "ItgRowID": itgRowID};
    postParams.addAll(AppState().postParamsJson);
    return await ApiClient().postJsonForObject((json) {
      GenericResponseModel? responseData = GenericResponseModel.fromJson(json);
      var jsonDecodedData = jsonDecode(jsonDecode(responseData.mohemmITGResponseItem!)['result']['data']);
      return GetAnnouncementDetails.fromJson(jsonDecodedData[0]);
    }, url, postParams);
  }

}
