//

//

#import "MiniPayBottomView.h"
#import "IdPhotoBottomCell.h"
@interface MiniPayBottomView ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)UICollectionViewFlowLayout *flowLayout;
@property (nonatomic, strong) NSArray *titleArray;
@property (nonatomic, strong) NSArray *imageNames;
@property (nonatomic, strong) NSMutableArray *topDataArray;
@property (nonatomic) NSInteger cellIndex;
@end
@implementation MiniPayBottomView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor clearColor];
        [self addSubview:self.collectionView];
    }
    return self;
}
#pragma mark-初始化collectionView
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth-20*WIDTHRADIU, 100*WIDTHRADIU) collectionViewLayout:self.flowLayout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.scrollEnabled = NO;
        //设置collectionView 是否可以滑动
        _collectionView.alwaysBounceVertical = NO;
        //隐藏滚动条
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        [_collectionView registerNib:[UINib nibWithNibName:@"IdPhotoBottomCell" bundle:nil] forCellWithReuseIdentifier:@"IdPhotoBottomCell"];
    }
    return _collectionView;
}
- (UICollectionViewFlowLayout *)flowLayout {
    if (!_flowLayout) {
        _flowLayout = [[UICollectionViewFlowLayout alloc] init];
        _flowLayout.minimumLineSpacing = 0;
        _flowLayout.minimumInteritemSpacing = 0;
        //        _flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    }
    return _flowLayout;
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.titleArray.count;
}


- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    IdPhotoBottomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"IdPhotoBottomCell" forIndexPath:indexPath];
    cell.titleLab.font = [UIFont systemFontOfSize:16.0 * WIDTHRADIU];
    if(self.topDataArray.count>0){
        KKRedPackageClickEventModel *model = self.topDataArray[indexPath.item];
        cell.titleLab.text=model.menuName;
        NSString*tag=model.menuTag;
        [cell.icon sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KKFindPackageIP,model.menuIcon]]];
    }else{
        cell.titleLab.text =  self.titleArray[indexPath.item];
        cell.icon.image = [UIImage imageNamed:self.imageNames[indexPath.item]];
    }
    NSLog(@"渲染:%ld",(long)indexPath.item);
    
    
    return cell;
}

/**
 cell的大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = (ScreenWidth-20*WIDTHRADIU) / 4.0;
    CGFloat height = 70*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.0;
}
/**
 分区内cell之间的最小列间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0.0;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate ) {
        if(self.topDataArray.count>0){
            KKRedPackageClickEventModel *model=self.topDataArray[indexPath.item];
            [self.delegate IdPhotoBottomWithItem:indexPath.item:model];
        }else{
            [self.delegate IdPhotoBottomWithItem:indexPath.item:nil];
        }
    }
}

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

- (void)collectionView:(UICollectionView *)colView  didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath{
//恢复颜色
    UICollectionViewCell *cell = (UICollectionViewCell*)[colView cellForItemAtIndexPath:indexPath];
    [cell setBackgroundColor:[UIColor clearColor]];

}

- (void)reloadData{
    
    [self getMenu];
    [self.collectionView reloadData];
}

#pragma mark - getters
- (NSArray *)titleArray
{
    if (!_titleArray) {
        _titleArray = @[@"优惠充值",@"签到打卡",@"存钱罐",@"限时优惠"];
    }
    return _titleArray;
}

- (NSArray *)imageNames
{
    if (!_imageNames) {
        _imageNames = @[@"id_photo_01",@"id_photo_02",@"id_photo_03",@"id_photo_04"];
    }
    return _imageNames;
}

- (void) getMenu{
    NSString*version = nil;
    NSString*v = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSArray*vArr = [v componentsSeparatedByString:@"."];
    for (int i = 0; i < vArr.count; i++) {
        if (version == nil) {
            version = [NSString stringWithFormat:@"%@",vArr[i]];
        }else{
            version = [version stringByAppendingFormat:@"%@",vArr[i]];
        }
    }
    NSString *url1 = [NSString stringWithFormat:@"%@dynamic/menu/get",[NetworkConfig ipYinShuConfig]];
    NSDictionary *params1 = @{
        @"configName":@"IdPhotoCenter",
        @"appName":@"kksh",
        @"version":version,
        @"systemType":@"iOS",
        @"uid":[NSString stringWithFormat:@"%ld",(long)[getPeopleID PeopleID]]
    };
    [NetworkRequest Post15:url1 Params:params1 Success:^(id _Nonnull objc) {
        NSLog(@"首页顶部按钮===objc:%@",objc);
        if ([objc isKindOfClass:[NSDictionary class]]) {
            NSString *state = [NSString stringWithFormat:@"%@",objc[@"state"]];
            if ([state isEqualToString:@"200"]) {
                if ([objc[@"result"] isKindOfClass:[NSArray class]]) {
                    self.topDataArray = [NSMutableArray array];
                    for (NSDictionary *dic in objc[@"result"]) {
                        KKRedPackageClickEventModel *model = [KKRedPackageClickEventModel mj_objectWithKeyValues:dic];
                        [self.topDataArray addObject:model];
                    }
                }
                [self.collectionView reloadData];
            }
        }
    } Failure:^(NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}


@end
