//

//

#import "GoldBottomView.h"
#import "GoldBottomViewCell.h"
@interface GoldBottomView ()<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 GoldBottomView
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor whiteColor];
        UILabel *tip=[[UILabel alloc]initWithFrame:CGRectMake(15*WIDTHRADIU, 10*WIDTHRADIU, 80*WIDTHRADIU, 20*WIDTHRADIU)];
        tip.text=@"热门好物";
        tip.font = [UIFont boldSystemFontOfSize:17 * WIDTHRADIU];
        tip.textColor=[UIColor colorWithHex:0x4c4c4c];
       
        UILabel *moreGood=[[UILabel alloc]initWithFrame:CGRectMake(ScreenWidth-95*WIDTHRADIU, 10*WIDTHRADIU, 80*WIDTHRADIU, 20*WIDTHRADIU)];
        moreGood.text=@"更多商品 >";
        moreGood.userInteractionEnabled=YES;
        UITapGestureRecognizer *moreGoodRz = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(moreGoodClick:)];
        [moreGood addGestureRecognizer:moreGoodRz];
        moreGood.font = [UIFont systemFontOfSize:14 * WIDTHRADIU];
        moreGood.textColor=[UIColor colorWithHex:0x4c4c4c];
        [self addSubview:moreGood];
        [self addSubview:tip];
        [self addSubview:self.collectionView];
    }
    return self;
}
//更多商品
-(void)moreGoodClick:(UITapGestureRecognizer *)tap{
    
    [[clickEvent instance] clickEventRooterVC:self.rootVc getFederatedLoginRequestWithUrl:@"https://web.kanface.com:444/common/forward/JinBiShangChengUrl" menuName:@"金币商城"];
    
}
#pragma mark-初始化collectionView
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10*WIDTHRADIU, 45*WIDTHRADIU, ScreenWidth-20*WIDTHRADIU, 440*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:@"GoldBottomViewCell" bundle:nil] forCellWithReuseIdentifier:@"GoldBottomViewCell"];
    }
    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{
    GoldBottomViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GoldBottomViewCell" forIndexPath:indexPath];
    cell.titleLab.font = [UIFont boldSystemFontOfSize:16.0 * WIDTHRADIU];
    cell.goldText.layer.cornerRadius = 13 * WIDTHRADIU;
    cell.goldText.layer.masksToBounds = YES;
    if(self.topDataArray.count>0){
        NSDictionary *result = self.topDataArray[indexPath.item];
        cell.titleLab.text=result[@"name"];
        NSString *imageUrl=result[@"thumbnailImage"];
        NSString *scorePrice=result[@"scorePrice"];
        NSString *cashPrice=result[@"cashPrice"];
        if([cashPrice isEqualToString:@"0"]){
            cell.goldText.text=[NSString stringWithFormat:@"%@金币",scorePrice];
        }else{
            cell.goldText.text=[NSString stringWithFormat:@"%@金币%@元",scorePrice,cashPrice];
        }
        [cell.icon sd_setImageWithURL:imageUrl];
    }else{
        cell.titleLab.text =  self.titleArray[indexPath.item];
        cell.icon.image = [UIImage imageNamed:self.imageNames[indexPath.item]];
    }
    
    cell.layer.cornerRadius=10*WIDTHRADIU;
    cell.layer.masksToBounds=YES;
    cell.layer.borderColor=[UIColor colorWithHex:0xcccccc].CGColor;
    cell.layer.borderWidth=0.5*WIDTHRADIU;
    //cell.backgroundColor=[UIColor colorWithHex:0xcccccc];
    NSLog(@"渲染:%ld",(long)indexPath.item);
    
    
    return cell;
}

/**
 cell的大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = (ScreenWidth-26*WIDTHRADIU) / 2.0;
    CGFloat height = 210*WIDTHRADIU;
    return CGSizeMake(width, height);
}
/**
 每个分区的内边距（上左下右）
 */
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 3*WIDTHRADIU, 0);
}
/**
 分区内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){
            NSDictionary *model=self.topDataArray[indexPath.item];
            [self.delegate GoldBottomViewItemClick:indexPath.item:model];
        }else{
            [self.delegate GoldBottomViewItemClick: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 *url=[NSString stringWithFormat:@"https://web.kanface.com:444/mall/goods/list/all?uid=%@",[NSString stringWithFormat:@"%ld",(long)[getPeopleID PeopleID]]];
    NSDictionary *params = @{
        @"uid":[NSString stringWithFormat:@"%ld",(long)[getPeopleID PeopleID]]
    };
    [NetworkRequest Post15:url Params:params 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 = objc[@"result"];
                }
                [self.collectionView reloadData];
            }
        }
    } Failure:^(NSError *error) {
        NSLog(@"error:%@",error);
    }];
    
}


@end
