
#import "RNBaiduAi.h"
#import <AipOcrSdk/AipOcrSdk.h>
#import <React/UIView+React.h>

@implementation RNBaiduAi


@synthesize bridge = _bridge;
- (dispatch_queue_t)methodQueue
{
    return dispatch_get_main_queue();
}
RCT_EXPORT_MODULE()

-(NSMutableDictionary *) getImageDictionary:(UIImage *) image
{
    NSMutableDictionary *imageDictionary = [NSMutableDictionary dictionaryWithCapacity :10];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent:
                      @"image.png" ];
    NSData* data = UIImagePNGRepresentation(image);
    NSString *dataString = [data base64EncodedStringWithOptions:0];
    [data writeToFile:path atomically:YES];
    
    [imageDictionary setObject:path forKey:@"filePath"];
    [imageDictionary setObject:dataString forKey:@"data"];
    return imageDictionary;
    
}
RCT_EXPORT_METHOD(releaseCameraModel){
    NSLog(@"releaseCameraModel::::::::::");
}
RCT_EXPORT_METHOD(authWithAKSK: (NSString *) ak SK: (NSString *) sk)
{
    NSLog(@"authWithAkSk::::::::::");
    [[AipOcrService shardService] authWithAK:ak andSK:sk];
}
RCT_EXPORT_METHOD(scanBankCard: (BOOL) isFront
                  successResolve:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject
                  )
{
    NSLog(@"scanIdCard:::::::");
    UIViewController *rootVC=[UIApplication sharedApplication].keyWindow.rootViewController;
    UIViewController * vc = [AipCaptureCardVC ViewControllerWithCardType:CardTypeBankCard  andImageHandler:^(UIImage *image) {
        [[AipOcrService shardService] detectBankCardFromImage:image
                                               successHandler:^(id result){
                                                   NSMutableDictionary *response= [NSMutableDictionary dictionaryWithCapacity:10];
                                                   NSMutableDictionary *words= [NSMutableDictionary dictionaryWithCapacity:10];
                                                   
                                                   NSMutableDictionary *finalData= [NSMutableDictionary dictionaryWithCapacity:10];
                                                   [words setObject:result forKey:@"words"];
                                                   NSMutableDictionary *imageObject = [self getImageDictionary:image];
                                                   [finalData setObject:words forKey:@"data"];
                                                   [finalData setObject:imageObject forKey:@"image"];
                                                   [response setObject:finalData forKey:@"result"];
                                                   [rootVC dismissViewControllerAnimated:YES completion:nil];
                                                   resolve(response);
                                                   
                                               }
                                                  failHandler:^(NSError *err){
                                                      [rootVC dismissViewControllerAnimated:YES completion:nil];
                                                      reject(@"detect error",@"error:",err);
                                                      
                                                  }];
    }];
    [rootVC presentViewController:vc animated:YES completion:nil];
}

-(void) cancel: (RCTPromiseRejectBlock)reject{
//    ^(void){
//         self.rejectHandler(@"cancel",@"error:",nil);
//    };
   
    
}

RCT_EXPORT_METHOD(scanLicensePlate: (RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject){
    NSLog(@"scanLicensePlate:::::::");
    UIViewController *rootVC=[UIApplication sharedApplication].keyWindow.rootViewController;

    UIViewController * vc = [AipGeneralVC ViewControllerWithHandler:^(UIImage *image) {

           [[AipOcrService shardService] detectPlateNumberFromImage:image
                                                        withOptions:nil
                                                     successHandler:^(id result){
                NSMutableDictionary *imageObj= [NSMutableDictionary dictionaryWithCapacity:10];
               NSMutableDictionary *imageData = [self getImageDictionary:image];
               NSMutableDictionary *finalData= [NSMutableDictionary dictionaryWithCapacity:10];
               [finalData setObject:imageData forKey:@"image"];
               [finalData setObject:result forKey:@"data"];
               dispatch_async(dispatch_get_main_queue(), ^{
                                                     [rootVC dismissViewControllerAnimated:YES completion:nil];
               });
                                                     resolve(finalData);
               
           }
                                                        failHandler:^(NSError *err){
                                                            reject(@"detect error",@"error:",err);
                                                            
                                                        }];;

       }];
     dispatch_async(dispatch_get_main_queue(), ^{
       [rootVC presentViewController:vc animated:YES completion:nil];
     });
    
}

RCT_EXPORT_METHOD(scanIdCard: (BOOL) isFront
                  successResolve:(RCTPromiseResolveBlock)resolve
                  rejecter:(RCTPromiseRejectBlock)reject
                  )
{
    NSLog(@"scanIdCard:::::::");
    UIViewController *rootVC=[UIApplication sharedApplication].keyWindow.rootViewController;
    
    if(isFront){
        UIViewController * vc = [AipCaptureCardVC ViewControllerWithCardType:CardTypeLocalIdCardFont  andImageHandler:^(UIImage *image) {
            [[AipOcrService shardService] detectIdCardFrontFromImage:image
                                                         withOptions:nil
                                                      successHandler:^(id result){
                                                          NSMutableDictionary *response= [NSMutableDictionary dictionaryWithCapacity:10];
                                                          NSMutableDictionary *words= [NSMutableDictionary dictionaryWithCapacity:10];
                                                          
                                                          NSMutableDictionary *finalData= [NSMutableDictionary dictionaryWithCapacity:10];
                                                          [words setObject:result forKey:@"words"];
                                                          NSMutableDictionary *imageObject = [self getImageDictionary:image];
                                                          [finalData setObject:words forKey:@"data"];
                                                          [finalData setObject:imageObject forKey:@"image"];
                                                          [response setObject:finalData forKey:@"result"];
                dispatch_async(dispatch_get_main_queue(), ^{
                                                          [rootVC dismissViewControllerAnimated:YES completion:nil];
                    });
                                                          resolve(response);
                                                          
                                                      }
                               
                                                         failHandler:^(NSError *err){
                                                             reject(@"detect error",@"error:",err);
                                                             
                                                         }];
        }];
        
        
        dispatch_async(dispatch_get_main_queue(), ^{
           
            [rootVC presentViewController:vc animated:YES completion:nil];
            
        });
    }else{
        UIViewController * vc = [AipCaptureCardVC ViewControllerWithCardType:CardTypeLocalIdCardBack  andImageHandler:^(UIImage *image) {
            [[AipOcrService shardService] detectIdCardBackFromImage:image
                                                        withOptions:nil
                                                     successHandler:^(id result){
                                                         NSMutableDictionary *response= [NSMutableDictionary dictionaryWithCapacity:10];
                                                         NSMutableDictionary *words= [NSMutableDictionary dictionaryWithCapacity:10];
                                                         
                                                         NSMutableDictionary *finalData= [NSMutableDictionary dictionaryWithCapacity:10];
                                                         [words setObject:result forKey:@"words"];
                                                         NSMutableDictionary *imageObject = [self getImageDictionary:image];
                                                         [finalData setObject:words forKey:@"data"];
                                                         [finalData setObject:imageObject forKey:@"image"];
                                                         [response setObject:finalData forKey:@"result"];
                dispatch_async(dispatch_get_main_queue(), ^{
                                                         [rootVC dismissViewControllerAnimated:YES completion:nil];
                });
                                                         resolve(response);
                                                         
                                                     }
                                                        failHandler:^(NSError *err){
                                                            reject(@"detect error",@"error:",err);
                                                            
                                                        }];
        }];
       
        dispatch_async(dispatch_get_main_queue(), ^{
        [rootVC presentViewController:vc animated:YES completion:nil];
        });
    }
}
@end


