//
//  SignRemindAlertView.m
//  Open
//
//  Created by qinxuyang on 2022/2/23.
//  Copyright © 2022 QXY. All rights reserved.
//

#import "SignRemindAlertView.h"
@interface SignRemindAlertView ()<UIGestureRecognizerDelegate>

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

@property (nonatomic, copy) SignRemindAlertViewClickBlock OpneBlock;

@property (nonatomic, copy) SignRemindAlertViewCloseBlock ShutdownBlock;

@end

@implementation SignRemindAlertView
- (instancetype)initWithImageName:(NSString *)imageName OpneBlock:(SignRemindAlertViewClickBlock)block ShutdownBlock:(SignRemindAlertViewCloseBlock)close{
    self = [super init];
    if (self) {
        self.OpneBlock = block;
        self.ShutdownBlock = close;
        [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(0, 0, 320 * WIDTHRADIU, 320 * WIDTHRADIU);
    self.contentView.center = self.center;
    self.contentView.backgroundColor = [UIColor whiteColor];
    self.contentView.layer.cornerRadius = 15;
    self.contentView.layer.masksToBounds = YES;
    [self addSubview:self.contentView];
    
    
    //背景图片
    UIImageView *iconImg = [[UIImageView alloc]initWithFrame:CGRectMake((self.contentView.width - 80) / 2, 20, 80, 83)];
    iconImg.image = [UIImage imageNamed:@"shareRemind"];
    [self.contentView addSubview:iconImg];
    
    UILabel *remind1 = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(iconImg.frame) + 10, self.contentView.width - 20, 30)];
    remind1.centerX = iconImg.centerX;
    remind1.textAlignment = NSTextAlignmentCenter;
    remind1.text = @"关闭签到提醒";
    remind1.font = [UIFont systemFontOfSize:20.0];
    [self.contentView addSubview:remind1];

    UILabel *remind2 = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(remind1.frame), self.contentView.width - 20, 30)];
    remind2.centerX = iconImg.centerX;
    remind2.textAlignment = NSTextAlignmentCenter;
    remind2.text = @"将无法收到签到通知哦～";
    remind2.font = [UIFont systemFontOfSize:20.0];
    [self.contentView addSubview:remind2];


    UIButton *openBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(remind2.frame) + 20, 260, 50)];
    openBtn.centerX = iconImg.centerX;
    openBtn.backgroundColor = K_MainColor;
    openBtn.layer.cornerRadius =25;
    openBtn.layer.masksToBounds = YES;
    [openBtn setTitle:@"继续提醒" forState:UIControlStateNormal];
    openBtn.titleLabel.font = [UIFont systemFontOfSize:20.0];
    [openBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [openBtn addTarget:self action:@selector(clickopenBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:openBtn];

    UIButton *ShutdownBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(openBtn.frame) + 10, 260, 50)];
    ShutdownBtn.centerX = iconImg.centerX;
    [ShutdownBtn setTitle:@"关闭提醒>>" forState:UIControlStateNormal];
    [ShutdownBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    ShutdownBtn.titleLabel.font = [UIFont systemFontOfSize:20.0];
    [ShutdownBtn addTarget:self action:@selector(clickShutdownBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:ShutdownBtn];
}

#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isDescendantOfView:self.contentView]) {
        return NO;
    }
    
    return YES;
}
+ (void)showInWindowWithImageName:(NSString *)imageName VC:(UIViewController *)vc OpneBlock:(SignRemindAlertViewClickBlock)block ShutdownBlock:(SignRemindAlertViewCloseBlock)close{
    SignRemindAlertView *alertView = [[SignRemindAlertView alloc]initWithImageName:imageName OpneBlock:block ShutdownBlock:close];
    
    [vc.view addSubview:alertView];
}

#pragma mark - event response
- (void)clickopenBtn:(UIButton *)sender
{
    if (self.OpneBlock) {
        self.OpneBlock(YES);
    }
    [self dismissUI];
}
- (void)clickShutdownBtn:(UIButton *)sender{
    if (self.ShutdownBlock) {
        self.ShutdownBlock(YES);
    }
    [self dismissUI];
}
- (void)tapClick:(UITapGestureRecognizer *)tap {
    [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
