//
//  artificalViewController.m
//  socialSecurity
//
//  Created by 看看智能 on 2017/7/11.
//  Copyright © 2017年 黄江涛 . All rights reserved.
//

#import "artificalViewController.h"
#import "IOSOCRAPI.h"
#import "Globaltypedef.h"
#import "SCCaptureCameraController.h"
#import "CityNetManager.h"
@interface artificalViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *AR_image;
@property (weak, nonatomic) IBOutlet UIButton *AR_button_edit;
@property (weak, nonatomic) IBOutlet UIButton *AR_button_cancel;




@property(nonatomic,assign)BOOL qudaoOCR;
//ocr的照片
@property(nonatomic,strong)NSString *ocrImage;
@end

@implementation artificalViewController
  WEBS
-(void)buttonClick_1{    ZHINENGKEFU}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title =@"人工提交";
//    UIButton * button_2 = [UIButton buttonWithType:UIButtonTypeCustom];        button_2.frame = CGRectMake(0, 0, 0, 49);     button_2.titleLabel.textAlignment =NSTextAlignmentRight;   [button_2 setTitle:@"     帮助" forState:UIControlStateNormal];        [button_2 addTarget:self action:@selector(buttonClick_1) forControlEvents:UIControlEventTouchUpInside];        UIBarButtonItem * backButtonItem_2 = [[UIBarButtonItem alloc] initWithCustomView:button_2];        self.navigationItem.rightBarButtonItem = backButtonItem_2;
    self.qudaoOCR= NO;
}
#pragma mark - 提交按钮
- (IBAction)buttonForEdit:(id)sender {

    
    SCCaptureCameraController *con = [[SCCaptureCameraController alloc] init];
    con.scNaigationDelegate = self;
    con.iCardType = TIDCARD2;
    con.isDisPlayTxt = NO;
    //con.TimeKey = TimeKeyS;
    con.ScanMode = TIDC_SCAN_MODE;
    [self presentViewController:con animated:YES completion:NULL];
    
}
#pragma mark - 取消按钮
- (IBAction)buttonForCancel:(id)sender {
    if (self.qudaoOCR == NO) {
        [SVProgressHUD showErrorWithStatus:@"请先拍身份证照片"];
        
        return;
    }
    
    if(self.auth_id!=nil){
        [SVProgressHUD show];
        [CityNetManager manualVerificationBakID:self.sqId andidentityNumber:self.auth_id andImage:UIImageJPEGRepresentation(self.failurePic,0.8) andOCR:self.ocrImage andTel:nil   completionHandle:^(CitytoCityModel *model, NSError *error) {
            if (model.code ==0) {
                [SVProgressHUD dismiss];
                SVProgressHUD.minimumDismissTimeInterval=1.0;
                [SVProgressHUD showSuccessWithStatus:@"人工提交成功,请关注社保认证记录"];
                [self.navigationController popToRootViewControllerAnimated:YES];
            }else
            {
                [SVProgressHUD dismiss];
                codeSelected *code = [codeSelected MustTakeReturn:model.code];
                [code returnCodeMethod];
            }
        }];
        
    }else{
        [SVProgressHUD show];
        [CityNetManager manualVerificationBakID:[[NSUserDefaults standardUserDefaults]objectForKey:@"bak"] andidentityNumber:[[NSUserDefaults standardUserDefaults]objectForKey:@"identityNumber"] andImage:UIImageJPEGRepresentation(self.failurePic,0.8) andOCR:self.ocrImage andTel:nil   completionHandle:^(CitytoCityModel *model, NSError *error) {
            if (model.code ==0) {
                [SVProgressHUD dismiss];
                SVProgressHUD.minimumDismissTimeInterval=1.0;
                [SVProgressHUD showSuccessWithStatus:@"人工提交成功,请关注社保认证记录"];
                [self.navigationController popToRootViewControllerAnimated:YES];
            }else
            {
                [SVProgressHUD dismiss];
                codeSelected *code = [codeSelected MustTakeReturn:model.code];
                [code returnCodeMethod];
            }
        }];
    }
    
}
#pragma mark - 调用OCR

-(void)getOCR
{
#if TARGET_IPHONE_SIMULATOR
    
#else
    // 打开引擎
    Gint ret = 0;
    Gint vaule = 1;
    ret = TREC_StartUP(TREC_GetEngineTimeKEY());
    if (ret == 100) {
        NSLog(@"引擎过期\n");
        return;
    }
    if (ret == 100) {
        NSLog(@"引擎过期\n");
        return;
    }if (ret == 101) {
        NSLog(@"引擎绑定失败\n");
        return;
    }
    //获取引擎信息，可以不用操作，可有可无
    //  self.nameText.text=  [[NSString alloc] initWithString:[NSString stringWithFormat:@"%@\n%@\n%@\n",TREC_GetCopyrightInfoArray(),TREC_GetVersionArray(),TREC_GetTimeStatus()]];
    // 设置引擎参数
    ret = TREC_SetParam(T_SET_HEADIMG, &vaule);
    // 设置引擎类型
    ret = TREC_SetSupportEngine(TIDCARD2);// 演示使用身份证，其他证件以此类推
    if (ret != 1) {
        NSLog(@"引擎不支持\n");
        TREC_ClearUP();
        return;
    }
    
    
    // 引擎内存释放
    ret = TREC_ClearUP();
#endif
    
    
    
}

- (void)sendCardFaceImage:(UIImage *)image
{
    if (image != NULL) {
    }
}

- (void)sendTakeImage:(TCARD_TYPE) iCardType image:(UIImage *)cardImage
{
    NSData *small =UIImageJPEGRepresentation([self imageByScalingAndCroppingForSize:CGSizeMake(667, 375) and:cardImage], 1.0);
    self.ocrImage = [small base64EncodedStringWithOptions:0];
    self.AR_image.image =[UIImage imageWithData:small];
    self.qudaoOCR = YES;
}
- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize and:(UIImage*)imageKs
{
    UIImage *sourceImage = imageKs;
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    
    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        
        if (widthFactor > heightFactor)
            scaleFactor = widthFactor; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        scaledWidth= width * scaleFactor;
        scaledHeight = height * scaleFactor;
        
        // center the image
        if (widthFactor > heightFactor)
        {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else if (widthFactor < heightFactor)
        {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    
    //UIGraphicsBeginImageContext(targetSize); // this will crop
    UIGraphicsImageRenderer*re=[[UIGraphicsImageRenderer alloc] initWithSize:targetSize];
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width= scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil)
        NSLog(@"could not scale image");
    
    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    return newImage;
}

@end
