//
//  KKAlertView.m
//  Open
//
//  Created by 雷俊博 on 2020/12/17.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "KKAlertView.h"
#import "NSMutableAttributedString+TY.h"

@interface KKAlertView ()<UIGestureRecognizerDelegate>

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

@property (nonatomic,copy) NSString*   message;

@property (nonatomic,copy) KKAlertViewBtnBlock   block;

@end

@implementation KKAlertView
- (instancetype)initWithMessage:(NSString*)message Block:(KKAlertViewBtnBlock)block
{
    self = [super init];
    if (self) {
        self.message = message;
        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:)];
    [self addGestureRecognizer:tap];
    
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.bounds = CGRectMake(0, 0, 295, 180);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 15 * WIDTHRADIU;
    
    UILabel* lb_title = [UILabel new];
    lb_title.text = @"提示";
    lb_title.textAlignment = NSTextAlignmentCenter;
    lb_title.textColor = [UIColor colorWithHex:0x4d4d4d];
    lb_title.font = [UIFont boldSystemFontOfSize:17];
    [self.contentView addSubview:lb_title];
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(20);
        make.centerX.equalTo(self.contentView);
        make.height.offset(20);
        make.width.mas_greaterThanOrEqualTo(0);
    }];
    
    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);
        make.right.offset(-15);
        make.centerX.equalTo(self.contentView);
        make.width.offset(265);
        make.top.equalTo(lb_title.mas_bottom).offset(20);
        make.height.mas_greaterThanOrEqualTo(10);
    }];
    
    UIView* hLine = [UIView new];
    hLine.backgroundColor = [UIColor colorWithHex:0xcccccc];
    [self.contentView addSubview:hLine];
    [hLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(lb_message.mas_bottom).offset(20);
        make.height.offset(0.5);
    }];
    
    UIView* vLine = [UIView new];
    vLine.backgroundColor = [UIColor colorWithHex:0xcccccc];
    [self.contentView addSubview:vLine];
    [vLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(hLine.mas_bottom);
        make.height.offset(45);
        make.width.offset(0.5);
        make.bottom.greaterThanOrEqualTo(self.contentView);
    }];
    
    UIButton * leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBtn setTitle:@"转账攻略" forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.right.equalTo(vLine.mas_left);
        make.top.equalTo(hLine.mas_bottom);
        make.height.offset(45);
        make.bottom.equalTo(self.contentView).offset(0);
    }];
    
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setTitle:@"复制卡号" forState:UIControlStateNormal];
    [rightBtn setTitleColor:K_MainColor forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:rightBtn];
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(0);
        make.left.equalTo(vLine.mas_right);
        make.top.equalTo(hLine.mas_bottom);
        make.height.offset(45);
        make.bottom.equalTo(self.contentView).offset(0);
    }];
    
    
    
}

#pragma mark - 显示弹框
+ (void)showAlertViewWithVC:(UIViewController *)vc message:(NSString *)message block:(KKAlertViewBtnBlock)block
{
    KKAlertView*baseView = [[KKAlertView alloc]initWithMessage:message Block:block];
    UIWindow* keyWindow = vc.view.window;
    
    if (keyWindow == nil) {
        keyWindow = [UIApplication sharedApplication].keyWindow;
    }

    [keyWindow addSubview:baseView];
}

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

#pragma mark - event reseponse
- (void)leftBtnClick:(UIButton *)sender {
    
    if (self.block) {
        self.block(KKAlertViewTypeDetailText);
    }
    [self dismissUI];
    
}
- (void)rightBtnClick:(UIButton *)sender {
    if (self.block) {
        self.block(KKAlertViewTypeCopy);
    }
    [self dismissUI];
}
- (void)tapClick:(UITapGestureRecognizer *)ges {
    
    
    [self dismissUI];
}

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

#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:0x808080]};
    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
