//
//  HomeCollectionCell.m
//  Open
//
//  Created by 雷俊博 on 2020/8/4.
//  Copyright © 2020 黄江涛 . All rights reserved.
//

#import "HomeCollectionCell.h"
#import "HomeCollectionItemCell.h"
#import "HomeCollectionItem2Cell.h"



@interface HomeCollectionCell ()<UICollectionViewDelegate,UICollectionViewDataSource>


@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (nonatomic, strong)NSArray* titleArray;

@property (nonatomic, strong)NSArray*imageNames;

@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *flow;

//@property (nonatomic, strong)HomeCollectionItem2Cell* gyCell;

//@property (nonatomic, strong)NSArray* titleArray2;
//
//@property (nonatomic, strong)NSArray*imageNames2;
@end

@implementation HomeCollectionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self.collectionView registerNib:[UINib nibWithNibName:@"HomeCollectionItem2Cell" bundle:nil] forCellWithReuseIdentifier:@"cell2"];
//    self.collectionView.scrollEnabled = NO;
    self.flow.minimumLineSpacing = 0;
    self.flow.minimumInteritemSpacing = 0;
    self.flow.scrollDirection = UICollectionViewScrollDirectionVertical;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
//    if (section == 0) {
//        return self.titleArray.count;
//    }
    if (self.dataArray.count > 0 && !self.hiddenButton) {
        return self.dataArray.count;
    }
    return self.titleArray.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    HomeCollectionItem2Cell*cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
    //cell.icon.frame.size.width==100;
    //cell.icon.frame.size.height==100;
    cell.lb_title.font = [UIFont systemFontOfSize:17.0 * WIDTHRADIU];
    if (self.dataArray.count > 0 && !self.hiddenButton) {
        KKRedPackageClickEventModel *model = self.dataArray[indexPath.item];
        cell.lb_title.text = model.menuName;
        [cell.icon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KKFindPackageIP,model.menuIcon]]];
        if (model.gifIcon.length > 0) {
            cell.imgGif.hidden = NO;
            cell.imgGif2.hidden = YES;
            [cell.imgGif sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KKFindPackageIP,model.gifIcon]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                        cell.imgGifWidth.constant = image.size.width / 2;
                        cell.imgGifHeight.constant = image.size.height / 2;
            }];
        }else{
            cell.imgGif.hidden = YES;
            cell.imgGif2.hidden = YES;
        }
    }else{
        cell.lb_title.text =  self.titleArray[indexPath.item];
        cell.icon.image = [UIImage imageNamed:self.imageNames[indexPath.item]];
        
    }
    return cell;

    
}

/**
 cell的大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
//    if (indexPath.section == 0) {
//       CGFloat width = ScreenWidth / 4.0;
//       CGFloat realWidth = ScreenWidth - 3*width;
//       CGFloat height = 104 * WIDTHRADIU;
//       CGSize size = CGSizeZero;
//       if (indexPath.item != 3) {
//           size = CGSizeMake(width, height);
//       }else{
//           size = CGSizeMake(realWidth, height);
//       }
//
//        return size;
//    }
    
    CGFloat width = ScreenWidth / 4.0 ;
    CGFloat height = 100*WIDTHRADIU;
    return  CGSizeMake(width, height);;
   
}
/**
 每个分区的内边距（上左下右）
 */
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}
/**
 分区内cell之间的最小行间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
   
    return 0;
}
/**
 分区内cell之间的最小列间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0.0;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataArray.count > 0 && !self.hiddenButton) {
        KKRedPackageClickEventModel *model = self.dataArray[indexPath.item];
        if (self.Itemdelegate && [self.Itemdelegate respondsToSelector:@selector(HomeItemCell:)]) {
            [self.Itemdelegate HomeItemCell:model];
        }
        return;
    }
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(HomeCollectionCell:didSelectItemAtIndexPath:)]) {
        [self.delegate HomeCollectionCell:self didSelectItemAtIndexPath:indexPath];
    }
    
}

-(void) collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
//设置选中时的颜色
    UICollectionViewCell *cell = (UICollectionViewCell*)[collectionView cellForItemAtIndexPath:indexPath];
    
    
    [cell setBackgroundColor:[UIColor colorWithHex:0xe5e5e5]];
}
 
- (void)collectionView:(UICollectionView *)colView  didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
//恢复颜色
    UICollectionViewCell *cell = (UICollectionViewCell*)[colView cellForItemAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor clearColor]];
    
}

- (void)setupCellWithModel:(DHCWNativeAdDataObject *)model
                  delegate:(id<DHCWNativeAdViewDelegate>)delegate loca:(id<DHCWNativeLocaVideoAdDelegate>)localDelegate
        rootViewController:(UIViewController *)rootViewController
{
    self.model = model;
    self.dhcDelegate = delegate;
    self.dhcLocaDelegate = localDelegate;
    self.rootViewController = rootViewController;
    //[self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:2]]];
    [self.collectionView reloadData];
}


#pragma mark - 公共方法
- (void)reloadData{
    self.titleArray = @[@"政务便民",@"缴新农合",@"认证查询",@"打卡领钱",@"计算器",@"照相馆",@"银行存款",@"更多功能"];
    self.imageNames = @[@"home_b_01",@"home_b_02",@"home_b_03",@"home_b_04",@"home_b_05",@"home_b_06",@"home_b_07",@"home_b_08"];
    
//    if (self.hiddenButton) {
//        self.titleArray = @[@"认证查询",@"申报查询",@"社保计算器",@"视频",@"智能客服",@"照相馆",@"诸葛找房",@"菜谱",@"万年历",@"周边游",@"话费充值",@"快递查询",@"抢红包",@"酒店",@"更多功能"];
//        self.imageNames = @[@"home_authQuery",@"home_noteProgress",@"home_SecurityComputer",@"home_video",@"home_smartService_blue",@"home_PhotoStudio",@"home_LookingRoom",@"home_Recipe",@"home_Calendar",@"home_Travel",@"home_Morecredit",@"home_ExpressQuery",@"Getenvelope",@"home_hotel",@"home_moreIcons"];
//    }else{
//        self.titleArray = @[@"政务便民",@"缴社保",@"认证查询",@"找工作",@"看病买药",@"电影券",@"团油",@"话费充值",@"照相馆",@"限时优惠",@"免费果园",@"打卡领钱",@"存钱罐",@"站长中心",@"更多功能"];
//        self.imageNames = @[@"home_zhengwu",@"home_daijiao",@"home_authQuery",@"home_yijiuye",@"home_doctor",@"Movieticket",@"home_Mass",@"home_Morecredit",@"home_PhotoStudio",@"Discount",@"home_freeFruit",@"home_daka",@"home_bank",@"home_parter_icon",@"home_moreIcons"];
//    }
    [self.collectionView reloadData];
}


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

    // Configure the view for the selected state
}

@end
