//

//

#import "GovBottomGride.h"
#import "GovBottomGrideCell.h"
@interface GovBottomGride ()<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 GovBottomGride
- (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, 180*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:@"GovBottomGrideCell" bundle:nil] forCellWithReuseIdentifier:@"GovBottomGrideCell"];
    }
    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{
    GovBottomGrideCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GovBottomGrideCell" forIndexPath:indexPath];
    cell.titleLab.font = [UIFont systemFontOfSize:14.0 * WIDTHRADIU];
    UIImageView *image=cell.icon;
    image.contentMode=UIViewContentModeLeft;
    [image mas_makeConstraints:^(MASConstraintMaker *make) {
        
    }];
    if(self.topDataArray.count>0){
        KKRedPackageClickEventModel *model = self.topDataArray[indexPath.item];
        cell.titleLab.text=model.menuName;
        [image sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",KKFindPackageIP,model.menuIcon]]];
    }else{
        cell.titleLab.text =  self.titleArray[indexPath.item];
        image.image = [UIImage imageNamed:self.imageNames[indexPath.item]];
    }
    NSLog(@"渲染:%ld",(long)indexPath.item);
    cell.layer.cornerRadius=10*WIDTHRADIU;
    cell.layer.masksToBounds=YES;
    cell.backgroundColor = [UIColor colorWithHex:0xE6EFF6];
    return cell;
}

/**
 cell的大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = (ScreenWidth-34*WIDTHRADIU) / 2.0;
    CGFloat height = 50*WIDTHRADIU;
    return CGSizeMake(width, height);
}
/**
 每个分区的内边距（上左下右）
 */
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(7*WIDTHRADIU, 0, 0, 7*WIDTHRADIU);
}
/**
 分区内cell之间的最小行间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 8*WIDTHRADIU;
}
/**
 分区内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 GovBottomGrideItemClick:indexPath.item:model];
        }else{
            [self.delegate GovBottomGrideItemClick: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 = @[@"gov_bottom_01",@"gov_bottom_02",@"gov_bottom_03",@"gov_bottom_04",@"gov_bottom_05",@"gov_bottom_06"];
    }
    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":@"GovNewHotView",
        @"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
