//
//  PreDeceptionAlertView.m
//  Open
//
//  Created by 雷俊博 on 2021/6/28.
//  Copyright © 2021 黄江涛 . All rights reserved.
//

#import "PreDeceptionAlertView.h"
#import <YYText.h>

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

@property (nonatomic, strong)PreDeceptionAlertViewClickBlock block;

@end

@implementation PreDeceptionAlertView
#pragma mark - life cycle
- (instancetype)initWithBlock:(PreDeceptionAlertViewClickBlock)block{
    self = [super init];
    if (self) {
        self.block = block;
        
        [self setupUI];
    }
    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.frame = CGRectMake(0, 0, 330 * WIDTHRADIU, 380 * WIDTHRADIU);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 15 * WIDTHRADIU;
    self.contentView.layer.masksToBounds = YES;
    
    
    //顶部图片
    UIImageView *topIcon = [[UIImageView alloc]init];
    topIcon.image = [UIImage imageNamed:@"zw_fpbg"];
    [self.contentView addSubview:topIcon];
    
    [topIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.offset(0);
        make.width.offset(330 * WIDTHRADIU);
        make.height.offset(80 * WIDTHRADIU);
    }];
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] init];
    [topIcon addSubview:titleLabel];
    titleLabel.font = [UIFont boldSystemFontOfSize:24 * WIDTHRADIU];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.text = @"防骗预警检测";
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(topIcon);
    }];
    
    
    
    UIView *view1 = [[UIView alloc]init];
    view1.backgroundColor = [UIColor colorWithHex:0x4c4c4c];
    view1.layer.cornerRadius = 3 * WIDTHRADIU;
    view1.layer.masksToBounds = YES;
    [self.contentView addSubview:view1];
    
    [view1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(55 * WIDTHRADIU);
        make.top.equalTo(topIcon.mas_bottom).offset(18 * WIDTHRADIU);
        make.width.height.offset(6 * WIDTHRADIU);
    }];
    
    UILabel *lb_content1 = [UILabel new];
    lb_content1.text = @"您的身份信息被盗用了吗？";
    lb_content1.textColor = [UIColor colorWithHex:0x4c4c4c];
    lb_content1.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    [self.contentView addSubview:lb_content1];
    
    [lb_content1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view1);
        make.left.equalTo(view1.mas_right).offset(8 * WIDTHRADIU);
    }];
    
    
    UIView *view2 = [[UIView alloc]init];
    view2.backgroundColor = [UIColor colorWithHex:0x4c4c4c];
    view2.layer.cornerRadius = 3 * WIDTHRADIU;
    view2.layer.masksToBounds = YES;
    [self.contentView addSubview:view2];
    
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.height.equalTo(view1);
        make.top.equalTo(view1.mas_bottom).offset(26 * WIDTHRADIU);
    }];
    
    UILabel *lb_content2 = [UILabel new];
    lb_content2.text = @"你被冒名办卡透支欠款了吗？";
    lb_content2.textColor = [UIColor colorWithHex:0x4c4c4c];
    lb_content2.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    [self.contentView addSubview:lb_content2];
    
    [lb_content2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view2);
        make.left.equalTo(view2.mas_right).offset(8 * WIDTHRADIU);
    }];
    
    UIView *view3 = [[UIView alloc]init];
    view3.backgroundColor = [UIColor colorWithHex:0x4c4c4c];
    view3.layer.cornerRadius = 3 * WIDTHRADIU;
    view3.layer.masksToBounds = YES;
    [self.contentView addSubview:view3];
    
    [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.height.equalTo(view1);
        make.top.equalTo(view2.mas_bottom).offset(26 * WIDTHRADIU);
    }];
    
    UILabel *lb_content3 = [UILabel new];
    lb_content3.text = @"你被牵扯进司法案件了吗？";
    lb_content3.textColor = [UIColor colorWithHex:0x4c4c4c];
    lb_content3.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    [self.contentView addSubview:lb_content3];
    
    [lb_content3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view3);
        make.left.equalTo(view3.mas_right).offset(8 * WIDTHRADIU);
    }];
    
    UIView *view4 = [[UIView alloc]init];
    view4.backgroundColor = [UIColor colorWithHex:0x4c4c4c];
    view4.layer.cornerRadius = 3 * WIDTHRADIU;
    view4.layer.masksToBounds = YES;
    [self.contentView addSubview:view4];
    
    [view4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.height.equalTo(view1);
        make.top.equalTo(view3.mas_bottom).offset(26 * WIDTHRADIU);
    }];
    
    UILabel *lb_content4 = [UILabel new];
    lb_content4.text = @"你被动成为涉案公司法人了吗？";
    lb_content4.textColor = [UIColor colorWithHex:0x4c4c4c];
    lb_content4.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    [self.contentView addSubview:lb_content4];
    
    [lb_content4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view4);
        make.left.equalTo(view4.mas_right).offset(8 * WIDTHRADIU);
    }];
    
    
    NSString *tempText = @"了解更多个人信息，预防个人风险，查询风险情况！";
    NSMutableAttributedString* text = [[NSMutableAttributedString alloc]initWithString:tempText];
    text.yy_font = [UIFont systemFontOfSize:13 * WIDTHRADIU];
    text.yy_color = [UIColor colorWithHex:0x1d7eea];
    text.yy_alignment = NSTextAlignmentCenter;
    
    [text yy_setTextHighlightRange:NSMakeRange(0, tempText.length) color:[UIColor colorWithHex:0x1d7eea] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        
        
    }];
       
       
    YYLabel *agreementLabel = [YYLabel new];
    agreementLabel.attributedText = text;
    [self.contentView addSubview:agreementLabel];
    [agreementLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(lb_content4.mas_bottom).offset(20 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
    
    
    UIButton *disAgreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [disAgreeBtn setTitle:@"不想检测" forState:UIControlStateNormal];
    [disAgreeBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    disAgreeBtn.titleLabel.font = [UIFont systemFontOfSize:16 * WIDTHRADIU];
    disAgreeBtn.layer.borderWidth = 0.5;
    disAgreeBtn.layer.borderColor = [UIColor colorWithHex:0x808080].CGColor;
    disAgreeBtn.layer.cornerRadius = 8 * WIDTHRADIU;
    [disAgreeBtn addTarget:self action:@selector(disAgreeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:disAgreeBtn];
    
   
    
    UIButton *ageeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [ageeBtn setTitle:@"立即检测" forState:UIControlStateNormal];
    [ageeBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    ageeBtn.titleLabel.font = [UIFont systemFontOfSize:16 * WIDTHRADIU];
    ageeBtn.backgroundColor = [UIColor colorWithHex:0x1d7eea];
    ageeBtn.layer.cornerRadius = 8 * WIDTHRADIU;
    [ageeBtn addTarget:self action:@selector(agreeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:ageeBtn];
    
    
    [disAgreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(18 * WIDTHRADIU);
        make.width.offset(130 * WIDTHRADIU);
        make.height.offset(40 * WIDTHRADIU);
        make.top.equalTo(agreementLabel.mas_bottom).offset(20 * WIDTHRADIU);
    }];
    
    [ageeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-18 * WIDTHRADIU);
        make.width.offset(155 * WIDTHRADIU);
        make.height.offset(40 * WIDTHRADIU);
        make.top.equalTo(agreementLabel.mas_bottom).offset(20 * WIDTHRADIU);
    }];
    
    UILabel *lb_from = [UILabel new];
    lb_from.text = @"本服务由天下信用提供";
    lb_from.textColor = [UIColor colorWithHex:0x808080];
    lb_from.font = [UIFont systemFontOfSize:12 * WIDTHRADIU];
    [self.contentView addSubview:lb_from];
    [lb_from mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(disAgreeBtn.mas_bottom).offset(20 * WIDTHRADIU);
    }];
    
    NSString *tempText2 = @"个人数据报告使用说明";
    NSDictionary *attribtDic = @{NSUnderlineStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString* text2 = [[NSMutableAttributedString alloc]initWithString:tempText2];
    text2.yy_font = [UIFont systemFontOfSize:13 * WIDTHRADIU];
    text2.yy_color = [UIColor colorWithHex:0x1d7eea];
    text2.yy_alignment = NSTextAlignmentCenter;
    [text2 yy_setAttributes:attribtDic];
    
    [text2 yy_setTextHighlightRange:NSMakeRange(0, tempText2.length) color:[UIColor colorWithHex:0x1d7eea] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        NSString *urlString = @"http://nginx.weface.com.cn/appH5/xy/index.html";
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:nil completionHandler:nil];
        
    }];
    
    
    
    YYLabel *agreementLabel2 = [YYLabel new];
    agreementLabel2.attributedText = text2;
    
    [self.contentView addSubview:agreementLabel2];
    [agreementLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_from.mas_bottom).offset(10 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
    
    

    
}


#pragma mark - 弹窗展示/隐藏

/** 弹出此弹窗 */
+(void)showWithBlock:(PreDeceptionAlertViewClickBlock)block
{
    PreDeceptionAlertView *alertView = [[PreDeceptionAlertView alloc]initWithBlock:block];
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    [keyWindow addSubview:alertView];
    
}
/** 移除此弹窗 */
- (void)dismiss {
    [self removeFromSuperview];
}




#pragma mark - event response
- (void)disAgreeBtnClick:(UIButton *)sender
{
    if (self.block) {
        self.block(201);
    }
    [self dismiss];
}

- (void)agreeBtnClick:(UIButton *)sender
{
    if (self.block) {
        self.block(200);
    }
    [self dismiss];
}





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

@end
