//
//  RewardTimeAlertView.m
//  Open
//
//  Created by 雷俊博 on 2020/5/9.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "RewardTimeAlertView.h"

@interface RewardTimeAlertView ()

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

@property (nonatomic, copy)NSString* imageName;

@property (nonatomic, strong)UILabel*lb_firstNumber;

@property (nonatomic, strong)UILabel*lb_secondNumber;

@property (nonatomic, strong)UILabel*lb_thirdNumber;

@property (nonatomic, strong)UILabel*lb_fourNumber;

@property (nonatomic, assign)NSInteger count;

@end

@implementation RewardTimeAlertView
- (instancetype)initWithImageName:(NSString *)imageName count:(NSInteger)count
{
    self = [super init];
    if (self) {
        self.count = count;
        self.imageName = imageName;
        [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, 245*WIDTHRADIU, 260 * WIDTHRADIU);
     self.contentView.center = self.center;
     [self addSubview:self.contentView];
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 15 * WIDTHRADIU;
    
    //顶部背景图
    UIImageView* topImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 245 * WIDTHRADIU, 75 * WIDTHRADIU)];
    topImg.image = [UIImage imageNamed:@"rewardAlertbg"];
    
    [self.contentView addSubview:topImg];
    
    
    //头像
    UIImageView* iconImg = [[UIImageView alloc]init];
    iconImg.image = [UIImage imageNamed:self.imageName];
    [self.contentView addSubview:iconImg];
    
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(21 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
        make.width.height.offset(80 * WIDTHRADIU);
    }];
    
    //message
    UILabel* lb_message = [[UILabel alloc]init];
    lb_message.textColor = [UIColor colorWithHex:0x333333];
    lb_message.font = [UIFont systemFontOfSize:16 * WIDTHRADIU];
    lb_message.textAlignment = NSTextAlignmentCenter;
    lb_message.text = @"休息一段时间再来吧~";
    [self.contentView addSubview:lb_message];
    
    [lb_message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconImg.mas_bottom).offset(13 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
    
    UIImageView*lb_maohao = [[UIImageView alloc]init];
    lb_maohao.image = [UIImage imageNamed:@"rewardMaoHao"];
    
    [self.contentView addSubview:lb_maohao];
    
    [lb_maohao mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.width.offset(14 * WIDTHRADIU);
        make.height.offset(44 * WIDTHRADIU);
        make.top.equalTo(lb_message.mas_bottom).offset(18 * WIDTHRADIU);
    }];
    
    _lb_secondNumber = [[UILabel alloc]init];
    _lb_secondNumber.backgroundColor = [UIColor colorWithHex:0x1294e5];
    _lb_secondNumber.textColor = [UIColor whiteColor];
    _lb_secondNumber.font = [UIFont boldSystemFontOfSize:35 * WIDTHRADIU];
    _lb_secondNumber.layer.cornerRadius = 6 * WIDTHRADIU;
    _lb_secondNumber.layer.masksToBounds = YES;
    _lb_secondNumber.textAlignment = NSTextAlignmentCenter;
    _lb_secondNumber.text = @"0";
    
    [self.contentView addSubview:_lb_secondNumber];
    
    [_lb_secondNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lb_maohao);
        make.right.equalTo(lb_maohao.mas_left);
        make.width.offset(35 * WIDTHRADIU);
        make.height.offset(44 * WIDTHRADIU);
    }];
    
    _lb_firstNumber = [[UILabel alloc]init];
    _lb_firstNumber.backgroundColor = [UIColor colorWithHex:0x1294e5];
    _lb_firstNumber.textColor = [UIColor whiteColor];
    _lb_firstNumber.font = [UIFont boldSystemFontOfSize:35 * WIDTHRADIU];
    _lb_firstNumber.layer.cornerRadius = 6 * WIDTHRADIU;
    _lb_firstNumber.layer.masksToBounds = YES;
    _lb_firstNumber.textAlignment = NSTextAlignmentCenter;
    _lb_firstNumber.text = @"0";
    
    [self.contentView addSubview:_lb_firstNumber];
    
    [_lb_firstNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lb_maohao);
        make.right.equalTo(self->_lb_secondNumber.mas_left).offset(- 5 * WIDTHRADIU);
        make.width.offset(35 * WIDTHRADIU);
        make.height.offset(44 * WIDTHRADIU);
    }];
    
    _lb_thirdNumber = [[UILabel alloc]init];
    _lb_thirdNumber.backgroundColor = [UIColor colorWithHex:0x1294e5];
    _lb_thirdNumber.textColor = [UIColor whiteColor];
    _lb_thirdNumber.font = [UIFont boldSystemFontOfSize:35 * WIDTHRADIU];
    _lb_thirdNumber.layer.cornerRadius = 6 * WIDTHRADIU;
    _lb_thirdNumber.layer.masksToBounds = YES;
    _lb_thirdNumber.textAlignment = NSTextAlignmentCenter;
    _lb_thirdNumber.text = @"0";
    
    [self.contentView addSubview:_lb_thirdNumber];
    
    [_lb_thirdNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lb_maohao);
        make.left.equalTo(lb_maohao.mas_right);
        make.width.offset(35 * WIDTHRADIU);
        make.height.offset(44 * WIDTHRADIU);
    }];
    
    _lb_fourNumber = [[UILabel alloc]init];
    _lb_fourNumber.backgroundColor = [UIColor colorWithHex:0x1294e5];
    _lb_fourNumber.textColor = [UIColor whiteColor];
    _lb_fourNumber.font = [UIFont boldSystemFontOfSize:35 * WIDTHRADIU];
    _lb_fourNumber.layer.cornerRadius = 6 * WIDTHRADIU;
    _lb_fourNumber.layer.masksToBounds = YES;
    _lb_fourNumber.textAlignment = NSTextAlignmentCenter;
    _lb_fourNumber.text = @"0";
    
    [self.contentView addSubview:_lb_fourNumber];
    
    [_lb_fourNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lb_maohao);
        make.left.equalTo(self->_lb_thirdNumber.mas_right).offset(5 * WIDTHRADIU);
        make.width.offset(35 * WIDTHRADIU);
        make.height.offset(44 * WIDTHRADIU);
    }];
    
    
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"好的" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    btn.backgroundColor = [UIColor colorWithHex:0x1294e5];
    btn.layer.cornerRadius = 20 *WIDTHRADIU;
    btn.layer.masksToBounds = YES;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(self->_lb_fourNumber.mas_bottom).offset(15 * WIDTHRADIU);
        make.width.offset(215 * WIDTHRADIU);
        make.height.offset(40 * WIDTHRADIU);
    }];
    
    
    [self startCount:_count];
    
}

- (instancetype)initWithImageName2:(NSString *)imageName
{
    self = [super init];
    if (self) {
        self.imageName = imageName;
        [self setupUI2];
    }
    return self;
}


- (void)setupUI2
{
    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, 245*WIDTHRADIU, 260 * WIDTHRADIU);
     self.contentView.center = self.center;
     [self addSubview:self.contentView];
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 15 * WIDTHRADIU;
    
    //顶部背景图
    UIImageView* topImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 245 * WIDTHRADIU, 75 * WIDTHRADIU)];
    topImg.image = [UIImage imageNamed:@"rewardAlertbg"];
    
    [self.contentView addSubview:topImg];
    
    
    //头像
    UIImageView* iconImg = [[UIImageView alloc]init];
    iconImg.image = [UIImage imageNamed:self.imageName];
    [self.contentView addSubview:iconImg];
    
    [iconImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(21 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
        make.width.height.offset(80 * WIDTHRADIU);
    }];
    
    //message
    UILabel* lb_message = [[UILabel alloc]init];
    lb_message.textColor = [UIColor colorWithHex:0x333333];
    lb_message.font = [UIFont systemFontOfSize:16 * WIDTHRADIU];
    lb_message.textAlignment = NSTextAlignmentCenter;
    lb_message.numberOfLines = 0;
    //lb_message.text = @"休息一段时间再来吧~";
    
    NSString*str = @"今日次数已达上限\n不要太贪心，明天再来呦~";
    NSAttributedString* message =[self getAttributedWithString:str WithLineSpace:6 * WIDTHRADIU kern:0.5 font:[UIFont systemFontOfSize:16 * WIDTHRADIU]];
    lb_message.attributedText = message;
    
    
    [self.contentView addSubview:lb_message];
    
    [lb_message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-88 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
    
    
    
    UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"好的" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    btn.backgroundColor = [UIColor colorWithHex:0x1294e5];
    btn.layer.cornerRadius = 20 *WIDTHRADIU;
    btn.layer.masksToBounds = YES;
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:btn];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.bottom.offset(-10 * WIDTHRADIU);
        make.width.offset(215 * WIDTHRADIU);
        make.height.offset(40 * WIDTHRADIU);
    }];
    

    
}


- (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:0x333333]};
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:string attributes:attriDict];
    return attributedString;
}

#pragma mark - 倒计时


- (void)startCount:(NSInteger)timeCount
{
    __block NSInteger second = timeCount;
    //(1)
    dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //(2)
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, quene);
    //(3)
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    //(4)
    dispatch_source_set_event_handler(timer, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (second == 0) {
                
                NSInteger mm = second /60;
                
                NSInteger ss = second % 60;
                
                NSInteger number1 = mm /10;
                
                NSInteger number2 = mm %10;
                
                NSInteger number3 = ss /10;
                
                NSInteger number4 = ss %10;
                
                self.lb_firstNumber.text = [NSString stringWithFormat:@"%ld",number1];
                self.lb_secondNumber.text = [NSString stringWithFormat:@"%ld",number2];
                self.lb_thirdNumber.text = [NSString stringWithFormat:@"%ld",number3];
                self.lb_fourNumber.text = [NSString stringWithFormat:@"%ld",number4];
                
               
                
                //(6)
                dispatch_cancel(timer);
                [Tools showMessage:@"可以抽奖啦!"];
                [self dismissUI];
            } else {
                NSInteger mm = second /60;
                
                NSInteger ss = second % 60;
                
                NSInteger number1 = mm /10;
                
                NSInteger number2 = mm %10;
                
                NSInteger number3 = ss /10;
                
                NSInteger number4 = ss %10;
                
                self.lb_firstNumber.text = [NSString stringWithFormat:@"%ld",number1];
                self.lb_secondNumber.text = [NSString stringWithFormat:@"%ld",number2];
                self.lb_thirdNumber.text = [NSString stringWithFormat:@"%ld",number3];
                self.lb_fourNumber.text = [NSString stringWithFormat:@"%ld",number4];
                
                
                second--;
            }
        });
    });
    //(5)
    dispatch_resume(timer);
}


#pragma mark - event response
- (void)tapClick:(UIGestureRecognizer *)ges
{
    [self dismissUI];
}

- (void)btnClick:(UIButton *)sender
{
    [self dismissUI];
}


#pragma mark - public methods
//显示倒计时
+ (void)showMoneyWithVC:(UIViewController *)vc imageName:(NSString *)imageName count:(NSInteger)count;
{
    RewardTimeAlertView* redView = [[RewardTimeAlertView alloc]initWithImageName:imageName count:count];
    
        UIWindow* keyWindow = vc.view.window;
    
        if (keyWindow == nil) {
            keyWindow = [UIApplication sharedApplication].keyWindow;
        }

    [keyWindow addSubview:redView];
}

+ (void)showMoney2WithVC:(UIViewController *)vc imageName:(NSString *)imageName
{
     RewardTimeAlertView* redView = [[RewardTimeAlertView alloc]initWithImageName2:imageName];
    UIWindow* keyWindow = vc.view.window;
    
        if (keyWindow == nil) {
            keyWindow = [UIApplication sharedApplication].keyWindow;
        }

    [keyWindow addSubview:redView];
}


- (void)dismissUI
{
     [self removeFromSuperview];
}


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

@end
