#import "RTNPGW.h"
#import <React/RCTUtils.h>

/**
 * Created by DavidBilly PK on 4/9/24.
 */

#if __has_include("PGWReactNative-Swift.h")
#import <PGWReactNative-Swift.h>
#else
#import <PGWReactNative/PGWReactNative-Swift.h>
#endif

@implementation RTNPGW

RCT_EXPORT_MODULE(RTNPGW)

//PGW
RCT_REMAP_METHOD(initialize, request:(NSString *)request initializeResolver:(RCTPromiseResolveBlock)resolve initializeRejecter:(RCTPromiseRejectBlock)reject) {

    return [self initialize:request resolve:resolve reject:reject];
}

- (void)initialize:(NSString *)request resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKReactNative initializeWithRequest:request resolve:resolve];
}

RCT_REMAP_METHOD(clientId, clientIdResolver:(RCTPromiseResolveBlock)resolve clientIdRejecter:(RCTPromiseRejectBlock)reject) {

    return [self clientId:resolve reject:reject];
}

- (void)clientId:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKReactNative clientIdWithResolve:resolve];
}

RCT_REMAP_METHOD(configuration, configurationResolver:(RCTPromiseResolveBlock)resolve configurationRejecter:(RCTPromiseRejectBlock)reject) {

    return [self configuration:resolve reject:reject];
}

- (void)configuration:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKReactNative configurationWithResolve:resolve];
}

RCT_REMAP_METHOD(paymentOption, request:(NSString *)request paymentOptionResolver:(RCTPromiseResolveBlock)resolve paymentOptionRejecter:(RCTPromiseRejectBlock)reject) {

    return [self paymentOption:request resolve:resolve reject:reject];
}

- (void)paymentOption:(NSString *)request resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKReactNative paymentOptionWithRequest:request resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(paymentOptionDetail, request:(NSString *)request paymentOptionDetailResolver:(RCTPromiseResolveBlock)resolve paymentOptionDetailRejecter:(RCTPromiseRejectBlock)reject) {

    return [self paymentOptionDetail:request resolve:resolve reject:reject];
}

- (void)paymentOptionDetail:(NSString *)request resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKReactNative paymentOptionDetailWithRequest:request resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(proceedTransaction, request:(NSString *)request proceedTransactionResolver:(RCTPromiseResolveBlock)resolve proceedTransactionRejecter:(RCTPromiseRejectBlock)reject) {

    return [self proceedTransaction:request resolve:resolve reject:reject];
}

- (void)proceedTransaction:(NSString *)request resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKReactNative proceedTransactionWithRequest:request resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(customerTokenInfo, request:(NSString *)request customerTokenInfoResolver:(RCTPromiseResolveBlock)resolve customerTokenInfoRejecter:(RCTPromiseRejectBlock)reject) {

    return [self customerTokenInfo:request resolve:resolve reject:reject];
}

- (void)customerTokenInfo:(NSString *)request resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKReactNative customerTokenInfoWithRequest:request resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(exchangeRate, request:(NSString *)request exchangeRateResolver:(RCTPromiseResolveBlock)resolve exchangeRateRejecter:(RCTPromiseRejectBlock)reject) {

    return [self exchangeRate:request resolve:resolve reject:reject];
}

- (void)exchangeRate:(NSString *)request resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKReactNative exchangeRateWithRequest:request resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(userPreference, request:(NSString *)request userPreferenceResolver:(RCTPromiseResolveBlock)resolve userPreferenceRejecter:(RCTPromiseRejectBlock)reject) {

    return [self userPreference:request resolve:resolve reject:reject];
}

- (void)userPreference:(NSString *)request resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKReactNative userPreferenceWithRequest:request resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(transactionStatus, request:(NSString *)request transactionStatusResolver:(RCTPromiseResolveBlock)resolve transactionStatusRejecter:(RCTPromiseRejectBlock)reject) {

    return [self transactionStatus:request resolve:resolve reject:reject];
}

- (void)transactionStatus:(NSString *)request resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKReactNative transactionStatusWithRequest:request resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(systemInitialization, request:(NSString *)request systemInitializationResolver:(RCTPromiseResolveBlock)resolve systemInitializationRejecter:(RCTPromiseRejectBlock)reject) {

    return [self systemInitialization:request resolve:resolve reject:reject];
}

- (void)systemInitialization:(NSString *)request resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKReactNative systemInitializationWithRequest:request resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(paymentNotification, request:(NSString *)request paymentNotificationResolver:(RCTPromiseResolveBlock)resolve paymentNotificationRejecter:(RCTPromiseRejectBlock)reject) {

    return [self paymentNotification:request resolve:resolve reject:reject];
}

- (void)paymentNotification:(NSString *)request resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKReactNative paymentNotificationWithRequest:request resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(cancelTransaction, request:(NSString *)request cancelTransactionResolver:(RCTPromiseResolveBlock)resolve cancelTransactionRejecter:(RCTPromiseRejectBlock)reject) {

    return [self cancelTransaction:request resolve:resolve reject:reject];
}

- (void)cancelTransaction:(NSString *)request resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKReactNative cancelTransactionWithRequest:request resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(loyaltyPointInfo, request:(NSString *)request loyaltyPointInfoResolver:(RCTPromiseResolveBlock)resolve loyaltyPointInfoRejecter:(RCTPromiseRejectBlock)reject) {

    return [self loyaltyPointInfo:request resolve:resolve reject:reject];
}

- (void)loyaltyPointInfo:(NSString *)request resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKReactNative loyaltyPointInfoWithRequest:request resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(paymentUI, request:(NSString *)request paymentUIResolver:(RCTPromiseResolveBlock)resolve paymentUIRejecter:(RCTPromiseRejectBlock)reject) {

    return [self paymentUI:request resolve:resolve reject:reject];
}

- (void)paymentUI:(NSString *)request resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    dispatch_async(dispatch_get_main_queue(), ^{

        UIViewController *uiViewController = RCTPresentedViewController();

        [PGWSDKReactNative paymentUIWithViewController:uiViewController request:request resolve:resolve reject:reject];
    });
}

//PGWHelper
RCT_REMAP_METHOD(supportApplePayPayment, request:(NSString *)request supportApplePayPaymentResolver:(RCTPromiseResolveBlock)resolve supportApplePayPaymentRejecter:(RCTPromiseRejectBlock)reject) {

    return [self supportApplePayPayment:request resolve:resolve reject:reject];
}

- (void)supportApplePayPayment:(NSString *)request resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKHelperReactNative supportApplePayPaymentWithRequest:request resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(proceedApplePayPayment, request:(NSString *)request fx:(NSString *)fx proceedApplePayPaymentResolver:(RCTPromiseResolveBlock)resolve proceedApplePayPaymentRejecter:(RCTPromiseRejectBlock)reject) {

    return [self proceedApplePayPayment:request fx:fx resolve:resolve reject:reject];
}

- (void)proceedApplePayPayment:(NSString *)request fx:(NSString *)fx resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKHelperReactNative proceedApplePayPaymentWithRequest:request fx:fx resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(supportGooglePayPayment, request:(NSString *)request googlePayAPIEnvironment:(NSInteger)googlePayAPIEnvironment supportGooglePayPaymentResolver:(RCTPromiseResolveBlock)resolve supportGooglePayPaymentRejecter:(RCTPromiseRejectBlock)reject) {

    return [self supportGooglePayPayment:request googlePayAPIEnvironment:googlePayAPIEnvironment resolve:resolve reject:reject];
}

- (void)supportGooglePayPayment:(NSString *)request googlePayAPIEnvironment:(double)googlePayAPIEnvironment resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKHelperReactNative supportGooglePayPaymentWithRequest:request googlePayAPIEnvironment:googlePayAPIEnvironment resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(proceedGooglePayPayment, request:(NSString *)request fx:(NSString *)fx googlePayAPIEnvironment:(NSInteger)googlePayAPIEnvironment proceedGooglePayPaymentResolver:(RCTPromiseResolveBlock)resolve proceedGooglePayPaymentRejecter:(RCTPromiseRejectBlock)reject) {

    return [self proceedGooglePayPayment:request fx:fx googlePayAPIEnvironment:googlePayAPIEnvironment resolve:resolve reject:reject];
}

- (void)proceedGooglePayPayment:(NSString *)request fx:(NSString *)fx googlePayAPIEnvironment:(double)googlePayAPIEnvironment resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKHelperReactNative proceedGooglePayPaymentWithRequest:request fx:fx googlePayAPIEnvironment:googlePayAPIEnvironment resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(proceedDeepLinkPayment, request:(NSString *)request proceedDeepLinkPaymentResolver:(RCTPromiseResolveBlock)resolve proceedDeepLinkPaymentRejecter:(RCTPromiseRejectBlock)reject) {

    return [self proceedDeepLinkPayment:request resolve:resolve reject:reject];
}

- (void)proceedDeepLinkPayment:(NSString *)request resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKHelperReactNative proceedDeepLinkPaymentWithRequest:request resolve:resolve reject:reject];
}

RCT_REMAP_METHOD(proceedZaloPayPayment, request:(NSString *)request zaloPayAPIEnvironment:(NSString *)zaloPayAPIEnvironment proceedZaloPayPaymentResolver:(RCTPromiseResolveBlock)resolve proceedZaloPayPaymentRejecter:(RCTPromiseRejectBlock)reject) {

    return [self proceedZaloPayPayment:request zaloPayAPIEnvironment:zaloPayAPIEnvironment resolve:resolve reject:reject];
}

- (void)proceedZaloPayPayment:(NSString *)request zaloPayAPIEnvironment:(NSString *)zaloPayAPIEnvironment resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {

    [PGWSDKHelperReactNative proceedZaloPayPaymentWithRequest:request zaloPayAPIEnvironment:zaloPayAPIEnvironment resolve:resolve reject:reject];
}

#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule: (const facebook::react::ObjCTurboModule::InitParams &)params {

    return std::make_shared<facebook::react::NativePGWSDKSpecJSI>(params);
}

#endif

@end
