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

#import "PayAttention2ToWXAlertView.h"

@interface PayAttention2ToWXAlertView ()

@property (nonatomic, strong)UIView *contentView;

@property (nonatomic, copy)NSString *title;

@property (nonatomic, copy)NSString *imageName;

@property (nonatomic, copy)PayAttention2ToWXAlertViewBlock block;

@end

@implementation PayAttention2ToWXAlertView
- (instancetype)initWithTitle:(NSString *)title imageName:(NSString *)imageName Block:(PayAttention2ToWXAlertViewBlock)block
{
    self = [super init];
    if (self) {
        self.title = title;
        self.imageName = imageName;
        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*WIDTHRADIU, 125 * WIDTHRADIU);
    self.contentView.center = CGPointMake(self.center.x, self.center.y + 30* WIDTHRADIU);
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 10 * WIDTHRADIU;
    
    
    //closeBtn
    
    UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn setImage:[UIImage imageNamed:@"btn_lottery_close"] forState:UIControlStateNormal];
    [closeBtn addTarget:self action:@selector(closeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:closeBtn];
    
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.contentView.mas_bottom).offset(20 * WIDTHRADIU);
        make.width.height.offset(40 * WIDTHRADIU);
    }];
    
    
    //顶部图片
    UIImageView*qrImg = [[UIImageView alloc]init];
    //qrImg.image = [UIImage imageNamed:@"authentication_qrcode_icon"];
    qrImg.image = [UIImage imageNamed:self.imageName];
    [self.contentView addSubview:qrImg];
    [qrImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(18 * WIDTHRADIU);
        make.centerY.equalTo(self.contentView);
        make.width.height.offset(89 * WIDTHRADIU);
    }];
    
    
    
    
    //标题
    UILabel*lb_title = [UILabel new];
    //lb_title.attributedText = [self getAttributedStringWithString:@"关注“看看认证”公众号\n实时接收认证通知" lineSpace:5 * WIDTHRADIU kern:0.5];
    lb_title.attributedText = [self getAttributedStringWithString:self.title lineSpace:5 * WIDTHRADIU kern:0.5];
    lb_title.textColor = [UIColor colorWithHex:0x666666];
    lb_title.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    lb_title.numberOfLines = 0;
    lb_title.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:lb_title];
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(qrImg.mas_right).offset(15 * WIDTHRADIU);
        make.top.equalTo(qrImg.mas_top).offset(2 * WIDTHRADIU);
    }];
    
   
    UIButton* saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [saveBtn setTitle:@"立即关注" forState:UIControlStateNormal];
    [saveBtn  setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    saveBtn.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    [saveBtn addTarget:self action:@selector(saveBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    saveBtn.backgroundColor = K_MainColor;
    saveBtn.layer.cornerRadius = 5 * WIDTHRADIU;
    [self.contentView addSubview:saveBtn];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(128 * WIDTHRADIU);
        make.height.offset(34 * WIDTHRADIU);
        make.left.equalTo(lb_title);
        make.bottom.equalTo(qrImg);
    }];
    
    
    
    
}


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


+ (void)showAlertViewWithTitle:(NSString *)title imageName:(NSString *)imageName Block:(PayAttention2ToWXAlertViewBlock)block
{
    PayAttention2ToWXAlertView *alertView = [[PayAttention2ToWXAlertView alloc]initWithTitle:title imageName:imageName Block:block];
    
    UIWindow* keyWindow = [UIApplication sharedApplication].keyWindow;
    
    [keyWindow addSubview:alertView];
}


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



#pragma mark - event response
- (void)closeBtnClick:(UIButton *)sender {
    [self dismissUI];
}
- (void)saveBtnClick:(UIButton *)sender {
    if (self.block) {
        self.block();
    }
    
    [self dismissUI];
}
- (void)tapClick:(UITapGestureRecognizer *)ges {
    //[self dismissUI];
}


#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 * WIDTHRADIU],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
