/*
 * Copyright © Dynamsoft Corporation. All rights reserved.
 */

#import "RNDynamsoftLicense.h"
#import <DynamsoftCaptureVisionBundle/DynamsoftCaptureVisionBundle.h>

@interface RNDynamsoftLicense () <DSLicenseVerificationListener>

@end

@implementation RNDynamsoftLicense {
    RCTPromiseResolveBlock licenseResolve;
    RCTPromiseRejectBlock licenseReject;
}

RCT_EXPORT_MODULE(DynamsoftLicenseModule)

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

+ (BOOL)requiresMainQueueSetup {
    return false;
}

RCT_EXPORT_METHOD(initLicense:(NSString *)license
                  resolver:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject)
{
    licenseResolve = resolve;
    licenseReject = reject;
    [DSLicenseManager initLicense:license verificationDelegate:self];
}

- (void)onLicenseVerified:(BOOL)isSuccess error:(nullable NSError *)error {
    if (isSuccess) {
        licenseResolve(@(YES));
    } else {
        NSString *code = [NSString stringWithFormat:@"%ld",(long)error.code];
        licenseReject(code, error.localizedDescription, error);
    }
}

@end
