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

#import "BCRechargeSignAlertView.h"

@interface BCRechargeSignAlertView ()<UIGestureRecognizerDelegate>

@property (nonatomic,strong)UIView* contentView;

@property (nonatomic,copy)NSString* title;

@property (nonatomic,copy)BCRechargeSignAlertViewBlock  block;

@end

@implementation BCRechargeSignAlertView

- (instancetype)initWithTitle:(NSString *)title block:(nonnull BCRechargeSignAlertViewBlock)blcok{
    self = [super init];
    if (self) {
        self.title = title;
        self.block = blcok;
        [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 whiteColor];
    self.contentView.frame = CGRectMake(0, ScreenHeight, self.bounds.size.width, 290 +(TAB_BAR_HEIGHT - 49));
    [self addSubview:self.contentView];
    
    UIBezierPath *cornerRadiusPath = [UIBezierPath bezierPathWithRoundedRect:self.contentView.bounds byRoundingCorners:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadii:CGSizeMake(16, 16)];

    CAShapeLayer *cornerRadiusLayer = [ [CAShapeLayer alloc ] init];

    cornerRadiusLayer.frame = self.contentView.bounds;

    cornerRadiusLayer.path = cornerRadiusPath.CGPath;
    self.contentView.layer.mask = cornerRadiusLayer;
    
    //红×
    UIImageView* wrongImg = [[UIImageView alloc]init];
    wrongImg.image = [UIImage imageNamed:@"bcWrongAlert_icon"];
    [self.contentView addSubview:wrongImg];
    [wrongImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(30 * WIDTHRADIU);
        make.centerX.equalTo(self.contentView);
        make.width.height.offset(60 * WIDTHRADIU);
    }];
    
    //充值失败
    UILabel* lb_title = [UILabel new];
    lb_title.text = @"充值失败";
    lb_title.textColor = [UIColor colorWithHex:0x4d4d4d];
    lb_title.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:lb_title];
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(wrongImg.mas_bottom).offset(20 * WIDTHRADIU);
        make.centerX.equalTo(wrongImg);
    }];
    
    //message
    UILabel* lb_message = [UILabel new];
    lb_message.text = self.title;
    lb_message.textColor = [UIColor colorWithHex:0x808080];
    lb_message.textAlignment = NSTextAlignmentCenter;
    lb_message.font = [UIFont systemFontOfSize:14];
    lb_message.numberOfLines = 0;
    [self.contentView addSubview:lb_message];
    [lb_message mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_title.mas_bottom);
        make.centerX.equalTo(wrongImg);
        make.left.offset(15 * WIDTHRADIU);
        make.right.offset(-15 * WIDTHRADIU);
    }];
    
    UIButton* signBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [signBtn setTitle:@"去签约" forState:UIControlStateNormal];
    [signBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    signBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    signBtn.layer.cornerRadius = 5;
    signBtn.backgroundColor  = K_MainColor;
    [signBtn addTarget:self action:@selector(signBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:signBtn];
    [signBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_message.mas_bottom).offset(30);
        make.left.offset(15 * WIDTHRADIU);
        make.right.offset(-10 * WIDTHRADIU);
        make.height.offset(45 * WIDTHRADIU);
    }];
    
    
    
    
    
}

+ (void)showWithVC:(UIViewController *)vc title:(nonnull NSString *)title block:(nonnull BCRechargeSignAlertViewBlock)blcok {
    BCRechargeSignAlertView*presentView = [[BCRechargeSignAlertView alloc]initWithTitle:title block:blcok];
    UIWindow* keyWindow = vc.view.window;
          
      if (keyWindow == nil) {
          keyWindow = [UIApplication sharedApplication].keyWindow;
      }

      [keyWindow addSubview:presentView];
      [presentView showView];
}

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

#pragma mark - event response
- (void)tapClick:(UITapGestureRecognizer *)ges {
    
    
    [self hideView];
}
- (void)signBtnClick:(UIButton *)sender {
    [self hideView];
    if (self.block) {
        self.block();
    }
}


#pragma mark - 隐藏或显示
- (void)showView
{
    [UIView animateWithDuration:0.2 animations:^{
        self.contentView.frame = CGRectMake(0, ScreenHeight - 290 -(TAB_BAR_HEIGHT - 49) , SCREEN_WIDTH, 290 +(TAB_BAR_HEIGHT - 49));
    } completion:^(BOOL finished) {
        
    }];
}

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

    [UIView animateWithDuration:0.1 animations:^{
        self.contentView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 290 +(TAB_BAR_HEIGHT - 49));
    } completion:^(BOOL finished) {
        [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
