//

//

#import "GovTopGride.h"
#import "GovTopGrideCell.h"
@interface GovTopGride ()<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 GovTopGride
- (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, 160*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:@"GovTopGrideCell" bundle:nil] forCellWithReuseIdentifier:@"GovTopGrideCell"];
    }
    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{
    GovTopGrideCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GovTopGrideCell" 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;
        [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 / 4.0;
    CGFloat height = 75*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 5*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 GovTopGrideItemClick:indexPath.item:model];
        }else{
            [self.delegate GovTopGrideItemClick: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_top_new_01.png",@"gov_top_new_02.png",@"gov_top_new_03.png",@"gov_top_new_04.png",@"gov_top_new_05.png",@"gov_top_new_06.png",@"gov_top_new_07.png",@"gov_top_new_08.png"];
    }
    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":@"GovNewTopView",
        @"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
