//

//

#import "HometTopView.h"
#import "HometTopCell.h"
@interface HometTopView ()<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;
@end
@implementation HometTopView
- (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, 5, ScreenWidth, 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:@"HometTopCell" bundle:nil] forCellWithReuseIdentifier:@"HometTopCell"];
    }
    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
{
    HometTopCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HometTopCell" forIndexPath:indexPath];
    cell.titleLab.font = [UIFont boldSystemFontOfSize:18.0 * WIDTHRADIU];
    if(self.topDataArray.count>0){
        KKRedPackageClickEventModel *model = self.topDataArray[indexPath.item];
        cell.titleLab.text=model.menuName;
        [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]];
    }
    
    return cell;
}
/**
 cell的大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    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.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 HometTopViewWithItem:indexPath.item :model];
        }else{
            [self.delegate HometTopViewWithItem: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{
//    if (self.hiddenButton) {
//        self.titleArray = @[@"电子社保卡",@"认证领钱",@"医保卡",@"优抚核查"];
//        self.imageNames = @[@"home_top_new_01",@"home_top_new_02",@"home_top_new_03",@"home_syoufu"];
//    }else{
//        self.titleArray = @[@"电子社保卡",@"认证领钱",@"医保卡",@"养老钱包"];
//        self.imageNames = @[@"home_top_new_01",@"home_top_new_02",@"home_top_new_03",@"home_top_new_04"];
//    }
    self.titleArray = @[@"电子社保卡",@"认证领钱",@"医保卡",@"养老钱包"];
    self.imageNames = @[@"home_top_new_01",@"home_top_new_02",@"home_top_new_03",@"home_top_new_04"];
    [self getTopMenu];
    [self.collectionView reloadData];
}
#pragma mark - getters
- (NSArray *)titleArray
{
    if (!_titleArray) {
        //_titleArray = @[@"电子社保卡",@"认证领钱",@"医保卡",@"优抚核查"];
        _titleArray = @[@"电子社保卡",@"认证领钱",@"医保卡",@"养老钱包"];
    }
    return _titleArray;
}

- (NSArray *)imageNames
{
    if (!_imageNames) {
        //_imageNames = @[@"home_top_new_01",@"home_top_new_02",@"home_top_new_03",@"home_syoufu"];
        _imageNames = @[@"home_top_new_01",@"home_top_new_02",@"home_top_new_03",@"home_top_new_04"];
    }
    return _imageNames;
}

- (void) getTopMenu{
    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":@"HomeTopMenu",
        @"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
