//
//  KKSelectNewsTypeAlertView.m
//  Open
//
//  Created by 雷俊博 on 2021/8/6.
//  Copyright © 2021 黄江涛 . All rights reserved.
//

#import "KKSelectNewsTypeAlertView.h"

@interface KKSelectNewsTypeAlertView ()<UIGestureRecognizerDelegate>

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

@property (nonatomic,copy)KKSelectNewsTypeAlertViewClickBlock block;

@end

@implementation KKSelectNewsTypeAlertView
- (instancetype)initWithBlock:(KKSelectNewsTypeAlertViewClickBlock)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.frame = CGRectMake(0, 0, 295 * WIDTHRADIU, 327 * WIDTHRADIU);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor clearColor];
    
    //背景图片
    UIImageView *bgImg = [[UIImageView alloc]initWithFrame:self.contentView.bounds];
    bgImg.image = [UIImage imageNamed:@"news_youLike"];
    [self.contentView addSubview:bgImg];
    
    
    UIButton *earnBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [earnBtn setTitle:@"边看边赚" forState:UIControlStateNormal];
    [earnBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    earnBtn.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    earnBtn.backgroundColor = [UIColor colorWithHex:0xff5842];
    earnBtn.tag = 200;
    earnBtn.layer.cornerRadius = 22.5 * WIDTHRADIU;
    [earnBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:earnBtn];
    [earnBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10 * WIDTHRADIU);
        make.width.offset(130 * WIDTHRADIU);
        make.height.offset(44 * WIDTHRADIU);
        make.bottom.offset(-12 * WIDTHRADIU);
    }];
    
    UIButton *quickBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [quickBtn setTitle:@"精选头条" forState:UIControlStateNormal];
    [quickBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    quickBtn.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTHRADIU];
    quickBtn.backgroundColor = [UIColor colorWithHex:0xff5842];
    quickBtn.tag = 201;
    quickBtn.layer.cornerRadius = 22.5 * WIDTHRADIU;
    [quickBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:quickBtn];
    [quickBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(earnBtn.mas_right).offset(15 * WIDTHRADIU);
        make.width.offset(130 * WIDTHRADIU);
        make.height.offset(44 * WIDTHRADIU);
        make.bottom.offset(-12 * WIDTHRADIU);
    }];
    
    
}

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


+ (void)showWithBlock:(KKSelectNewsTypeAlertViewClickBlock)block
{
    KKSelectNewsTypeAlertView *alertView = [[KKSelectNewsTypeAlertView alloc]initWithBlock:block];
    
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    [keyWindow addSubview:alertView];
}


#pragma mark - event response
- (void)btnClick:(UIButton *)sender
{
    if (self.block) {
        
        if (sender.tag == 200) {
            self.block(YES);
        }else if (sender.tag == 201){
            self.block(NO);
        }
        
    }
    [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
