
#import "RNBaiduFace.h"
#import "LivenessViewController.h"
#import "IDLFaceSDK/IDLFaceSDK.h"
#import "LivingConfigModel.h"
#import "DetectionViewController.h"
#import "BDFaceCompeleteDelegate.h"

@interface RNBaiduFace ()<BDFaceCompleteDelegate>

@property (nonatomic,copy) RCTResponseSenderBlock callBackCopy;

@end


@implementation RNBaiduFace


static RNBaiduFace* _instance = nil;


+(instancetype) shareInstance
{
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc] init] ;
    }) ;
    
    return _instance ;
}


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



-(void) initBDFaceSdkAndViewController:(UIViewController * ) rootvc Name:(NSString*)name Type:(NSString*)type LicenseId:(NSString *)lid {
    NSString* licensePath = [[NSBundle mainBundle] pathForResource:name ofType:type];
    NSAssert([[NSFileManager defaultManager] fileExistsAtPath:licensePath], @"license文件路径不对，请仔细查看文档");
    [[FaceSDKManager sharedInstance] setLicenseID:lid andLocalLicenceFile:licensePath];
    self.rootVc = rootvc;
    self.fname = name;
    self.ftype = type;
    self.licenseId = lid;
    
}

RCT_EXPORT_MODULE()

RCT_EXPORT_METHOD(livenessWithCallBack:(RCTResponseSenderBlock)callback)
{
  if ([[FaceSDKManager sharedInstance] canWork]) {
      NSString* licensePath = [[NSBundle mainBundle] pathForResource:[[RNBaiduFace shareInstance] fname] ofType:[[RNBaiduFace shareInstance] ftype]];
      [[FaceSDKManager sharedInstance] setLicenseID:[[RNBaiduFace shareInstance] licenseId] andLocalLicenceFile:licensePath];
  }
    
    [[RNBaiduFace shareInstance] setCallBackCopy:callback];
  LivenessViewController* lvc = [[LivenessViewController alloc] init];
    lvc.delegate = [RNBaiduFace shareInstance];
  LivingConfigModel* model = [LivingConfigModel sharedInstance];
  [lvc livenesswithList:model.liveActionArray order:model.isByOrder numberOfLiveness:model.numOfLiveness];
  UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:lvc];
  navi.navigationBarHidden = true;
  [[RNBaiduFace shareInstance].rootVc presentViewController:navi animated:YES completion:nil];
}

RCT_EXPORT_METHOD(detectWithCallBack:(RCTResponseSenderBlock)callback)
{
  if ([[FaceSDKManager sharedInstance] canWork]) {
      NSString* licensePath = [[NSBundle mainBundle] pathForResource:[[RNBaiduFace shareInstance] fname] ofType:[[RNBaiduFace shareInstance] ftype]];
      [[FaceSDKManager sharedInstance] setLicenseID:[[RNBaiduFace shareInstance] licenseId] andLocalLicenceFile:licensePath];
  }
    [[RNBaiduFace shareInstance] setCallBackCopy:callback];
    DetectionViewController* dvc = [[DetectionViewController alloc] init];
    dvc.delegate = [RNBaiduFace shareInstance];
    UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:dvc];
    navi.navigationBarHidden = true;
  [[RNBaiduFace shareInstance].rootVc presentViewController:navi animated:YES completion:nil];
}

-(void)getImageSuccess:(NSData *)data{
//    NSData *data = UIImagePNGRepresentation(img);
    NSString *encodedImageStr = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//    encodedImageStr = [@"data:image/jpeg;base64," stringByAppendingString:encodedImageStr];
    [RNBaiduFace shareInstance].callBackCopy(@[[NSNull null],encodedImageStr]);
}

@end
  
