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

#import "BCNewActivityShareView.h"

@interface BCNewActivityShareView ()<UIGestureRecognizerDelegate>

@property (nonatomic,strong)UIView* contentView;

@property (nonatomic,copy)BCNewActivityShareViewBtnBlock block;

@end

@implementation BCNewActivityShareView
- (instancetype)initWithBlock:(BCNewActivityShareViewBtnBlock)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.backgroundColor = [UIColor whiteColor];
    self.contentView.frame = CGRectMake(0, ScreenHeight, self.bounds.size.width, 150 +(TAB_BAR_HEIGHT - 49));
    [self addSubview:self.contentView];
    
    
    //title
    UILabel* lb_title = [UILabel new];
    lb_title.text = @"邀请新用户存款500元以上送20元";
    lb_title.textColor = [UIColor colorWithHex:0x808080];
    lb_title.font = [UIFont systemFontOfSize:14];
    lb_title.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:lb_title];
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(20);
        make.centerX.equalTo(self.contentView);
    }];
    
    UIView* leftLine = [UIView new];
    leftLine.backgroundColor = [UIColor colorWithHex:0x808080];
    [self.contentView addSubview:leftLine];
    [leftLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lb_title);
        make.right.equalTo(lb_title.mas_left).offset(-10);
        make.width.offset(40);
        make.height.offset(0.5);
    }];
    
    UIView* rightLine = [UIView new];
    rightLine.backgroundColor = [UIColor colorWithHex:0x808080];
    [self.contentView addSubview:rightLine];
    [rightLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lb_title);
        make.left.equalTo(lb_title.mas_right).offset(10);
        make.width.offset(40);
        make.height.offset(0.5);
    }];
    
    
    UIButton *wxBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [wxBtn setTitle:@"微信" forState:UIControlStateNormal];
    [wxBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    [wxBtn setImage:[UIImage imageNamed:@"wx"] forState:UIControlStateNormal];
    wxBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [wxBtn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleTop imageTitleSpace:10];
    [wxBtn addTarget:self action:@selector(wxBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:wxBtn];
    
    UIButton *pyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [pyBtn setTitle:@"朋友圈" forState:UIControlStateNormal];
    [pyBtn setTitleColor:[UIColor colorWithHex:0x808080] forState:UIControlStateNormal];
    [pyBtn setImage:[UIImage imageNamed:@"pengyouquan"] forState:UIControlStateNormal];
    pyBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [pyBtn layoutButtonWithEdgeInsetsStyle:GLButtonEdgeInsetsStyleTop imageTitleSpace:10];
    [pyBtn addTarget:self action:@selector(pyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:pyBtn];
    
    
    [wxBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_title.mas_bottom).offset(20);
        make.width.offset(60);
        make.height.offset(80);
        make.centerX.offset(-60);
    }];
    
    [pyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lb_title.mas_bottom).offset(20);
        make.width.offset(60);
        make.height.offset(80);
        make.centerX.offset(60);
    }];
    
    
}


#pragma mark - public methods
+ (void)showWithVC:(UIViewController *)vc block:(BCNewActivityShareViewBtnBlock)block {
    BCNewActivityShareView*presentView = [[BCNewActivityShareView alloc]initWithBlock:block];
    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)wxBtnClick:(UIButton *)sender {
    
    if (self.block) {
        self.block(KKSaveActivityShareTypeWX);
    }
    
    [self hideView];
   
}

- (void)pyBtnClick:(UIButton *)sender {
    
    if (self.block) {
        self.block(KKSaveActivityShareTypeFriend);
    }
    
    [self hideView];
}


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

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

    [UIView animateWithDuration:0.1 animations:^{
        self.contentView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 150 +(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
