//
//  PayAttentionToWXAlertView.m
//  Open
//
//  Created by 雷俊博 on 2019/12/6.
//  Copyright © 2019 黄江涛 . All rights reserved.
//

#import "PayAttentionToWXAlertView.h"
#import "UIView+frameAdjust.h"

@interface PayAttentionToWXAlertView ()
{
    CGFloat myScale;
}

@end

@implementation PayAttentionToWXAlertView
#pragma mark - life cycle
#pragma mark - 构造方法
- (instancetype)init:(AttentionBlcok)block
{
    self = [super init];
    if (self) {
        self.block = block;
         myScale = WIDTHRADIU *1.1;
        //UI搭建
        [self initUI];
        
       
         
    }
    return self;
}
- (void)initUI
{
    self.frame = ScreenBounds;
    // 背景遮罩图层
    [self addSubview:self.backgroundView];
    // 弹出视图
    [self addSubview:self.alertView];
    
    UIButton *btn_close = [UIButton buttonWithType:UIButtonTypeCustom];
    
    btn_close.frame = CGRectMake(self.alertView.maxX , self.alertView.minY  - 27 , 30, 30);
    
    [btn_close setImage:[UIImage imageNamed:@"btn_lottery_close"] forState:UIControlStateNormal];
    
    btn_close.alpha = 0.4;
    
    [btn_close addTarget:self action:@selector(closeViewClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:btn_close];
    
    
    
}

- (void)dealloc
{
    NSLog(@"PayAttentionToWXAlertView被销毁了");
}

#pragma mark - public methods
+ (void)showAlertView:(AttentionBlcok)block
{
    PayAttentionToWXAlertView *alertView = [[PayAttentionToWXAlertView alloc]init:block];
    
    [alertView show];
}

#pragma mark - event response
-(void)didTapBackgroundView:(UITapGestureRecognizer *)sender
{
}

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

- (void)closeViewClick:(UIButton *)sender
{
    [self dismiss];
}

#pragma mark - 弹窗展示/隐藏

/** 弹出此弹窗 */
- (void)show {
    // 出场动画
    self.alpha = 0;
    self.alertView.transform = CGAffineTransformScale(self.alertView.transform, 1.3, 1.3);
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 1;
        self.alertView.transform = CGAffineTransformIdentity;
    }];
    
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window addSubview:self];
}

/** 移除此弹窗 */
- (void)dismiss {
    
    [self removeFromSuperview];
}

#pragma mark - getters and setters
- (UIView *)backgroundView
{
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc]initWithFrame:SCREEN_BOUNDS];
        _backgroundView.backgroundColor = [UIColor blackColor] ;
        _backgroundView.alpha = 0.3f ;
        _backgroundView.userInteractionEnabled = YES;
        UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapBackgroundView:)];
        [_backgroundView addGestureRecognizer:myTap];
    }
    return _backgroundView;
}
- (UIView *)alertView
{
    if (!_alertView) {
        _alertView = [[UIView alloc]initWithFrame:CGRectMake((ScreenWidth - WhiteViewWidth)/2, (ScreenHeight - WhiteViewHeight)/2 - 20, WhiteViewWidth, WhiteViewHeight)];
        _alertView.backgroundColor = [UIColor whiteColor];
        _alertView.layer.cornerRadius = 10 *self->myScale;
        [_alertView addSubview:self.topImg];
        [_alertView addSubview:self.titleLabel];
        
        [_alertView addSubview:self.qrCodeImg];
        [_alertView addSubview:self.btn_attention];
        
        
        [self.topImg mas_makeConstraints:^(MASConstraintMaker *make) {
                  make.top.left.right.offset(0);
            make.height.offset(104 * self->myScale);
              }];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.offset(12* WIDTHRADIU);
            make.left.right.offset(0);
        }];
        
        [self.qrCodeImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).offset(8* self->myScale);
                make.centerX.offset(0);
            make.width.height.offset(75* self->myScale);
        }];
        
        [self.btn_attention mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.qrCodeImg.mas_bottom).offset(8* self->myScale);
            make.left.offset(15);
            make.right.offset(-15);
            make.height.offset(38* self->myScale);
        }];
        
      
        
    }
    return _alertView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont systemFontOfSize:17.0f* myScale];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.numberOfLines = 0;
        NSString *str = @"关注“看看智能”公众号\n实时接收认证通知";
        NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:str];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        paragraphStyle.lineSpacing = 6.0; // 设置行间距
        paragraphStyle.alignment = NSTextAlignmentCenter; //设置两端对齐显示
        [attributedStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, attributedStr.length)];
        NSRange range = NSMakeRange(0, str.length);
        // 设置字体大小
        [attributedStr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:17] range:range];
         [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:range];
        _titleLabel.attributedText = attributedStr;
    }
    return _titleLabel;
}

- (UIImageView *)topImg
{
    if (!_topImg) {
        _topImg = [[UIImageView alloc]init];
        _topImg.image = [UIImage imageNamed:@"authentication_bg"];
    }
    return _topImg;
}

- (UIImageView *)qrCodeImg
{
    if (!_qrCodeImg) {
        _qrCodeImg = [[UIImageView alloc]init];
        _qrCodeImg.image = [UIImage imageNamed:@"authentication_qrcode_icon"];
    }
    return _qrCodeImg;
}

- (UIButton *)btn_attention
{
    if (!_btn_attention) {
        _btn_attention = [UIButton buttonWithType:UIButtonTypeCustom];
        _btn_attention.backgroundColor = K_MainColor;
        _btn_attention.layer.cornerRadius = 19* myScale;
        [_btn_attention setTitle:@"立即关注" forState:UIControlStateNormal];
        [_btn_attention setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _btn_attention.titleLabel.font = [UIFont systemFontOfSize:18* myScale];
        [_btn_attention addTarget:self action:@selector(payAttentionClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn_attention;
}

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

@end
