//
//  CoinCenterCoinRewardAlertView.m
//  Open
//
//  Created by 雷俊博 on 2021/3/1.
//  Copyright © 2021 黄江涛 . All rights reserved.
//

#import "CoinCenterCoinRewardAlertView.h"

@interface CoinCenterCoinRewardAlertView ()<UIGestureRecognizerDelegate>

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

@property (nonatomic, copy)CoinCenterCoinRewardAlertViewBtnBlock block;

@property (nonatomic, copy)NSString* number;


@end

@implementation CoinCenterCoinRewardAlertView
- (instancetype)initWithTitle:(NSString *)number block:(CoinCenterCoinRewardAlertViewBtnBlock)block;
{
    self = [super init];
    if (self) {
        self.number = number;
        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:)];
    tap.delegate = self;
    [self addGestureRecognizer:tap];
       
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.frame = CGRectMake((ScreenWidth - 243*WIDTHRADIU)/2.0, (ScreenHeight - 489 * WIDTHRADIU)/2, 243*WIDTHRADIU, 259 * WIDTHRADIU);
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor clearColor];
    
    UIImageView*backImg = [[UIImageView alloc]initWithFrame:_contentView.bounds];
    backImg.image = [UIImage imageNamed:@"coinCenter_coinView"];
    [_contentView addSubview:backImg];
    
    
    //数量
    UILabel* lb_number = [UILabel new];
    lb_number.text = self.number;
    lb_number.font = [UIFont boldSystemFontOfSize:30 * WIDTHRADIU];
    lb_number.textColor = [UIColor colorWithHex:0xa41d1f];
    lb_number.textAlignment = NSTextAlignmentCenter;
    [_contentView addSubview:lb_number];
    [lb_number mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(30 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
    
    
    
    
    
    //领福利按钮
   UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
   confirmBtn.backgroundColor = [UIColor colorWithHex:0xfed6c7];
   [confirmBtn setTitle:@"翻倍领取" forState:UIControlStateNormal];
   [confirmBtn setTitleColor:[UIColor colorWithHex:0xa41d1f] forState:UIControlStateNormal];
   confirmBtn.titleLabel.font = [UIFont systemFontOfSize:16 * WIDTHRADIU];
   confirmBtn.layer.cornerRadius = 22.5 * WIDTHRADIU;
   [confirmBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:confirmBtn];
   [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       make.bottom.offset(- 15 * WIDTHRADIU);
       make.centerX.equalTo(self.contentView);
       make.width.offset(200 * WIDTHRADIU);
       make.height.offset(45 * WIDTHRADIU);
   }];
   
   
               
    
    
}

+ (void)showViewWithVC:(UIViewController *)vc coin:(NSString *)number block:(CoinCenterCoinRewardAlertViewBtnBlock)block
{
    CoinCenterCoinRewardAlertView* alertView = [[CoinCenterCoinRewardAlertView alloc]initWithTitle:number block:block];
    
//    UIWindow* keyWindow = vc.view.window;
//
//    if (keyWindow == nil) {
//        keyWindow = [UIApplication sharedApplication].keyWindow;
//    }

    [vc.view addSubview:alertView];
}

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


#pragma mark - event response
- (void)tapClick:(UITapGestureRecognizer *)tap {
    [self dismissUI];
    
    if (self.block) {
        self.block(NO);
    }
}

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




#pragma mark -隐藏弹框
- (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
