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

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

@interface KKPrivateAlertView ()

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

@property (nonatomic, strong)KKPrivateAlertViewClickBlock block;

@end

@implementation KKPrivateAlertView
#pragma mark - life cycle
/**
 申报异常弹窗的构造方法
 
 @param title 弹窗标题
 @param message 弹窗message
 @param delegate 确定代理方
 @param leftButtonTitle 左边按钮的title
 @param rightButtonTitle 右边按钮的title
 @return 一个申报异常的弹窗
 */
- (instancetype)initWithBlock:(KKPrivateAlertViewClickBlock)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, 326 * WIDTHRADIU, 445 * 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;
    
    // 标题
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, self.contentView.width, 22)];
    [self.contentView addSubview:titleLabel];
    titleLabel.font = [UIFont boldSystemFontOfSize:17 * WIDTHRADIU];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor colorWithHex:0x333333];
    titleLabel.text = @"用户协议、隐私条款及权限说明";
    
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(23 * WIDTHRADIU);
        make.left.offset(18 * WIDTHRADIU);
        make.right.offset(- 18 * WIDTHRADIU);
    }];
    
    
    
    //副标题
    UILabel *subTitleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:subTitleLabel];
    subTitleLabel.font = [UIFont boldSystemFontOfSize:14 * WIDTHRADIU];
    subTitleLabel.textAlignment = NSTextAlignmentCenter;
    subTitleLabel.textColor = [UIColor colorWithHex:0x464646];
    subTitleLabel.numberOfLines = 0;
    subTitleLabel.attributedText = [self getAttributed2StringWithString:@"我们依据最新的法律，向您说明看看生活软件的隐私政策，特向您推送本提示。请您阅读并充分理解相关条款。" font:[UIFont boldSystemFontOfSize:14 * WIDTHRADIU] color:[UIColor colorWithHex:0x464646] lineSpace:6 * WIDTHRADIU];
    
    [subTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleLabel.mas_bottom).offset(18 * WIDTHRADIU);
        make.left.right.equalTo(titleLabel);
    }];
    
    
    
    UILabel *lb_head = [UILabel new];
    [self.contentView addSubview:lb_head];
    lb_head.font = [UIFont boldSystemFontOfSize:14 * WIDTHRADIU];
    lb_head.textAlignment = NSTextAlignmentLeft;
    lb_head.textColor = [UIColor colorWithHex:0x333333];
    lb_head.text = @"我们承诺:";
    [lb_head mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(subTitleLabel.mas_bottom).offset(18 * WIDTHRADIU);
        make.left.equalTo(titleLabel);
    }];
    
    UIImageView *insuranceIcon = [[UIImageView alloc]init];
    insuranceIcon.image = [UIImage imageNamed:@"alert_baozhang"];
    [self.contentView addSubview:insuranceIcon];
    [insuranceIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_head.mas_bottom).offset(18 * WIDTHRADIU);
        make.left.equalTo(lb_head);
        make.width.height.offset(50 * WIDTHRADIU);
    }];
    
    UILabel *lb_message1 = [UILabel new];
    [self.contentView addSubview:lb_message1];
    lb_message1.font = [UIFont systemFontOfSize:11 * WIDTHRADIU];
    lb_message1.textAlignment = NSTextAlignmentLeft;
    lb_message1.textColor = [UIColor colorWithHex:0x666666];
    lb_message1.numberOfLines = 0;
    lb_message1.attributedText = [self getAttributed2StringWithString:@"我们会严格按照《网络安全法》、《信息传播保护条例》等保护您的个人信息" font:[UIFont systemFontOfSize:11 * WIDTHRADIU] color:[UIColor colorWithHex:0x666666] lineSpace:5 * WIDTHRADIU];
    [lb_message1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(insuranceIcon);
        make.left.equalTo(insuranceIcon.mas_right).offset(15 * WIDTHRADIU);
        make.right.equalTo(titleLabel);
    }];
    
    
    
    UIImageView *personIcon = [[UIImageView alloc]init];
    personIcon.image = [UIImage imageNamed:@"alert_person"];
    [self.contentView addSubview:personIcon];
    [personIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(insuranceIcon.mas_bottom).offset(15 * WIDTHRADIU);
        make.left.equalTo(lb_head);
        make.width.height.offset(50 * WIDTHRADIU);
    }];
    
    UILabel *lb_message2 = [UILabel new];
    [self.contentView addSubview:lb_message2];
    lb_message2.font = [UIFont systemFontOfSize:11 * WIDTHRADIU];
    lb_message2.textAlignment = NSTextAlignmentLeft;
    lb_message2.textColor = [UIColor colorWithHex:0x666666];
    lb_message2.numberOfLines = 0;
    lb_message2.attributedText = [self getAttributed2StringWithString:@"如果未经您的授权，我们不会使用您的个人信息用于您未授权的其他途径或目的。" font:[UIFont systemFontOfSize:11 * WIDTHRADIU] color:[UIColor colorWithHex:0x666666] lineSpace:5 * WIDTHRADIU];
    [lb_message2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(personIcon);
        make.left.equalTo(personIcon.mas_right).offset(15 * WIDTHRADIU);
        make.right.equalTo(titleLabel);
    }];
    
    
    UILabel *lb_message = [UILabel new];
    [self.contentView addSubview:lb_message];
    lb_message.font = [UIFont systemFontOfSize:13 * WIDTHRADIU];
    lb_message.textAlignment = NSTextAlignmentLeft;
    lb_message.textColor = [UIColor colorWithHex:0x4c4c4c];
    lb_message.numberOfLines = 0;
    lb_message.attributedText = [self getAttributed2StringWithString:@"您点击“同意”，即表示您已阅读并同意更新后的《服务协议》及《隐私协议》" font:[UIFont systemFontOfSize:13 * WIDTHRADIU] color:[UIColor colorWithHex:0x4c4c4c] lineSpace:5 * WIDTHRADIU];
    [lb_message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(personIcon.mas_bottom).offset(18 * WIDTHRADIU);
        make.left.right.equalTo(titleLabel);
    }];
    
    
    NSMutableAttributedString* text = [[NSMutableAttributedString alloc]initWithString:@"查看完整版《服务协议》及《隐私协议》"];
    text.yy_font = [UIFont systemFontOfSize:13 * WIDTHRADIU];
    text.yy_color = [UIColor colorWithHex:0x666666];
    
    [text yy_setTextHighlightRange:NSMakeRange(5, 6) color:K_MainColor backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        
        if (self.block) {
            self.block(203);
        }
        
    }];
       
       
    [text yy_setTextHighlightRange:NSMakeRange(12, 6) color:K_MainColor backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {

        if (self.block) {
            self.block(204);
        }
       
    }];
    
    
    YYLabel *agreementLabel = [YYLabel new];
    agreementLabel.attributedText = text;
    [self.contentView addSubview:agreementLabel];
    [agreementLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(lb_message.mas_bottom).offset(20 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
    
    UIView *btnView = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.contentView.bounds) - 50 * WIDTHRADIU, CGRectGetWidth(self.contentView.bounds), 50 * WIDTHRADIU)];
    btnView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:btnView];
    [self addSingleSidesShadowToView:btnView withColor:[UIColor blackColor]];
    UIButton *disAgreeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [disAgreeBtn setTitle:@"不同意" forState:UIControlStateNormal];
    [disAgreeBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    disAgreeBtn.titleLabel.font = [UIFont systemFontOfSize:16 * WIDTHRADIU];
    [disAgreeBtn addTarget:self action:@selector(disAgreeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btnView addSubview:disAgreeBtn];
    
    UILabel *lb_agreeText = [UILabel new];
    lb_agreeText.backgroundColor = K_MainColor;
    lb_agreeText.numberOfLines = 0;
    lb_agreeText.textColor = [UIColor whiteColor];
    lb_agreeText.attributedText = [self getAttributed3StringWithString];
    lb_agreeText.textAlignment = NSTextAlignmentCenter;
    [btnView addSubview:lb_agreeText];
    
    UIButton *ageeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [ageeBtn addTarget:self action:@selector(agreeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [btnView addSubview:ageeBtn];
    
    
    [disAgreeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(btnView);
        make.width.equalTo(btnView).multipliedBy(0.5);
    }];
    
    [lb_agreeText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(disAgreeBtn.mas_right);
        make.right.top.bottom.equalTo(btnView);
    }];
    [ageeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(lb_agreeText);
    }];
    
    

    
}

#pragma mark - 弹窗展示/隐藏

/** 弹出此弹窗 */
+(void)showWithBlock:(KKPrivateAlertViewClickBlock)block
{
    KKPrivateAlertView *alertView = [[KKPrivateAlertView 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];
}

#pragma mark - 在顶部添加阴影
//在顶部添加阴影
-(void)addSingleSidesShadowToView:(UIView *)theView withColor:(UIColor*)theColor{
    //阴影颜色
    theView.layer.shadowColor = theColor.CGColor;
    //阴影偏移
    theView.layer.shadowOffset = CGSizeMake(0, 0 );
    //阴影透明度，默认0
    theView.layer.shadowOpacity = 0.3;
    //阴影半径，默认3
    theView.layer.shadowRadius = 3;
    //单边阴影
    CGFloat shadowPathWidth = theView.layer.shadowRadius;
    CGRect shadowRect = CGRectMake(0, 0-shadowPathWidth/2, theView.bounds.size.width, shadowPathWidth);
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:shadowRect];
    theView.layer.shadowPath = path.CGPath;
}

#pragma mark - 富文本

-(NSAttributedString *)getAttributed3StringWithString {
    
    NSString *string = @"已阅读并同意\n《服务协议》及《隐私协议》";
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = 5 * WIDTHRADIU; // 调整行间距
    paragraphStyle.alignment = NSTextAlignmentCenter;
    NSRange range = NSMakeRange(0, [string length]);
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
    [attributedString addAttribute:NSForegroundColorAttributeName
     
                          value:[UIColor whiteColor]
     
                          range:NSMakeRange(0, [string length])];
    [attributedString addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:16 * WIDTHRADIU]
     
                          range:NSMakeRange(0, 6)];
    
    [attributedString addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:10 * WIDTHRADIU]
     
                          range:NSMakeRange(7, 13)];
    

    return attributedString;
}


-(NSAttributedString *)getAttributed2StringWithString:(NSString *)string font:(UIFont *)font color:(UIColor *)color lineSpace:(CGFloat)lineSpace {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpace; // 调整行间距
    NSRange range = NSMakeRange(0, [string length]);
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
    [attributedString addAttribute:NSForegroundColorAttributeName
     
                          value:color
     
                          range:range];
    [attributedString addAttribute:NSFontAttributeName
     
                          value:font
     
                          range:range];
    

    return attributedString;
}

-(NSAttributedString *)getAttributedStringWithString:(NSString *)string lineSpace:(CGFloat)lineSpace {
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = lineSpace; // 调整行间距
    NSRange range = NSMakeRange(0, [string length]);
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:range];
    [attributedString addAttribute:NSForegroundColorAttributeName
     
                          value:[UIColor colorWithHex:0x808080]
     
                          range:range];
    [attributedString addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:13]
     
                          range:range];
    

    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
