//
//  TrustDevice.m
//  TrustDeviceReactNative
//
//  Created by LEE on 7/12/24.
//

#import "TrustDevice.h"
#import <React/RCTLog.h>
#import <React/RCTConvert.h>
#import <TDMobRisk/TDMobRisk.h>

@implementation TrustDevice

RCT_EXPORT_MODULE();


RCT_REMAP_METHOD(getSDKVersion,
                 getSDKVersionResolver:(RCTPromiseResolveBlock)resolve
                 rejecter:(RCTPromiseRejectBlock)reject)
{
  TDMobRiskManager_t *manager = [TDMobRiskManager sharedManager];
  NSString* version = manager->getSDKVersion();
  resolve(version);
}

RCT_EXPORT_METHOD(initWithOptions:(NSDictionary*)options)
{
  NSMutableDictionary* mutableOptions = [options mutableCopy];
  mutableOptions[@"allowd"] = options[@"debug"];
  TDMobRiskManager_t *manager = [TDMobRiskManager sharedManager];
  manager->initWithOptions([mutableOptions copy]);
}

RCT_REMAP_METHOD(getBlackBox,
                 getBlackBoxResolver:(RCTPromiseResolveBlock)resolve
                 rejecter:(RCTPromiseRejectBlock)reject)
{
  TDMobRiskManager_t *manager = [TDMobRiskManager sharedManager];
  NSString* blackBox = manager->getBlackBox();
  resolve(blackBox);
}

RCT_REMAP_METHOD(getBlackBoxAsync,
                 getBlackBoxAsyncResolver:(RCTPromiseResolveBlock)resolve
                 rejecter:(RCTPromiseRejectBlock)reject)
{
  TDMobRiskManager_t *manager = [TDMobRiskManager sharedManager];
  manager->getBlackBoxAsync(^(NSString* blackBox){
    resolve(blackBox);
  });
}

- (UIWindow *)getKeyWindow
{
    if (@available(iOS 13.0, *))
        {
        for (UIWindowScene* windowScene in [UIApplication sharedApplication].connectedScenes) {
            if (windowScene.activationState == UISceneActivationStateForegroundActive)
                {
                for (UIWindow *window in windowScene.windows)
                    {
                    if (window.isKeyWindow)
                        {
                        return window;
                        }
                    }
                }
        }
        }
    else
        {
        return [UIApplication sharedApplication].keyWindow;
        }
    return nil;
}

RCT_REMAP_METHOD(showCaptcha,
                 showCaptchaResolver:(RCTPromiseResolveBlock)resolve
                 rejecter:(RCTPromiseRejectBlock)reject)
{
  TDMobRiskManager_t *manager = [TDMobRiskManager sharedManager];
  
  dispatch_async(dispatch_get_main_queue(), ^{
    UIWindow *keyWindow = [self getKeyWindow];
    manager->showCaptcha(keyWindow,^(TDShowCaptchaResultStruct resultStruct) {
      NSMutableDictionary *resultDictionary = [NSMutableDictionary dictionary];
      switch (resultStruct.resultType) {
        case TDShowCaptchaResultTypeSuccess:
          resultDictionary[@"function"] = @"onSuccess";
          resultDictionary[@"token"] = @(resultStruct.validateToken);
          break;
        case TDShowCaptchaResultTypeFailed:
          resultDictionary[@"function"] = @"onFailed";
          resultDictionary[@"errorCode"] = @(resultStruct.errorCode);
          resultDictionary[@"errorMsg"] = @(resultStruct.errorMsg);
          break;
        case TDShowCaptchaResultTypeReady:
          resultDictionary[@"function"] = @"onReady";
          break;
        default:
          break;
      }
      resolve(resultDictionary);
    });
  });
}



                  


@end
