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

#import "WithDrawPayAmountSelectCell.h"
#import "WithDrawCollectionViewCell.h"
#import "WithDrawAmountOneSelectCell.h"
#import "WithDrawFooterView.h"
@interface WithDrawPayAmountSelectCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UIImageView *itemImgView;

@property (nonatomic, strong)UILabel *titleLabel;

@property (nonatomic, strong) UIButton *btn_bind;

@property (nonatomic, strong) UIImageView *rightImg;

@property (nonatomic, strong)UICollectionView* collectionView;

@property (nonatomic, strong)NSArray *dataArray;

@end

@implementation WithDrawPayAmountSelectCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIImageView *itemImgView = [[UIImageView alloc]init];
        itemImgView.image = [UIImage imageNamed:@"WeChatItem"];
        self.itemImgView = itemImgView;
        [self.contentView addSubview:itemImgView];
        //标题
        UILabel* lb_title = [UILabel new];
        lb_title.textColor = [UIColor colorWithHex:0x4d4d4d];
        lb_title.font = [UIFont boldSystemFontOfSize:16.0];
        lb_title.text = @"提现金额";
        [self.contentView addSubview:lb_title];
        self.titleLabel = lb_title;
        
        [self.contentView addSubview:self.btn_bind];
        [self.contentView addSubview:self.rightImg];
        
        //绑定
//        CGRect footerFrame = CGRectMake(0, 95* WIDTHRADIU, ScreenWidth, 35 * WIDTHRADIU);
//        WithDrawFooterView *footerView = [[WithDrawFooterView alloc]initWithFrame:footerFrame];
//        footerView.delegate = self;
//        [self.contentView addSubview:footerView];
//        self.footerView = footerView;
        
        //amount
        [self.contentView addSubview:self.collectionView];
        
        [self configFrame];
    }
    return self;
}
- (UIButton *)btn_bind
{
    if (!_btn_bind) {
        _btn_bind = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btn_bind setTitle:@"立即绑定" forState:UIControlStateNormal];
        [_btn_bind setTitleColor:[UIColor colorWithHex:0x4d4d4d] forState:UIControlStateNormal];
        _btn_bind.titleLabel.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
        [_btn_bind addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btn_bind;
}
- (UIImageView *)rightImg
{
    if (!_rightImg) {
        _rightImg = [[UIImageView alloc]init];
        _rightImg.image = [UIImage imageNamed:@"right_black"];
    }
    return _rightImg;
}
- (void)configFrame{
    [self.itemImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(15 * WIDTHRADIU);
        make.left.offset(20 * WIDTHRADIU);
        make.height.offset(20 * WIDTHRADIU);
        make.width.offset(20 * WIDTHRADIU);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.itemImgView.mas_right).offset(5 * WIDTHRADIU);
        make.centerY.equalTo(self.itemImgView);
    }];
    
    [self.rightImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.itemImgView);
        make.right.offset(-15  * WIDTHRADIU);
        make.height.offset(15 * WIDTHRADIU);
        make.width.offset(7 * WIDTHRADIU);
    }];
    
    [self.btn_bind mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.itemImgView);
        make.right.equalTo(self.rightImg.mas_left).offset(- 5 * WIDTHRADIU);
        make.height.offset(13 * WIDTHRADIU);
    }];
}
- (void)refreshUIWithDic:(NSDictionary *)dic
{
    [self.btn_bind setTitle:@"已绑定微信" forState:UIControlStateNormal];
    [self.btn_bind setTitleColor:[UIColor colorWithHex:0xf98505] forState:UIControlStateNormal];
}
#pragma mark - event resposne
- (void)btnClick:(UIButton *)sender
{
    if (self.binddelegate && [self.binddelegate respondsToSelector:@selector(bindWXWithCell:)]) {
        [self.binddelegate bindWXWithCell:sender];
    }
}
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *identifier = @"WithDrawPayAmountSelectCell";
    
    WithDrawPayAmountSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        cell = [[WithDrawPayAmountSelectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.dataArray.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0 && self.firstWithDraw == NO) {
        
        WithDrawAmountOneSelectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
        
        NSNumber *money = self.dataArray[indexPath.row];
        cell.lb_money.text = [NSString stringWithFormat:@"%d元",money.intValue];
        if (self.index == indexPath.row) {
            cell.cellSelected = YES;
        }else{
            cell.cellSelected = NO;
        }
        
        
        return cell;
       
    }
    
    WithDrawCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
        cell.hiddenImg = NO;
        NSNumber *money = self.dataArray[indexPath.row];
        cell.lb_money.text = [NSString stringWithFormat:@"%d元",money.intValue];
        if (self.index == indexPath.row) {
            cell.cellSelected = YES;
        }else{
            cell.cellSelected = NO;
        }
    return cell;
}

/**
 cell的大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = (ScreenWidth - (3 - 1) * 10 * WIDTHRADIU - 20 * WIDTHRADIU * 2)  / 3;

    CGFloat height = 60 * WIDTHRADIU;
    
    CGSize size = CGSizeMake(width, height);
    return size;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    return CGSizeZero;
}
/**
 每个分区的内边距（上左下右）
 */
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 20 * WIDTHRADIU,0, 20 * WIDTHRADIU);
}
/**
 分区内cell之间的最小行间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 10 * WIDTHRADIU;
}
/**
 分区内cell之间的最小列间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 10 * WIDTHRADIU;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.firstWithDraw == NO && indexPath.row == 0) {
        return;
    }
    
    if (self.index == indexPath.item) {
        return;
    }
    
    self.index = indexPath.item;
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(clickCellWithIndex:)]) {
        [self.delegate clickCellWithIndex:indexPath.item];
    }
    
    [self.collectionView reloadData];
    
}


- (void)reloadCellCollectViewWithIndex:(NSInteger)index firstWithDraw:(BOOL)firstWithDraw{
    self.index = index;
    self.firstWithDraw = firstWithDraw;
    
    [self collectionView:self.collectionView didSelectItemAtIndexPath:[NSIndexPath indexPathForRow:self.index inSection:0]];
}



#pragma mark - getters and setters
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layOut = [[UICollectionViewFlowLayout alloc]init];
        layOut.minimumInteritemSpacing = 0;
        layOut.minimumLineSpacing = 0;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 50 * WIDTHRADIU, ScreenWidth , 130 * WIDTHRADIU) collectionViewLayout:layOut];
        
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[WithDrawCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [_collectionView registerClass:[WithDrawAmountOneSelectCell class] forCellWithReuseIdentifier:@"cell2"];
    }
    return _collectionView;
}

- (NSArray *)dataArray
{
    if (!_dataArray) {
//        _dataArray = @[[NSNumber numberWithDouble:1.0],[NSNumber numberWithDouble:3.0],[NSNumber numberWithDouble:5.0],[NSNumber numberWithDouble:10.0],[NSNumber numberWithDouble:20.0],[NSNumber numberWithDouble:50.0]];
        _dataArray = @[[NSNumber numberWithDouble:1.0],[NSNumber numberWithDouble:3.0],[NSNumber numberWithDouble:10]];
    }
    return _dataArray;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
