import 'package:mc_common_app/api/api_client.dart';
import 'package:mc_common_app/classes/app_state.dart';
import 'package:mc_common_app/classes/consts.dart';
import 'package:mc_common_app/config/dependency_injection.dart';
import 'package:mc_common_app/extensions/string_extensions.dart';
import 'package:mc_common_app/models/general_models/generic_resp_model.dart';
import 'package:mc_common_app/models/payment_models/pay_order_detail_resp_model.dart';
import 'package:mc_common_app/utils/enums.dart';

abstract class PaymentsRepo {
  Future<bool> verifyPayment({required int paymentTypeId, required int id, bool isForAppointments = false, List<int>? appointmentIds});

  Future<PayOrderDetailRespModel> getPayOrderDetails({required int paymentTypeId, required int adId});
}

class PaymentsRepoImp implements PaymentsRepo {
  ApiClient apiClient = injector.get<ApiClient>();
  AppState appState = injector.get<AppState>();

  @override
  Future<PayOrderDetailRespModel> getPayOrderDetails({required int paymentTypeId, required int adId}) async {
    var queryParameters = {
      "AdsID": adId.toString(),
      "PaymentType": paymentTypeId.toString(),
    };

    GenericRespModel adsGenericModel = await apiClient.getJsonForObject(
      token: appState.getUser.data!.accessToken,
      queryParameters: queryParameters,
      (json) => GenericRespModel.fromJson(json),
      ApiConsts.payForOrderDetailGet,
    );
    PayOrderDetailRespModel payOrderDetailRespModel = PayOrderDetailRespModel.fromJson(adsGenericModel.data);
    return payOrderDetailRespModel;
  }

  String getKeyByPaymentType({required int paymentType}) {
    PaymentTypes paymentTypeEnum = paymentType.getPaymentTypeFromId();
    switch (paymentTypeEnum) {
      case PaymentTypes.subscription:
        return "SubscriptionAppliedID";
      case PaymentTypes.appointment:
      case PaymentTypes.partialAppointment:
        return "appointmentIds";
      case PaymentTypes.ads:
      case PaymentTypes.adReserve:
      case PaymentTypes.extendAds:
        return "AdsID";
      case PaymentTypes.request:
        return "RequestID";
    }
  }

  @override
  Future<bool> verifyPayment({required int paymentTypeId, required int id, bool isForAppointments = false, List<int>? appointmentIds}) async {
    Map<String, dynamic> queryParameters = {
      "ID": id.toString(),
      "PaymentType": paymentTypeId.toString(),
    };

    if (isForAppointments) {
      List<String> ids = [];
      if (appointmentIds != null) {
        for (var element in appointmentIds) {
          ids.add(element.toString());
        }
      }
      queryParameters.removeWhere((key, value) => key == "ID");
      queryParameters.addAll({"IDs": ids});
    }

    GenericRespModel adsGenericModel = await apiClient.getJsonForObject(
      token: appState.getUser.data!.accessToken,
      queryParameters: queryParameters,
      (json) => GenericRespModel.fromJson(json),
      ApiConsts.payForOrderIsPaidGet,
    );
    if (adsGenericModel.data == null) {
      return false;
    }
    bool isPaid = adsGenericModel.data['isPaid'] ?? false;
    return isPaid;
  }
}
