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

#import "KKSaveActivityPackageView.h"

@interface KKSaveActivityPackageView ()<UIGestureRecognizerDelegate>

@property (nonatomic, strong)UIView* contentView;

@property (nonatomic, copy)NSString* title;

@property (nonatomic, copy)NSString* message;

@property (nonatomic, copy)KKSaveActivityPackageViewBtnBlock block;

@end

@implementation KKSaveActivityPackageView

- (instancetype)initWithTitle:(NSString *)title message:(nonnull NSString *)message block:(nonnull KKSaveActivityPackageViewBtnBlock)block
{
    self = [super init];
    if (self) {
        self.title = title;
        self.message = message;
        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.backgroundColor = [UIColor clearColor];
    self.contentView.bounds = CGRectMake(0, 0, 307 * WIDTHRADIU, 368 * WIDTHRADIU);
    
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    
    //背景图片
    UIImageView* bgImg = [[UIImageView alloc]init];
    bgImg.image = [UIImage imageNamed:@"newYear_ActivityBg"];
    bgImg.contentMode =  UIViewContentModeScaleAspectFill;
    [self.contentView addSubview:bgImg];
    bgImg.frame = self.contentView.bounds;
    
    //文字
//    UIImageView* wordImg = [[UIImageView alloc]init];
//    wordImg.image = [UIImage imageNamed:@"newYear_ActivityWord"];
//    wordImg.contentMode =  UIViewContentModeScaleAspectFill;
//    [bgImg addSubview:wordImg];
//    [wordImg mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.offset(100 * WIDTHRADIU);
//        make.centerX.equalTo(bgImg);
//        make.width.offset(230 * WIDTHRADIU);
//        make.height.offset(33 * WIDTHRADIU);
//    }];
    
    UILabel* lb_title = [UILabel new];
    lb_title.text = self.title;
    lb_title.textColor = [UIColor whiteColor];
    lb_title.font =  [UIFont fontWithName:@"Helvetica-BoldOblique" size:30];
    CGAffineTransform matrix = CGAffineTransformMake(1, 0, tanf(-15 * (CGFloat)M_PI / 180), 1, 0, 0);

    lb_title.transform = matrix;
    lb_title.numberOfLines = 0;
    lb_title.textAlignment = NSTextAlignmentCenter;
    [bgImg addSubview:lb_title];
    
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(95 * WIDTHRADIU);
        make.centerX.equalTo(bgImg);
    }];
    
    
    
    //message
    UILabel* lb_message = [UILabel new];
    lb_message.text = self.message;
    lb_message.textColor = [UIColor whiteColor];
    lb_message.numberOfLines = 0;
    lb_message.textAlignment = NSTextAlignmentCenter;
    [bgImg addSubview:lb_message];
    
    [lb_message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_title.mas_bottom).offset(20 * WIDTHRADIU);
        make.centerX.equalTo(bgImg);
    }];
    
    
    UIButton* receieveBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [receieveBtn setImage:[UIImage imageNamed:@"newYear_button"] forState:UIControlStateNormal];
    
    [receieveBtn addTarget:self action:@selector(receieveBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.contentView addSubview:receieveBtn];
    
    [receieveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(203 * WIDTHRADIU);
        make.centerX.equalTo(bgImg);
        make.width.offset(155 * WIDTHRADIU);
        make.height.offset(137 * WIDTHRADIU);
    }];
    
    
}


+ (void)showViewWithVC:(UIViewController *)vc title:(nonnull NSString *)title message:(nonnull NSString *)message block:(nonnull KKSaveActivityPackageViewBtnBlock)block {
    KKSaveActivityPackageView* packageView = [[KKSaveActivityPackageView alloc]initWithTitle:title message:message block:block];
    
    UIWindow* keyWindow = vc.view.window;

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

    [keyWindow addSubview:packageView];
}

#pragma mark - event response
- (void)receieveBtnClick:(UIButton *)sender {
    
    if (self.block) {
        self.block(YES);
    }
    
    [self hideView];
    
    
}

- (void)tapClick:(UITapGestureRecognizer *)ges {
    
    
    [self hideView];
}




//关闭界面
- (void)hideView{

    [self removeFromSuperview];

}


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

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
