//
//  RNExocrLiveness.m
//  RN59
//
//  Created by linjson on 2022/2/10.
//  Copyright © 2022 Facebook. All rights reserved.
//

#import "RNExocrLiveness.h"
#import <HTJCFaceLiveDetectSdk/HTJCFaceLiveDetectManager.h>
#import <React/RCTConvert.h>
@implementation RNExocrLiveness

RCT_EXPORT_MODULE(RNExocrLiveness);

- (dispatch_queue_t) methodQueue
{
    return dispatch_get_main_queue();
}

RCT_EXPORT_METHOD(openLiveness:(NSDictionary*)value resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject) {
    BOOL cameraBack = [RCTConvert BOOL:value[@"cameraBack"]];
    NSArray* actions = [RCTConvert NSArray:value[@"actions"]];

    UIViewController* viewController = [UIApplication sharedApplication].delegate.window.rootViewController;

    HTJCFaceLiveDetectManager* manager = [HTJCFaceLiveDetectManager sharedManager:viewController];

    manager.isCameraBack = cameraBack;

    manager.liveDetectTypeArray = actions;

    [manager getLiveDetectCompletion:^(BOOL success, NSData* imageData) {
        [manager dismissTakeCaptureSessionViewController];
        NSString* filePath = [self writeToSandBox:imageData];
        BOOL ok = success && (filePath != nil);
        if (ok) {
            NSDictionary* result = @{
                @"success": @(ok),
                @"filePath": filePath
            };
            resolve(result);
        } else {
            reject(@"-99", @"文件保存失败", nil);
        }
    } cancel:^(BOOL success, NSError* error) {
        reject([NSString stringWithFormat:@"%ld", error.code], error.domain, nil);
    } failed:^(NSError* error, NSData* imageData) {
        reject([NSString stringWithFormat:@"%ld", error.code], error.domain, nil);
    }];
}

- (NSString*) writeToSandBox:(NSData*)imageData {
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsDirectory = [paths objectAtIndex:0];
    NSString* testDirectory = [documentsDirectory stringByAppendingPathComponent:@"/saveImage"];
    NSFileManager* fileManager = [NSFileManager defaultManager];
    BOOL res = [fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    if (!res) {
        return nil;
    } else {
        NSString* name = [self getTimeInterval];
        NSString* filePath = [testDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", name]];
        BOOL res = [imageData writeToFile:filePath atomically:YES];
        return res ? filePath : nil;
    }
}

//获取当前时间
- (NSString*) getTimeInterval {
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYYMMddHHmmss"];
    NSString* currentTime = [formatter stringFromDate:[NSDate date]];
    return currentTime;
}

//- (NSString*) transformErrorToMessage:(NSError*)error {
//    NSString* errorInfo = error.domain;
//    NSRange range = [errorInfo rangeOfString:@":"];
//    if (range.length > 0) {
//        NSString* failedStr = [self reminderWithCode:error.code];
//        errorInfo = [NSString stringWithFormat:@"%@ : %ld", failedStr, (long)error.code];
//    }
//    return errorInfo;
//}
//
//- (NSString*) reminderWithCode:(NSInteger)errorCode {
//    NSString* reminderMessage = @"";
//    if (errorCode == -1000) {
//        reminderMessage = @"检测中断";
//    } else if (errorCode == -1001) {
//        reminderMessage = @"引导检测超时";
//    } else if (errorCode == -1002) {
//        reminderMessage = @"1.光线太明或太暗\n2.操作过程中有遮挡，未露出额头\n3.操作过程中幅度过大或非常规操作\n4.未检测到人脸";
//    } else if (errorCode == -1003) {
//        reminderMessage = @"屏幕中出现多张人脸";
//    } else if (errorCode == -1004) {
//    } else if (errorCode == -1005) {
//        reminderMessage = @"操作过程中幅度过大或非常规操作";
//    } else if (errorCode == -1006) {
//        reminderMessage = @"检测超时";
//    } else if (errorCode == -1007) {
//        reminderMessage = @"1.光线太明或太暗\n2.非常规操作\n3.未将脸部置于屏幕正中间";
//    } else if (errorCode == -1008 || errorCode == -1010 || errorCode == -1011) {
//        reminderMessage = @"非常规操作";
//    } else if (errorCode == -1009) {
//        reminderMessage = @"1.光线太明或太暗\n2.非常规操作";
//    } else if (errorCode == -1012) {
//        reminderMessage = @"初始化错误";
//    } else if (errorCode == -1013) {
//        reminderMessage = @"授权异常";
//    } else if (errorCode == -1014) {
//        reminderMessage = @"摄像头打开失败";
//    } else if (errorCode == -1015) {
//        reminderMessage = @"SD卡打开失败";
//    } else {
//        reminderMessage = @"未知错误";
//    }
//
//    return reminderMessage;
//}

@end
