//
//  BankPresentView.m
//  Open
//
//  Created by 雷俊博 on 2020/7/29.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "BankPresentView.h"
#import "ProductListCell.h"
#import "BankListCell.h"
#import "SaveBankModel.h"

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

@property (nonatomic,strong)UIView* contentView;

@property (nonatomic, strong)UITableView* tableView;

@property (nonatomic, strong)NSArray* dataArray;

@property (nonatomic, assign)BankPresentViewType type;

@property (nonatomic, copy)BankPresentViewSelectBlock block;

@end

@implementation BankPresentView
- (instancetype)initWithType:(BankPresentViewType)type bankArray:(NSArray *)array block:(BankPresentViewSelectBlock)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, 380 * WIDTHRADIU);
    
    //标题
    UILabel*lb_title = [[UILabel alloc]init];
    lb_title.text = @"支持银行";
    lb_title.textColor = [UIColor colorWithHex:0x4d4d4d];
    lb_title.font = [UIFont boldSystemFontOfSize:16];
    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(-(TAB_BAR_HEIGHT - 49));
        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:(BankPresentViewType)type bankArray:(NSArray *)array block:(BankPresentViewSelectBlock)block
{
    BankPresentView*presentView = [[BankPresentView 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";
    BankListCell*cell = [tableView dequeueReusableCellWithIdentifier:identify];
    
    if (self.type == BankPresentViewTypeWuDang) {
        SaveBankModel*model = self.dataArray[indexPath.section];
        cell.bankIcon.image = [UIImage imageNamed:model.bankImage];
        cell.lb_bankName.text = model.bankName;
        cell.lb_note.text = [NSString stringWithFormat:@"单笔扣款限额%@万元,每日扣款限额%@万元",model.orderMin,model.dMin];
    }else if(self.type == BankPresentViewTypeBiCai) {
        SaveBCBankModel*model = self.dataArray[indexPath.section];
        [cell.bankIcon sd_setImageWithURL:[NSURL URLWithString:model.bankLogoUrl] completed:nil];
        cell.lb_bankName.text = model.bankName;
        cell.lb_note.text = model.bankCardQuotaDescDto.singleDedctDesc;
    }
   
    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;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SaveBankModel*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 - 380 * WIDTHRADIU, SCREEN_WIDTH, 380 * WIDTHRADIU);
    } 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:@"BankListCell" 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
