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

#import "ClickOnForCoinsAlertView.h"

@interface ClickOnForCoinsAlertView ()

@property (nonatomic, strong)UIView *contentView;

@property (nonatomic, copy)ClickOnForCoinsAlertViewClickBlock block;

@property (nonatomic, copy)ClickExchangBlock ExchangBlock;
@end

@implementation ClickOnForCoinsAlertView
- (instancetype)initWithBlock:(ClickOnForCoinsAlertViewClickBlock)block ExchangBlock:(ClickExchangBlock)ExchangBlock
{
    self = [super init];
    if (self) {
        self.block = block;
        self.ExchangBlock = ExchangBlock;
        [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:)];
//    tap.delegate = self;
//    [self addGestureRecognizer:tap];
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.bounds = CGRectMake(0, 0, 280 * WIDTHRADIU, 275 * WIDTHRADIU);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 10 * WIDTHRADIU;
    
    //icon
    UIImageView *topBg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 280 * WIDTHRADIU, 90 * WIDTHRADIU)];
    topBg.image = [UIImage imageNamed:@"daKaCoin_bg"];
    [self.contentView addSubview:topBg];
    
//    //title
//    UILabel *lb_title = [UILabel new];
//    lb_title.text = @"打卡领金币";
//    lb_title.textColor = [UIColor whiteColor];
//    lb_title.font = [UIFont boldSystemFontOfSize:20 * WIDTHRADIU];
//    [topBg addSubview:lb_title];
//    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.offset(20 * WIDTHRADIU);
//        make.centerX.equalTo(topBg);
//    }];
    
    //message
    UILabel *lb_message = [UILabel new];
    lb_message.numberOfLines = 0;
    lb_message.attributedText = [self getAttributedWithString:@"今日未打卡\n连续打卡可领取更多金币" WithLineSpace:8* WIDTHRADIU kern:0.5 font:[UIFont systemFontOfSize:15 * WIDTHRADIU]];
    lb_message.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:lb_message];
    [lb_message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topBg.mas_bottom).offset(12 * WIDTHRADIU);
        make.centerX.equalTo(topBg);
    }];
    
    //date
    UIImageView *dateImageView = [[UIImageView alloc]init];
    dateImageView.image = [UIImage imageNamed:@"daKaCoin_date"];
    [self.contentView addSubview:dateImageView];
    
    [dateImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_message.mas_bottom).offset(18 * WIDTHRADIU);
        make.centerX.equalTo(topBg);
        make.width.offset(246 * WIDTHRADIU);
        make.height.offset(35 * WIDTHRADIU);
    }];
    
    
    //button
    UIButton *signBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [signBtn setTitle:@"立即打卡" forState:UIControlStateNormal];
    [signBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    signBtn.titleLabel.font = [UIFont systemFontOfSize:16 * WIDTHRADIU];
    signBtn.layer.cornerRadius = 20 * WIDTHRADIU;
    signBtn.layer.masksToBounds = YES;
    [signBtn setBackgroundColor:[UIColor colorWithHex:0xf98505]];
    [signBtn addTarget:self action:@selector(signBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:signBtn];
    [signBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dateImageView.mas_bottom).offset(18 * WIDTHRADIU);
        make.centerX.equalTo(topBg);
        make.width.offset(240 * WIDTHRADIU);
        make.height.offset(40 * WIDTHRADIU);
    }];
    
//    UIButton *exchangBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [exchangBtn setTitle:@"兑换好物>>" forState:UIControlStateNormal];
//    [exchangBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
//    exchangBtn.titleLabel.font = [UIFont systemFontOfSize:12.0];
//    [exchangBtn addTarget:self action:@selector(exchangBtn:) forControlEvents:UIControlEventTouchUpInside];
//    [self.contentView addSubview:exchangBtn];
//    [exchangBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(signBtn.mas_bottom).offset(5);
//        make.centerX.equalTo(signBtn);
//        make.width.offset(240 * WIDTHRADIU);
//        make.height.offset(30 * WIDTHRADIU);
//    }];
    
    
    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.top.equalTo(self.contentView.mas_bottom).offset(10 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
        make.width.height.offset(60 * WIDTHRADIU);
        
    }];
    
    
}

+ (void)showViewWithBlock:(ClickOnForCoinsAlertViewClickBlock)block ExchangBlock:(ClickExchangBlock)ExchangBlock
{
    ClickOnForCoinsAlertView *alertView = [[ClickOnForCoinsAlertView alloc]initWithBlock:block ExchangBlock:ExchangBlock];
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    [keyWindow addSubview:alertView];
}


#pragma mark - event response
- (void)signBtnClick:(UIButton *)sender
{
    if (self.block) {
        self.block();
    }
    
    [self dismissUI];
}
#pragma mark - 兑换礼物
- (void)exchangBtn:(UIButton *)button{
    if (self.ExchangBlock) {
        self.ExchangBlock();
    }
    [self dismissUI];
}
- (void)closeBtnClick:(UIButton *)sender
{
    [self dismissUI];
}

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


#pragma mark - 富文本
- (NSAttributedString *)getAttributedWithString:(NSString *)string WithLineSpace:(CGFloat)lineSpace kern:(CGFloat)kern font:(UIFont *)font{
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    
    paragraphStyle.alignment = NSTextAlignmentCenter;
    
    //调整行间距
    paragraphStyle.lineSpacing = lineSpace;
    NSDictionary *attriDict = @{NSParagraphStyleAttributeName:paragraphStyle,NSKernAttributeName:@(kern),
                                NSFontAttributeName:font,NSForegroundColorAttributeName:[UIColor colorWithHex:0x808080]};
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:string 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
