//
//  KKAuthWhiteAlertView.m
//  Open
//
//  Created by 雷俊博 on 2021/1/20.
//  Copyright © 2021 黄江涛 . All rights reserved.
//

#import "KKAuthWhiteAlertView.h"

@interface KKAuthWhiteAlertView ()<UIGestureRecognizerDelegate>

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

@property (nonatomic,copy)NSString* title;

@property (nonatomic,copy)NSString* message;

@property (nonatomic,assign)KKAuthWhiteAlertViewType type;

@property (nonatomic,copy)KKAuthWhiteAlertViewBlock block;

@end

@implementation KKAuthWhiteAlertView

- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message type:(KKAuthWhiteAlertViewType)type block:(KKAuthWhiteAlertViewBlock)block {
    self = [super init];
    if (self) {
        
        self.title = title;
        self.message = message;
        self.type = type;
        self.block = block;
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
    
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.bounds = CGRectMake(0, 0, 280 * WIDTHRADIU, 290 * WIDTHRADIU);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 15 * WIDTHRADIU;
    
    //顶部头像
    UIImageView* topIcon = [UIImageView new];
    topIcon.image = [UIImage imageNamed:@"auth_touxiang"];
    [self.contentView addSubview:topIcon];
    [topIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(20 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
        make.width.height.offset(90 * WIDTHRADIU);
    }];
        
    
    UIButton* closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[UIImage imageNamed:@"close"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(40 * WIDTHRADIU);
        make.top.offset(0);
        make.right.offset(0);
    }];
    
    
    
    
    UILabel* lb_title = [UILabel new];
    lb_title.text = self.title;
    lb_title.textAlignment = NSTextAlignmentCenter;
    lb_title.textColor = [UIColor colorWithHex:0x666666];
    lb_title.font = [UIFont boldSystemFontOfSize:18 * WIDTHRADIU];
    [self.contentView addSubview:lb_title];
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topIcon.mas_bottom).offset(15 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
    
    UILabel* lb_message = [UILabel new];
    lb_message.attributedText = [self getAttributedStringWithString:self.message lineSpace:8 kern:0.5];
    lb_message.textAlignment = NSTextAlignmentCenter;
    lb_message.font = [UIFont systemFontOfSize:15];
    lb_message.numberOfLines = 0;
    [self.contentView addSubview:lb_message];
    
    [lb_message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15 * WIDTHRADIU);
        make.right.offset(-15 * WIDTHRADIU);
        make.top.equalTo(lb_title.mas_bottom).offset(15 * WIDTHRADIU);
    }];
    
   
    
    UIButton * authBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    authBtn.backgroundColor = [UIColor colorWithHex:0x086ed7];
    
    if (self.type == KKAuthWhiteAlertViewTypeAuth) {
        [authBtn setTitle:@"立即实名" forState:UIControlStateNormal];
    }else if (self.type == KKAuthWhiteAlertViewTypeUpper) {
        [authBtn setTitle:@"确定" forState:UIControlStateNormal];
    }
    
    
    [authBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    authBtn.titleLabel.font = [UIFont systemFontOfSize:16 * WIDTHRADIU];
    [authBtn addTarget:self action:@selector(authBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:authBtn];
    authBtn.layer.cornerRadius = 22.5 * WIDTHRADIU;
    [authBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(- 10 * WIDTHRADIU);
        make.left.offset(10 * WIDTHRADIU);
        make.top.equalTo(lb_message.mas_bottom).offset(20 * WIDTHRADIU);
        make.height.offset(45 * WIDTHRADIU);
    }];
    
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:self.contentView]) {
        return NO;
    }
    
    return YES;
}

#pragma mark - public methods
+ (void)showAlertViewWithVC:(UIViewController *)vc type:(KKAuthWhiteAlertViewType)type title:(NSString *)title message:(NSString *)message block:(KKAuthWhiteAlertViewBlock)block {
    KKAuthWhiteAlertView*baseView = [[KKAuthWhiteAlertView alloc]initWithTitle:title message:message type:type block:block];
    UIWindow* keyWindow = vc.view.window;
    
    if (keyWindow == nil) {
        keyWindow = [UIApplication sharedApplication].keyWindow;
    }

    [keyWindow addSubview:baseView];
}


#pragma mark - event response
- (void)tapClick:(UITapGestureRecognizer *)tap {
    [self dismissUI];
}

- (void)authBtnClick:(UIButton *)sender {
    
    if (self.type == KKAuthWhiteAlertViewTypeAuth) {
        if (self.block) {
            self.block();
        }
    }
    
    [self dismissUI];
}

//关闭
- (void)closeBtnClick:(UIButton *)sender
{
    [self dismissUI];
}


#pragma mark -隐藏弹框
- (void)dismissUI
{
     [self removeFromSuperview];
}

#pragma mark - 获取自定义行间隔富文本
- (NSAttributedString *)getAttributedStringWithString:(NSString *)text lineSpace:(CGFloat)lineSpace kern:(CGFloat)kern {
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    //调行行间距
    paragraphStyle.lineSpacing = lineSpace;
    paragraphStyle.alignment = NSTextAlignmentCenter;
    
    NSDictionary*attriDict =@{NSParagraphStyleAttributeName:paragraphStyle,NSKernAttributeName:@(kern),NSFontAttributeName:[UIFont systemFontOfSize:15],NSForegroundColorAttributeName:[UIColor colorWithHex:0x666666]};
    NSMutableAttributedString*attributedString = [[NSMutableAttributedString alloc]initWithString:text attributes:attriDict];
    return attributedString;
}


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

@end
