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

#import "QRRedPackageGiftView.h"

@interface QRRedPackageGiftView ()<UIGestureRecognizerDelegate>

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

@property (nonatomic, copy)clickQRRedPackageGiftViewBtnBlock block;

@end

@implementation QRRedPackageGiftView
- (instancetype)initWithBlock:(clickQRRedPackageGiftViewBtnBlock)block;
{
    self = [super init];
    if (self) {
        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.bounds = CGRectMake(0, 0, 314*WIDTHRADIU, 341 * WIDTHRADIU);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor clearColor];
    
    UIImageView*backImg = [[UIImageView alloc]initWithFrame:_contentView.bounds];
    backImg.image = [UIImage imageNamed:@"save_qrOpenedPackage"];
    [_contentView addSubview:backImg];
    
    //标题
    UILabel* lb_title = [UILabel new];
    lb_title.text = @"人工智能赋能创业";
    lb_title.textColor = [UIColor colorWithHex:0xcb9836];
    lb_title.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
    lb_title.textAlignment = NSTextAlignmentCenter;
    [backImg addSubview:lb_title];
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25 * WIDTHRADIU);
        make.centerX.equalTo(backImg);
    }];
    
    //左边线
    UIView*leftLine = [UIView new];
    leftLine.backgroundColor = [UIColor colorWithHex:0xcb9836];
    [backImg addSubview:leftLine];
    [leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lb_title.mas_left).offset(-10 * WIDTHRADIU);
        make.centerY.equalTo(lb_title);
        make.width.offset(25 * WIDTHRADIU);
        make.height.offset(0.5);
    }];
    
    //右边线
    UIView*rightLine = [UIView new];
    rightLine.backgroundColor = [UIColor colorWithHex:0xcb9836];
    [backImg addSubview:rightLine];
    [rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lb_title.mas_right).offset(10 * WIDTHRADIU);
        make.centerY.equalTo(lb_title);
        make.width.offset(25 * WIDTHRADIU);
        make.height.offset(0.5);
    }];
    
    UILabel*lb_money = [UILabel new];
    lb_money.text = @"价值888元创业礼包";
    lb_money.textColor = [UIColor colorWithHex:0xe5281d];
    lb_money.textAlignment = NSTextAlignmentCenter;
    lb_money.font = [UIFont boldSystemFontOfSize:23 * WIDTHRADIU];
    [backImg addSubview:lb_money];
    [lb_money mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_title.mas_bottom).offset(15 * WIDTHRADIU);
        make.centerX.equalTo(backImg);
    }];
    
    UILabel*lb_message = [UILabel new];
    lb_message.text = @"0投资随时随地”躺“着赚钱";
    lb_message.textColor = [UIColor colorWithHex:0xe5281d];
    lb_message.textAlignment = NSTextAlignmentCenter;
    lb_message.font = [UIFont systemFontOfSize:13 * WIDTHRADIU];
    [backImg addSubview:lb_message];
    [lb_message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_money.mas_bottom).offset(5 * WIDTHRADIU);
        make.centerX.equalTo(backImg);
    }];
    
    
    //领福利按钮
   UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
   confirmBtn.backgroundColor = [UIColor colorWithHex:0xfcdb93];
   [confirmBtn setTitle:@"扫二维码领取" forState:UIControlStateNormal];
   [confirmBtn setTitleColor:[UIColor colorWithHex:0xa41d1f] forState:UIControlStateNormal];
   confirmBtn.titleLabel.font = [UIFont systemFontOfSize:17 * WIDTHRADIU];
   confirmBtn.layer.cornerRadius = 15 * WIDTHRADIU;
   [confirmBtn addTarget:self action:@selector(confirmBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:confirmBtn];
   [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       make.bottom.offset(- 10 * WIDTHRADIU);
       make.centerX.equalTo(self.contentView);
       make.width.offset(230 * WIDTHRADIU);
       make.height.offset(50 * WIDTHRADIU);
   }];
   
   //提示
   UILabel* messageLabel = [UILabel new];
   messageLabel.text = @"微信扫码加客服领福利";
   messageLabel.textColor = [UIColor colorWithHex:0xfff2c3];
   messageLabel.font = [UIFont systemFontOfSize:13 * WIDTHRADIU];
   messageLabel.textAlignment = NSTextAlignmentCenter;
   [self.contentView addSubview:messageLabel];
   [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       make.bottom.equalTo(confirmBtn.mas_top).offset(-13 * WIDTHRADIU);
       make.centerX.equalTo(self.contentView);
   }];
   
   UIImageView *qrImg = [[UIImageView alloc]init];
   qrImg.image = [UIImage imageNamed:@"save_qrcode"];
   [self.contentView addSubview:qrImg];
   [qrImg mas_makeConstraints:^(MASConstraintMaker *make) {
       make.centerX.equalTo(self.contentView);
       make.bottom.equalTo(messageLabel.mas_top).offset(-8 * WIDTHRADIU);
       make.width.offset(120 * WIDTHRADIU);
       make.height.offset(120 * WIDTHRADIU);
   }];
               
    
    
}

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

#pragma mark - public methods
+ (void)showViewWithVC:(UIViewController *)vc block:(clickQRRedPackageGiftViewBtnBlock)block;
{
    QRRedPackageGiftView* redView = [[QRRedPackageGiftView alloc]initWithBlock:block];
    
    UIWindow* keyWindow = vc.view.window;

    if (keyWindow == nil) {
        keyWindow = [UIApplication sharedApplication].keyWindow;
    }

    [keyWindow addSubview:redView];
}


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

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

- (void)confirmBtnClick:(UIButton *)btn
{
    if (self.block) {
        self.block(YES);
    }
    [self dismissUI];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
