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

#import "LogHiddenFunctionItemsCell.h"
#import "LogFunctionCollectionViewCell.h"

@interface LogHiddenFunctionItemsCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong)UICollectionView *collectionView;

@property (nonatomic, strong)NSArray *titleArray;

@property (nonatomic, strong)NSArray *iconArray;

@end

@implementation LogHiddenFunctionItemsCell
- (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, 175 * 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
{
    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];
    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.delegate && [self.delegate respondsToSelector:@selector(LogHiddenFunctionItemsCellClickWithIndex:)]) {
        [self.delegate LogHiddenFunctionItemsCellClickWithIndex: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
- (NSArray *)titleArray
{
    if (!_titleArray) {
        _titleArray = @[@"个人信息",@"实名认证",@"社保认证记录",@"推荐有礼",@"意见反馈",@"设置",@"关于"];
    }
    return _titleArray;
}

- (NSArray *)iconArray
{
    if (!_iconArray) {
        _iconArray = @[@"log_personInfo",@"log_auth",@"log_shebaoRecord",@"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
