//
//  SearchProgressViewController.m
//  Open
//
//  Created by 看看智能 on 2017/11/29.
//  Copyright © 2017年 黄江涛 . All rights reserved.
//

#import "SearchProgressViewController.h"
#import "CTAPIBaseManager.h"

@interface SearchProgressViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong)CTAPIBaseManager *manager;

@end

@implementation SearchProgressViewController

-(void)buttonClick_1{
    [self maoasfdsfg];

}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
   [SVProgressHUD show];
    self.BackGuardTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [SearchProgressNet getgetBindIdCardsListHandle:^(SearchProgress *model, NSError *error) {
        if (model.code ==0) {
            [SVProgressHUD dismiss];
            
            NSLog(@"%@",model.result[0].idCardPhoto);
            
            [self.baochunShuZu removeAllObjects];
            [self.baochunShuZu addObjectsFromArray:model.result];
            [self.BackGuardTableView reloadData];
        }else
        {
            [SVProgressHUD dismiss];
            codeSelected *code = [codeSelected MustTakeReturn:model.code];
            [code returnCodeMethod];
        }
    }];
    
    
    
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
       self.navigationController.navigationBar.translucent =YES;

 
    self.BackGuardTableView.delegate =self;
    self.BackGuardTableView.dataSource =self;
    if (@available(iOS 11.0, *)) {
        self.BackGuardTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
      self.BackGuardTableView.contentInset = UIEdgeInsetsMake(20, 0, 49, 0);
        self.BackGuardTableView.scrollIndicatorInsets = self.BackGuardTableView.contentInset;
    }else{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.BackGuardTableView.contentInset = UIEdgeInsetsMake(20, 0, 49, 0);
    }
    self.title =@"社保认证状态";
    [self getOCR];
    //设置初始化未100
    self.rows =100;

    UIBarButtonItem *right1 = [[UIBarButtonItem alloc] initWithTitle:@" 添加证件" style:UIBarButtonItemStyleDone target:self action:@selector(buttonClick_1)];

    self.navigationItem.rightBarButtonItem = right1;
    


}


#pragma mark - loadData
- (void)loadData
{
    
}

#pragma mark - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.baochunShuZu.count;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier =@"SPBackgroundCell";
    SPBackgroundCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SPBackgroundCell"];
    if (cell == nil) {
        cell = [[SPBackgroundCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // 放入姓名和身份证信息
   SearchProgressResult *SPmodes =    self.baochunShuZu[indexPath.section];
    cell.SPmodel =SPmodes;
    if (self.isOpen == YES) {
        cell.zhankaijiantou.image = [UIImage imageNamed:@"进度展开-箭头"];
    }else{
       cell.zhankaijiantou.image = [UIImage imageNamed:@"进度未展开-箭头"];
    }
   
    
    
    
    

//    if (self.isOpen == YES) {
//
       [cell setNeedsDisplay];
//
//    }
    
//    for ( in <#collection#>) {
//        <#statements#>
//    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 20;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc]init];    headView.backgroundColor = [UIColor clearColor];
    return headView;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.rows =indexPath.section;

    if(self.isOpen == YES){
                 [self.BackGuardTableView reloadData];
     
        
        
        
        
        
        self.isOpen = NO;
 
        
    }else{
                 [self.BackGuardTableView reloadData];
          self.isOpen = YES;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    printf("%ld",(long)indexPath.section );

        if (indexPath.section ==  self.rows && self.isOpen == YES) {
            return 284.5 + 65-142.25 + 10;//我改动的地方
        }
        
         return 65;
    
    
}





#pragma mark - 调用OCR
-(void)maoasfdsfg{
    
    //获取ocr
    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];
    
    
}
-(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)sendIDCValue:(NSString *)name SEX:(NSString *)sex FOLK:(NSString *)folk BIRTHDAY:(NSString *)birthday ADDRESS:(NSString *) address NUM:(NSString *)num
{



    GetProgressController *getVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"GetProgressController"];
    getVC.nameOCR =name;
    getVC.IDOCR =num;
    getVC.SearchimageOCR = self.imageOCR;
    [self.navigationController pushViewController:getVC animated:YES];
    
}
- (void)sendTakeImage:(TCARD_TYPE) iCardType image:(UIImage *)cardImage
{
    NSData *small =UIImageJPEGRepresentation([self imageByScalingAndCroppingForSize:CGSizeMake(667, 375) and:cardImage], 1.0);
   
    if([[small base64EncodedStringWithOptions:0] isEqualToString:@""]){
        [SVProgressHUD showErrorWithStatus:@"识别错误"];
        return;
    }else{
        self.imageOCR = [small base64EncodedStringWithOptions:0];
        
    }
    
}


#pragma mark - private methods
//图片压缩
- (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;
}

#pragma mark - getters and setters
- (CTAPIBaseManager *)manager
{
    if (!_manager) {
        _manager = [[CTAPIBaseManager alloc]init];
    }
    return _manager;
}

-(NSMutableArray *)baochunShuZu
{
    
    if (!_baochunShuZu) {
        _baochunShuZu =[NSMutableArray new];
        
    }
    return _baochunShuZu;
    
    
}



@end
