//
//  FtCjhxDialog.m
//  Open
//
//  Created by weface on 2023/6/9.
//  Copyright © 2023 QXY. All rights reserved.
//

#import "GiveChanceDialog.h"

@interface GiveChanceDialog ()<UIGestureRecognizerDelegate>
@property (nonatomic, strong)UIView *contentView;
@property (nonatomic, copy)btnYes btnYes;
@property (nonatomic, copy) btnNo btnNo;

@end

@implementation GiveChanceDialog

- (instancetype)initWithTitle:(NSString *)btnTitle btnYes:(btnYes)btnYes btnNo:(btnNo)btnNo{
    self = [super init];
    if (self) {
        self.btnYes = btnYes;
        self.btnNo = btnNo;
        [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, ScreenWidth/20*17, 40 * WIDTHRADIU*9.5);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    //self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 5 * WIDTHRADIU;
    
    
    //顶部图片
    UIImageView *topImage=[UIImageView new];
    topImage.image  = [UIImage imageNamed:@"give_chance_dialog"];
    topImage.userInteractionEnabled=YES;
    UITapGestureRecognizer *tapText03 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(btnNo:)];
    [topImage addGestureRecognizer:tapText03];
    [self.contentView addSubview:topImage];
    [topImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(25 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
    }];
 
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmBtn setTitle:@"免费抽奖" forState:UIControlStateNormal];
    [confirmBtn setTitleColor:[UIColor colorWithHex:0xBF0F1C] forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = [UIFont systemFontOfSize:16 * WIDTHRADIU];
    confirmBtn.backgroundColor = [UIColor colorWithHex:0xFCEBBA];
    confirmBtn.layer.cornerRadius = 15 * WIDTHRADIU;
    confirmBtn.layer.masksToBounds = YES;
    [confirmBtn addTarget:self action:@selector(btnYes:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topImage.mas_bottom).offset(20 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
        make.width.offset(ScreenWidth/20*17-60*WIDTHRADIU);
        make.height.offset(40 * WIDTHRADIU);
    }];
    
}

-(void)btnYes:(UIButton *)sender{
    if (self.btnYes) {
        self.btnYes();
    }
    [self dismissUI];
    
}
-(void)btnNo:(UITapGestureRecognizer *)tap{
    if (self.btnNo) {
        self.btnNo();
    }
    [self dismissUI];
}


#pragma mark - public methods,
+ (void)showWithTitle:(UINavigationController *) view:(NSString *)btnTitle btnYes:(btnYes)btnYes btnNo:(btnNo)btnNo{
    
    GiveChanceDialog *alertView = [[GiveChanceDialog alloc] initWithTitle: btnTitle btnYes:btnYes btnNo:btnNo];
    
    //UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    [view.view addSubview:alertView];
}

//点击空白处
- (void)tapClick:(UITapGestureRecognizer *)tap {
    [self dismissUI];
}

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

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