import 'applepay/apple_pay.dart';
import 'applepay/apple_pay_info.dart';
import 'code/agile_pay_code.dart';
import 'listener/agile_pay_state.dart';
import 'listener/i_agile_pay.dart';

class AgilePay {
  static IAgilePay? realPay;

  static void check() {
    IAgilePay? iAgilePay;
    iAgilePay = ApplePay();
    realPay = iAgilePay;
    iAgilePay.check();
  }

  static void restore(
      {required AgilePaySuccess success,
      required AgilePayError payError,
      required AgileError error,
        AgileRestoreSuccess? restore,
      AgilePayBefore? before}) {
    IAgilePay? iAgilePay;
    iAgilePay = ApplePay();
    realPay = iAgilePay;

    iAgilePay.setPayListener(AgilePayStateImpl(
        paySuccessListener: success,
        payErrorListener: payError,
        errorListener: error,
        restoreSuccessListener: restore,
        payBeforeListener: before));
    iAgilePay.restore();
  }

  static void startPay(dynamic payInfo,
      {required AgilePaySuccess success,
      required AgilePayError payError,
      required AgileError error,
      AgilePayBefore? before}) {
    IAgilePay? iAgilePay;

    if (payInfo is ApplePayInfo) {
      iAgilePay = ApplePay(payInfo);
    }

    realPay = iAgilePay;
    if (iAgilePay != null) {
      iAgilePay.setPayListener(AgilePayStateImpl(
          paySuccessListener: success,
          payErrorListener: payError,
          errorListener: error,
          payBeforeListener: before));
      iAgilePay.pay();
    } else {
      payError(AgilePayCode.payCodeNotSupport,
          AgilePayCode.getMessageByCode(AgilePayCode.payCodeNotSupport));
    }
  }
}
