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

#import "SuppleSignAlertView.h"

@interface SuppleSignAlertView ()<UIGestureRecognizerDelegate>

@property (nonatomic, strong)UIView *contentView;

@property (nonatomic, copy)NSString *viewTitle;

@property (nonatomic, copy)NSString *viewMessage;

@property (nonatomic, copy)NSString *btnTitle;

@property (nonatomic, copy)SuppleSignAlertViewClickBlock block;

@property (nonatomic, copy) ClickExchangBlock ExchangBlock;

@end

@implementation SuppleSignAlertView
#pragma mark - life cycle
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message btnTitle:(NSString *)btnTitle block:(SuppleSignAlertViewClickBlock)block ExchangBlock:(ClickExchangBlock)ExchangBlock
{
    self = [super init];
    if (self) {
        
        self.viewTitle = title;
        self.viewMessage = message;
        self.btnTitle = btnTitle;
        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, 230 * WIDTHRADIU, 208 * WIDTHRADIU);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 15 * WIDTHRADIU;
    
    //标题label
    UILabel *lb_title = [UILabel new];
    lb_title.text = self.viewTitle;
    lb_title.font = [UIFont systemFontOfSize:17 * WIDTHRADIU weight:UIFontWeightMedium];
    lb_title.textAlignment = NSTextAlignmentCenter;
    lb_title.textColor = [UIColor colorWithHex:0xf98505];
    [self.contentView addSubview:lb_title];
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(10 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
    
    //message
    UILabel *lb_message = [UILabel new];
    lb_message.text = self.viewMessage;
    lb_message.font = [UIFont systemFontOfSize:12 * WIDTHRADIU];
    lb_message.textAlignment = NSTextAlignmentCenter;
    lb_message.textColor = [UIColor colorWithHex:0x808080];
    [self.contentView addSubview:lb_message];
    [lb_message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_title.mas_bottom).offset(6 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
    
    //背景图片
    UIImageView *signImageView = [UIImageView new];
    signImageView.image = [UIImage imageNamed:@"suppleSignCard"];
    [self.contentView addSubview:signImageView];
    [signImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_message.mas_bottom).offset(10 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
        make.width.offset(195 * WIDTHRADIU);
        make.height.offset(80 * WIDTHRADIU);
    }];
    
    //按钮
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmBtn setTitle:self.btnTitle forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    confirmBtn.backgroundColor = [UIColor colorWithHex:0xf98505];
    confirmBtn.layer.cornerRadius = 20 * WIDTHRADIU;
    confirmBtn.layer.masksToBounds = YES;
    
    [confirmBtn addTarget:self action:@selector(confirmBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(signImageView.mas_bottom).offset(10 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
        make.width.offset(200 * 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];
//    [ExchangBtn addTarget:self action:@selector(ExchangBtn:) forControlEvents:UIControlEventTouchUpInside];
//    [self.contentView addSubview:ExchangBtn];
//    [ExchangBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(confirmBtn.mas_bottom).offset(5 * WIDTHRADIU);
//        make.centerX.equalTo(self.contentView);
//        make.width.offset(200 * WIDTHRADIU);
//        make.height.offset(30 * 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)showWithTitle:(NSString *)title message:(NSString *)message btnTitle:(NSString *)btnTitle block:(SuppleSignAlertViewClickBlock)block ExchangBlock:(ClickExchangBlock)ExchangBlock
{
    SuppleSignAlertView *alertView = [[SuppleSignAlertView alloc]initWithTitle:title message:message btnTitle:btnTitle block:block ExchangBlock:ExchangBlock];
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    [keyWindow addSubview:alertView];
}


#pragma mark - event response
- (void)tapClick:(UITapGestureRecognizer *)tap {
    [self dismissUI];
}
- (void)confirmBtn:(UIButton *)sender
{
    if (self.block) {
        self.block();
    }
    [self dismissUI];
}
- (void)ExchangBtn:(UIButton *)button{
    if (self.ExchangBlock) {
        self.ExchangBlock();
    }
    [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
