//
//  VerificationFailureAlertView.m
//  Open
//
//  Created by 雷俊博 on 2020/4/8.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "VerificationFailureAlertView.h"

@interface VerificationFailureAlertView ()
/** 弹窗主内容view */
@property (nonatomic,strong) UIView   *contentView;

@property (nonatomic, strong)UIImageView *img_bg;

@property (nonatomic, strong)UIButton* btn_reRZ;

@property (nonatomic, strong)UIButton* btn_kefu;

@property (nonatomic, copy)void (^Callback)(NSInteger index) ;

@end

@implementation VerificationFailureAlertView
#pragma mark - life cycle
- (instancetype)initWithCustomWithCallback:(void (^)(NSInteger index)  )Callback
{
    self = [super init];
    if (self) {
        [self setupUI];
        self.Callback = Callback;
    }
    return self;
}

#pragma mark - UI搭建
/** UI搭建 */
- (void)setupUI
{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
       
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.bounds = CGRectMake(0, 0, 267*WIDTHRADIU, 200 * WIDTHRADIU);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor clearColor];
       
    [self.contentView addSubview:self.img_bg];
    
    [self.img_bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
}


+ (void)showWithViewController:(UIViewController*)viewController Callback:(void (^)(NSInteger index)  )Callback;
{
    UIWindow* keyWindow = viewController.view.window;
    
    if (keyWindow == nil) {
        keyWindow = [UIApplication sharedApplication].keyWindow;
    }
    
    VerificationFailureAlertView *alertView = [[VerificationFailureAlertView alloc]initWithCustomWithCallback:Callback];
    
    [alertView show:keyWindow];
}


#pragma mark - 显示或隐藏
/** 弹出此弹窗 */
- (void)show:(UIWindow *)window {
    // 出场动画
    self.alpha = 0;
    self.contentView.transform = CGAffineTransformScale(self.contentView.transform, 1.3, 1.3);
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 1;
        self.contentView.transform = CGAffineTransformIdentity;
    }];
    
    [window addSubview:self];
}

/** 移除此弹窗 */
- (void)dismiss {
    [self removeFromSuperview];
}


#pragma mark - event response
- (void)btnClick:(UIButton *)sender
{
    if (sender == self.btn_reRZ) {
        if (self.Callback) {
            self.Callback(200);
        }
        [self dismiss];
    }else if (sender == self.btn_kefu){
        if (self.Callback) {
            self.Callback(201);
        }
        [self dismiss];
    }
}


#pragma mark - getters and setters
- (UIImageView *)img_bg
{
    if (!_img_bg) {
        _img_bg = [[UIImageView alloc]init];
        _img_bg.image = [UIImage imageNamed:@"face_10"];
        [_img_bg addSubview:self.btn_reRZ];
        [_img_bg addSubview:self.btn_kefu];
        _img_bg.userInteractionEnabled = YES;
        [self.btn_reRZ mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(126 * WIDTHRADIU);
            make.width.offset(100 * WIDTHRADIU);
            make.height.offset(45 * WIDTHRADIU);
            make.left.offset(20 * WIDTHRADIU);
        }];
        
        [self.btn_kefu mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(126 * WIDTHRADIU);
            make.width.offset(100 * WIDTHRADIU);
            make.height.offset(45 * WIDTHRADIU);
            make.right.offset(-20 * WIDTHRADIU);
        }];
    }
    return _img_bg;
}

- (UIButton*)btn_reRZ
{
    if (!_btn_reRZ) {
        _btn_reRZ = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn_reRZ.backgroundColor = [UIColor colorWithHex:0x13a480];
        _btn_reRZ.layer.cornerRadius = 5 * WIDTHRADIU;
        [_btn_reRZ setTitle:@"重新认证" forState:UIControlStateNormal];
        [_btn_reRZ setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _btn_reRZ.titleLabel.font = [UIFont systemFontOfSize:19 * WIDTHRADIU];
        [_btn_reRZ addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn_reRZ;
}

- (UIButton*)btn_kefu
{
    if (!_btn_kefu) {
        _btn_kefu = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn_kefu.backgroundColor = [UIColor colorWithHex:0x13a480];
        _btn_kefu.layer.cornerRadius = 5 * WIDTHRADIU;
        [_btn_kefu setTitle:@"人工识别" forState:UIControlStateNormal];
        [_btn_kefu setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _btn_kefu.titleLabel.font = [UIFont systemFontOfSize:19 * WIDTHRADIU];
        [_btn_kefu addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn_kefu;
}

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

@end
