//
//  GreenChannelAlertView.m
//  Open
//
//  Created by 雷俊博 on 2020/3/26.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "GreenChannelAlertView.h"
#import "Constants.h"

@interface GreenChannelAlertView ()

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

@property (nonatomic, strong)UIImageView *img_bg;

@property (nonatomic, strong)UIView* hLine;

@property (nonatomic, strong)UIView* vLine;

@property (nonatomic, strong)UIButton* leftBtn;

@property (nonatomic, strong)UIButton* rightBtn;

@property (nonatomic, copy)void (^Callback)(NSInteger index) ;


@end

@implementation GreenChannelAlertView

- (instancetype)initWithCustomWithCallback:(void (^)(NSInteger index)  )Callback;
{
    self = [super init];
    if (self) {
        [self setupUI];
        self.Callback = Callback;
    }
    return self;
}

#pragma mark - UI搭建
/** UI搭建 */
- (void)setupUI
{
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.bounds = CGRectMake(0, 0, 280*WIDTHRADIU, 390 * WIDTHRADIU);
    self.contentView.center = self.center;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor clearColor];
    
    [self.contentView addSubview:self.img_bg];
    [self.contentView addSubview:self.hLine];
    [self.contentView addSubview:self.vLine];
    [self.contentView addSubview:self.leftBtn];
    [self.contentView addSubview:self.rightBtn];
    
    [self.img_bg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    [self.hLine setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.hLine setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.hLine setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [self.hLine setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [self.hLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-45 * WIDTHRADIU);
        make.left.right.offset(0);
        make.height.offset(0.5);
    }];
    
    [self.vLine setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.vLine setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];
    [self.vLine setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [self.vLine setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
    [self.vLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.hLine.mas_bottom);
        make.bottom.centerX.offset(0);
        make.width.offset(0.5);
    }];
    
    [self.leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.offset(0);
        make.top.equalTo(self.hLine.mas_bottom);
        make.right.equalTo(self.vLine.mas_left);
    }];
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.offset(0);
        make.top.equalTo(self.hLine.mas_bottom);
        make.left.equalTo(self.vLine.mas_right);
    }];
    
    
    
}


+ (void)showWithViewController:(UIViewController*)viewController Callback:(void (^)(NSInteger index)  )Callback;
{
    UIWindow* keyWindow = viewController.view.window;
    
    if (keyWindow == nil) {
        keyWindow = [UIApplication sharedApplication].keyWindow;
    }
    
    GreenChannelAlertView *alertView = [[GreenChannelAlertView alloc]initWithCustomWithCallback:Callback];
    
    [alertView show:keyWindow];
}


#pragma mark - event response
- (void)goToLastVC:(UIButton *)sender
{
    if (self.Callback) {
        self.Callback(200);
    }
    [self dismiss];
}

- (void)goToNextVC:(UIButton *)sender
{
    if (self.Callback) {
        self.Callback(201);
    }
    
    [self dismiss];
}

#pragma mark - 显示或隐藏
/** 弹出此弹窗 */
- (void)show:(UIWindow *)window {
    // 出场动画
    self.alpha = 0;
    self.contentView.transform = CGAffineTransformScale(self.contentView.transform, 1.3, 1.3);
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 1;
        self.contentView.transform = CGAffineTransformIdentity;
    }];
    
    [window addSubview:self];
}

/** 移除此弹窗 */
- (void)dismiss {
    [self removeFromSuperview];
}


#pragma mark - getters and setters
- (UIImageView *)img_bg
{
    if (!_img_bg) {
        _img_bg = [[UIImageView alloc]init];
        _img_bg.image = [UIImage imageNamed:@"bg_greenChannelAlertView"];
    }
    return _img_bg;
}
- (UIView *)hLine
{
    if (!_hLine) {
        _hLine = [[UIView alloc]init];
        _hLine.backgroundColor = [UIColor colorWithHex:0xcccccc];
    }
    return _hLine;
}
- (UIView *)vLine
{
    if (!_vLine) {
        _vLine = [[UIView alloc]init];
        _vLine.backgroundColor = [UIColor colorWithHex:0xcccccc];
    }
    return _vLine;
}
- (UIButton *)leftBtn
{
    if (!_leftBtn) {
        _leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftBtn setTitle:@"返回上一步" forState:UIControlStateNormal];
        [_leftBtn setTitleColor:[UIColor colorWithHex:0x069EE6] forState:UIControlStateNormal];
        _leftBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16 * WIDTHRADIU];
        [_leftBtn setBackgroundColor:[UIColor clearColor]];
        [_leftBtn addTarget:self action:@selector(goToLastVC:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftBtn;
}

- (UIButton *)rightBtn
{
    if (!_rightBtn) {
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightBtn setTitle:@"好的，继续" forState:UIControlStateNormal];
        [_rightBtn setTitleColor:[UIColor colorWithHex:0x069EE6] forState:UIControlStateNormal];
        _rightBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16 * WIDTHRADIU];
        [_rightBtn setBackgroundColor:[UIColor clearColor]];
        [_rightBtn addTarget:self action:@selector(goToNextVC:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightBtn;
}

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

@end
