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

#import "LogFunctionItemsCell.h"
#import "LogFunctionCollectionViewCell.h"
#import "KKRedPackageClickEventModel.h"

@interface LogFunctionItemsCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong)UICollectionView *collectionView;

@property (nonatomic, strong)NSArray *titleArray;

@property (nonatomic, strong)NSArray *iconArray;

@end

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

- (void)setupUI
{
    //UICollectionView
    UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc]init];
    //布局
    fl.minimumLineSpacing = 1.0;
    fl.minimumInteritemSpacing = 1.0;
           
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 340 * WIDTHRADIU) collectionViewLayout:fl];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.scrollEnabled = NO;
   
    // _collectionView.backgroundColor = K_GrayColor;
   
    _collectionView.backgroundColor = [UIColor whiteColor];
    
   
    //cell注册
    [_collectionView registerClass:[LogFunctionCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [self.contentView addSubview:_collectionView];
}

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

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    LogFunctionCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (self.DataArr.count > 0) {
        KKRedPackageClickEventModel *model = self.DataArr[indexPath.row];
        cell.lb_title.text = model.menuName;
        [cell.icon 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;
        }
//        if ([cell.lb_title.text isEqualToString:@"金币兑换"]) {
//            cell.gifImg.hidden = NO;
//            cell.gifImg.image = [JobUtil changeImageToGit:@"freecon.gif"];
//        }else{
//            cell.gifImg.hidden = YES;
//        }
    }else{
        cell.lb_title.text = self.titleArray[indexPath.item];
        cell.icon.image = [UIImage imageNamed:self.iconArray[indexPath.item]];
    }
    return cell;
}

/**
 cell的大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    

    CGFloat width = (ScreenWidth - 20 * WIDTHRADIU - 3)/4;
    CGFloat realWidth = (ScreenWidth - 20 * WIDTHRADIU - 3)  - 3 * width - 1;
    
    if (indexPath.row %4 != 3) {
        return CGSizeMake(width, 74 * WIDTHRADIU );
    }
    
    return CGSizeMake(realWidth, 74 * WIDTHRADIU);
    
}

/**
 每个分区的内边距（上左下右）
 */
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    

    
    return UIEdgeInsetsMake(10 * WIDTHRADIU, 10 * WIDTHRADIU, 10 * WIDTHRADIU, 10 * WIDTHRADIU);
}


/**
 分区内cell之间的最小行间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    
    return 7 * WIDTHRADIU;
}
/**
 分区内cell之间的最小列间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 1.0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.DataArr.count > 0) {
        KKRedPackageClickEventModel *model = self.DataArr[indexPath.row];
        if (self.delegate && [self.delegate respondsToSelector:@selector(goToClickRedPackageVCWithModel:)]) {
            [self.delegate goToClickRedPackageVCWithModel:model];
        }
        
        return;
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(LogFunctionItemsCellClickWithIndex:)]) {
        [self.delegate LogFunctionItemsCellClickWithIndex: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];
}
- (void)refreshWithData:(NSArray *)arr{
    self.DataArr = arr;

    [self.collectionView reloadData];
}
#pragma mark - getters and setters
- (NSArray *)titleArray
{
    if (!_titleArray) {
//        _titleArray = @[@"个人信息",@"实名认证",@"我的资产",@"收益排行榜",@"社保认证记录",@"社保采集记录",@"找工作简历",@"推荐给好友",@"意见反馈",@"设置",@"关于"];
        _titleArray = @[@"个人信息",@"实名认证",@"我的资产",@"收益排行榜",@"社保认证记录",@"找工作简历",@"推荐有礼",@"意见反馈",@"设置",@"关于"];
    }
    return _titleArray;
}

- (NSArray *)iconArray
{
    if (!_iconArray) {
//        _iconArray = @[@"log_personInfo",@"log_auth",@"log_MyAssets",@"log_rank",@"log_shebaoRecord",@"log_shebaoCollection",@"log_FindJob",@"log_share",@"log_feedBack",@"log_setting",@"log_about"];
        _iconArray = @[@"log_personInfo",@"log_auth",@"log_MyAssets",@"log_rank",@"log_shebaoRecord",@"log_FindJob",@"log_share",@"log_feedBack",@"log_setting",@"log_about"];
    }
    return _iconArray;
}



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