//
//  AgreementPresentView.m
//  Open
//
//  Created by 雷俊博 on 2020/8/17.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "AgreementPresentView.h"
#import "AgreementListCell.h"

@interface AgreementPresentView ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>

@property (nonatomic,strong)UIView* contentView;

@property (nonatomic, strong)UITableView* tableView;

@property (nonatomic, strong)NSArray* dataArray;

@property (nonatomic, assign)AgreementPresentViewType type;

@property (nonatomic, copy)AgreementPresentViewSelectBlock block;


@end

@implementation AgreementPresentView
- (instancetype)initWithType:(AgreementPresentViewType)type bankArray:(NSArray *)array block:(AgreementPresentViewSelectBlock)block
{
    self = [super init];
    if (self) {
        self.type = type;
        self.dataArray = array;
        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:)];
//    [self addGestureRecognizer:tap];
    
    
    //------- 弹窗主内容 -------//
    self.contentView = [[UIView alloc] init];
    self.contentView.frame = CGRectMake(0, ScreenHeight, self.bounds.size.width, 290 +(TAB_BAR_HEIGHT - 49));
    
    //标题
    UILabel*lb_title = [[UILabel alloc]init];
    lb_title.text = @"相关协议";
    lb_title.textColor = [UIColor colorWithHex:0x4d4d4d];
    lb_title.font = [UIFont boldSystemFontOfSize:18];
    lb_title.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:lb_title];
    [lb_title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(50);
    }];
    
    [self.contentView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.bottom.offset(0);
        make.top.offset(50);
    }];
    
    UIImageView*closeImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"close"]];
    [self.contentView addSubview:closeImage];
    [closeImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-20);
        make.width.offset(20);
        make.height.offset(20);
        make.top.offset(15);
    }];
    
    UIButton*closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [closeBtn addTarget:self action:@selector(closeClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:closeBtn];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(0);
        make.width.offset(60);
        make.height.offset(50);
        make.top.offset(0);
    }];
    
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor whiteColor];
    //self.contentView.layer.cornerRadius = 15 * WIDTHRADIU;
}

+ (void)showBankListWithVC:(UIViewController *)vc type:(AgreementPresentViewType)type bankArray:(NSArray *)array block:(AgreementPresentViewSelectBlock)block
{
    AgreementPresentView*presentView = [[AgreementPresentView alloc]initWithType:type bankArray:array block:block];
    UIWindow* keyWindow = vc.view.window;
          
      if (keyWindow == nil) {
          keyWindow = [UIApplication sharedApplication].keyWindow;
      }

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

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString*identify = @"cell";
    AgreementListCell*cell = [tableView dequeueReusableCellWithIdentifier:identify];
    
    if (self.type == AgreementPresentViewTypeWuDang) {
        NSDictionary*dic = self.dataArray[indexPath.section];
        cell.lb_name.text = dic[@"name"];
    }else if (self.type == AgreementPresentViewTypeBiCai) {
        BCSubModel* model = self.dataArray[indexPath.section];
        cell.lb_name.text = model.agreementTitle;
    }
   
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    id model = self.dataArray[indexPath.section];
    if (self.block) {
        self.block(model);
    }
    [self hideView];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView.contentOffset.y <= 0) {
        scrollView.contentOffset = CGPointMake(0, 0);
    }
}

#pragma mark - event response
- (void)tapClick:(UITapGestureRecognizer *)ges
{
    [self hideView];
}
- (void)closeClick
{
    [self hideView];
}

#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, 0);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];

}

#pragma mark - getters
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.estimatedSectionFooterHeight = 10;
        _tableView.estimatedSectionHeaderHeight = 10;
        //_tableView.rowHeight = UITableViewAutomaticDimension;
        //_tableView.estimatedRowHeight = 100;
        [_tableView registerNib:[UINib  nibWithNibName:@"AgreementListCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    }
    return _tableView;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
