//
//  JinDuAlertView.m
//  KKCivilAd
//
//  Created by 雷俊博 on 2020/4/17.
//  Copyright © 2020 雷俊博. All rights reserved.
//

#import "JinDuAlertView.h"
#import "UIView+frameAdjust.h"
#import "UIViewController+ChangeUI.h"

#import "Globaltypedef.h"
#if TARGET_IPHONE_SIMULATOR == 0
#import "SCCaptureCameraController.h"
#endif

@interface JinDuAlertView ()
#if TARGET_IPHONE_SIMULATOR == 0
   <SCNavigationControllerDelegate>
#endif
{
    BOOL showView;
    NSString* idCard;
}

@property (nonatomic, strong)UIView* topView;

@property (nonatomic, strong)UIView* contentView;

@property (nonatomic, strong)UIViewController*vc;

@property (nonatomic, assign)CGFloat minY;

@property (nonatomic, strong)UIView*maskView;

@property (nonatomic, strong)UILabel*lb_title;

@property (nonatomic, strong)UIButton*btn_Card;

@property (nonatomic, strong)UIButton*cancelBtn;

@property (nonatomic, strong)UIButton*confirmBtn;


@end

@implementation JinDuAlertView
#pragma mark - life cycle

- (instancetype)initWithVC:(UIViewController *)vc origialY:(CGFloat)y
{
    self = [super init];
    if (self) {
        self.vc = vc;
        self.minY = y;
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        self.frame = CGRectMake(0, y, vc.view.bounds.size.width, 40* WIDTHRADIU);
        [self configUI];
    }
    return self;
}

- (void)configUI
{
    [self.vc.view addSubview:self];
    //创建contentView
    UIView* contentView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 40 * WIDTHRADIU)];
    contentView.backgroundColor = [UIColor whiteColor];
    [self addSubview:contentView];
    
    self.contentView = contentView;
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    //topview;
    [contentView addSubview:self.topView];
    
   
   
    
}

#pragma mark - SCNavigationControllerDelegate
- (void)sendIDCValue:(NSString *)name SEX:(NSString *)sex FOLK:(NSString *)folk BIRTHDAY:(NSString *)birthday ADDRESS:(NSString *) address NUM:(NSString *)num
{
    
    if (![IDCardNumber_Validate isValidateIDCardString:num]) {
        [Tools showMessage:@"身份证识别错误,请重新拍摄"];
        return;
    }
   
    idCard = [num stringByReplacingOccurrencesOfString:@" " withString:@""];
}

- (void)sendTakeImage:(TCARD_TYPE) iCardType image:(UIImage *)cardImage
{
    if (iCardType == TIDCARD2) {
        [self.btn_Card setBackgroundImage:cardImage forState:UIControlStateNormal];
    }
}


#pragma mark - 隐藏和显示
-(void)show
{
    
    
    
    
    CGRect frame = self.frame;
    frame.size.height = 0;
       
    self.frame = frame;
    [UIView animateWithDuration:0.5 animations:^{
           
           CGRect frame = self.frame;
           frame.size.height = 370 * WIDTHRADIU;
              
           self.frame = frame;
           
       } completion:nil];
    
    UIView* maskView = [[UIView alloc]init];
       
       self.maskView = maskView;
       maskView.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.5];
      
       [self.vc.view addSubview:maskView];
       
       [maskView mas_makeConstraints:^(MASConstraintMaker *make) {
           make.left.right.bottom.equalTo(self.vc.view);
           make.top.offset(self.maxY);
       }];
       
       
       [maskView layoutIfNeeded];
    
    
   
    
    //title
         UILabel*lb_title = [[UILabel alloc]init];
         lb_title.text = @"拍摄身份证:";
         lb_title.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
         lb_title.textColor = [UIColor colorWithHex:0x4d4d4d];
         self.lb_title = lb_title;
        [self.contentView addSubview:lb_title];
         
         //身份证图片
         UIButton* btn_Card = [UIButton buttonWithType:UIButtonTypeCustom];
         [btn_Card setBackgroundImage:[UIImage imageNamed:@"jinduIdbackImg"] forState:UIControlStateNormal];
    [btn_Card addTarget:self action:@selector(photoClick) forControlEvents:UIControlEventTouchUpInside];
         self.btn_Card = btn_Card;
         [self.contentView addSubview:btn_Card];
         
         //
         UIButton* cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
         [cancelBtn setTitle:@"取消筛选" forState:UIControlStateNormal];
         [cancelBtn setTitleColor:[UIColor colorWithHex:0x4d4d4d] forState:UIControlStateNormal];
         cancelBtn.backgroundColor = [UIColor whiteColor];
         cancelBtn.layer.cornerRadius = 4 * WIDTHRADIU;
         cancelBtn.layer.borderWidth = 0.5;
         cancelBtn.layer.borderColor = [UIColor colorWithHex:0xcccccc].CGColor;
         cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
         [cancelBtn addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
         self.cancelBtn = cancelBtn;
         [self.contentView addSubview:cancelBtn];
         
         
         //
         UIButton*  confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
         [confirmBtn setTitle:@"确认筛选" forState:UIControlStateNormal];
         [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
         confirmBtn.backgroundColor = K_MainColor;
         confirmBtn.layer.cornerRadius = 4 * WIDTHRADIU;
         confirmBtn.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
         [confirmBtn addTarget:self action:@selector(confirmBtnClick) forControlEvents:UIControlEventTouchUpInside];
         self.confirmBtn = confirmBtn;
         [self.contentView addSubview:confirmBtn];
         
         
         
         [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
             make.top.equalTo(self.topView.mas_bottom).offset(5 * WIDTHRADIU);
             make.left.offset(15 * WIDTHRADIU);
         }];
         
         [btn_Card mas_makeConstraints:^(MASConstraintMaker *make) {
             make.top.equalTo(self.topView.mas_bottom).offset(30 * WIDTHRADIU);
             make.centerX.equalTo(self.contentView);
             make.width.offset(355 * WIDTHRADIU);
             make.height.offset(200 * WIDTHRADIU);
         }];
         
         [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
             make.top.equalTo(btn_Card.mas_bottom).offset(30 * WIDTHRADIU);
             make.left.offset(15 * WIDTHRADIU);
             make.height.offset(45 * WIDTHRADIU);
             make.width.equalTo(confirmBtn).multipliedBy(0.5);
         }];
         
         [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
             make.top.height.equalTo(cancelBtn);
             make.left.equalTo(cancelBtn.mas_right).offset(15 * WIDTHRADIU);
             make.right.offset(-15 * WIDTHRADIU);
         }];
    
    
    [self layoutIfNeeded];
   
}

- (void)dismiss
{
        self.maskView.alpha              = 0;
         CGRect frame = self.frame;
         frame.size.height = 40 * WIDTHRADIU;
         self.frame = frame;
        
   
        [self.lb_title removeFromSuperview];
        [self.btn_Card removeFromSuperview];
        [self.cancelBtn removeFromSuperview];
        [self.confirmBtn removeFromSuperview];
        [self layoutIfNeeded];
        [self.maskView removeFromSuperview];
         
       idCard = nil;
}

#pragma mark - 启动ocr
- (void)startOCR
{
    
    #if TARGET_IPHONE_SIMULATOR == 0
    SCCaptureCameraController*con = [[SCCaptureCameraController alloc] init];
    con.scNaigationDelegate = self;
    con.iCardType = TIDCARD2; // 其他证件以此类推
    con.isDisPlayTxt = NO;
    con.ScanMode = TIDC_SCAN_MODE;
    con.modalPresentationStyle = UIModalPresentationFullScreen;
    [self.vc presentViewController:con animated:YES completion:NULL];
    #endif
    
}



#pragma mark - event response
- (void)cancelClick
{
    showView = NO;
    [self dismiss];
}

- (void)confirmBtnClick
{
   
    if (idCard && self.block) {
        self.block(idCard);
    }
    showView = NO;
   [self dismiss];
    
   
}

- (void)btnClck:(UIButton *)sender
{
    showView = !showView;
    if (showView) {
        [self show];
    }else{
        [self dismiss];
    }
}

- (void)photoClick
{
    [self startOCR];
}

#pragma mark - getters and setters
- (UIView *)topView
{
    if (!_topView) {
        _topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 40 * WIDTHRADIU)];
        
        UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"条件筛选" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHex:0x4d4d4d] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"pullDownImage"] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
        [btn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleRight imageTitleSpace:4];
        [btn addTarget:self action:@selector(btnClck:) forControlEvents:UIControlEventTouchUpInside];
        [_topView addSubview:btn];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.height.equalTo(_topView);
            make.width.offset(120);
        }];
        
    }
    return _topView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
