//
//  LogMiddleFunctionTableCell.m
//  Open
//
//  Created by 雷俊博 on 2021/7/22.
//  Copyright © 2021 黄江涛 . All rights reserved.
//

#import "LogMiddleFunctionTableCell.h"
#import "LogBtnCollectionViewCell.h"

@interface LogMiddleFunctionTableCell ()<UICollectionViewDelegate,UICollectionViewDataSource>


@property (nonatomic, strong)NSArray  *arrayBtns;
@property (nonatomic, strong)NSArray *arrayImages;

@end

@implementation LogMiddleFunctionTableCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self setupUI];
        
    }
    return self;
}

- (void)setupUI
{
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.collectionView];
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (self.midDataArr.count > 0) {
        return self.midDataArr.count;
    }
    return self.arrayBtns.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    LogBtnCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (self.midDataArr.count > 0) {
        KKRedPackageClickEventModel *model = self.midDataArr[indexPath.row];
        cell.lb_btn.text = model.menuName;
        [cell.img_btn sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KKFindPackageIP,model.menuIcon]]];
        if (model.gifIcon.length > 0) {
            cell.gifImg.hidden = NO;
            [cell.gifImg sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KKFindPackageIP,model.gifIcon]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                       
            }];
        }else{
            cell.gifImg.hidden = YES;
        }
    }else{
        cell.img_btn.image = [UIImage imageNamed:self.arrayImages[indexPath.item]];
        
        cell.lb_btn.text = self.arrayBtns[indexPath.item];
        
        cell.gifImg.hidden = indexPath.item != 3;
        
        cell.gifImg.image = [JobUtil changeImageToGit:@"find95.gif"];
    }
    return cell;
}

/**
 cell的大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    CGFloat width = (CGRectGetWidth(collectionView.bounds) - 40 * WIDTHRADIU) / self.arrayBtns.count;
    CGFloat realWidth = (CGRectGetWidth(collectionView.bounds) - 40 * WIDTHRADIU) - floor(width)*(self.arrayBtns.count - 1) - 1;
    // CGFloat itemW = K_ScreenWidth/array.count;
    if (indexPath.row%self.arrayBtns.count == 0) {
        return CGSizeMake(realWidth, 66 * WIDTHRADIU);
    }else {
        return CGSizeMake(floor(width), 66 * WIDTHRADIU);
    }
}
/**
 每个分区的内边距（上左下右）
 */
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(18 * WIDTHRADIU, 20 * WIDTHRADIU, 18 * WIDTHRADIU, 20 * WIDTHRADIU);
}
/**
 分区内cell之间的最小行间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0.0;
}
/**
 分区内cell之间的最小列间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0.0;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
        return CGSizeZero;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",indexPath);
    if (self.midDataArr.count > 0) {
        KKRedPackageClickEventModel *model = self.midDataArr[indexPath.row];
        if (self.delegate &&[self.delegate respondsToSelector:@selector(LogMiddleFunctionTableCellClickWithModel:)]) {
            [self.delegate LogMiddleFunctionTableCellClickWithModel:model];
        }
        return;
    }
    
    if (self.delegate &&[self.delegate respondsToSelector:@selector(LogMiddleFunctionTableCellClickWithIndex:)]) {
        [self.delegate LogMiddleFunctionTableCellClickWithIndex:indexPath.item];
    }
}

- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor colorWithHex:0x000000 alpha:0.1];
}

- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor clearColor];
}

#pragma mark - getters and setters
- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc]init];
        
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 102 * WIDTHRADIU) collectionViewLayout:fl];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsVerticalScrollIndicator = NO;
        
        // _collectionView.backgroundColor = K_GrayColor;
        
        _collectionView.backgroundColor = [UIColor whiteColor];
        //布局
        fl.minimumLineSpacing = 0;
        fl.minimumInteritemSpacing = 0;
        
        //cell注册
        [_collectionView registerNib:[UINib nibWithNibName:@"LogBtnCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
        
       
        
        
    }
    return _collectionView;
}
- (void)refreshWithData:(NSArray *)arr{
    self.midDataArr = arr;
    
    [self.collectionView reloadData];
}
- (NSArray *)arrayBtns
{
    if (!_arrayBtns) {
        _arrayBtns = @[
                       @"健康统计",@"金币抽奖",@"我的奖品",@"话费充值"
                       ];
    }
    return _arrayBtns;
}

- (NSArray *)arrayImages
{
    if (!_arrayImages) {
        _arrayImages = @[
                       @"my_statics_normal",@"my_lottery_normal",@"my_prize_normal",@"my_feecharge_normal"
                       ];
    }
    return _arrayImages;
}


- (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
