//
//  KWMRecommendHeader.m
//  iCemarose
//
//  Created by HouWeiBin on 2017/7/10.
//  Copyright © 2017年 kollway. All rights reserved.
//

#import "KWMRecommendView.h"
#import "KWMCollectionCell.h"

@interface KWMRecommendView()

@property (nonatomic,weak) IBOutlet UIView *vView;

@property (nonatomic,weak) IBOutlet UICollectionView *cvRecommend;

@property (nonatomic,weak) IBOutlet UILabel *lbTitle;

@end

@implementation KWMRecommendView

+(NSString *)kwmTag{
    return @"KWMRecommendView";
}

- (id)init{
    if (self=[super init]){
        [self addView];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self addView];
    }
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self =[super initWithFrame:frame]) {
        [self addView];
    }
    return self;
}

-(void)addView{
    [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class])
                                  owner:self
                                options:nil];
    
    self.vView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
    [self addSubview:self.vView];
    [self initView];
}

-(void)initView{
    self.cvRecommend.delegate = self;
    self.cvRecommend.dataSource = self;
    
    [self.cvRecommend registerNib:[UINib nibWithNibName:NSStringFromClass([KWMCollectionCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([KWMCollectionCell class])];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 20, 0, 0);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.cvRecommend.collectionViewLayout = layout;
}

-(void)setTitle:(NSString *)title{
    _title = title;
    if(self.lbTitle){
        [self.lbTitle setText:title];
    }
}

-(void)setProductArray:(NSArray *)productArray{
    _productArray = productArray;
    [self removeSameProduct];
    [self.cvRecommend reloadData];
}

-(void)setBoughtProductIdArray:(NSArray *)boughtProductIdArray{
    _boughtProductIdArray = boughtProductIdArray;
    [self removeSameProduct];
    [self.cvRecommend reloadData];
}

-(void)removeSameProduct{
    if(self.productArray && self.boughtProductIdArray){
        _productArray = [self.productArray rx_filterWithBlock:^BOOL(BUYProduct *each){
            return (![self.boughtProductIdArray containsObject:each.identifier]);
        }];
    }
}

#pragma mark - UICollectionViewDataSource
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    KWMCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([KWMCollectionCell class]) forIndexPath:indexPath];
    
    BUYProduct *product = [self.productArray objectAtIndex:indexPath.row];
    [cell setData:product];
    cell.isMore = indexPath.row == 9;
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeMake(134, 180);
    return size;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    NSInteger productCount = self.productArray?self.productArray.count:0;
    NSInteger boughtCount = self.boughtProductIdArray?self.boughtProductIdArray.count:0;
    if(productCount > 9){
        return 10;
    }
    if(productCount + boughtCount > 9){
        return productCount+1;
    }
    return self.productArray?self.productArray.count:0;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    NSInteger productCount = self.productArray?self.productArray.count:0;
    NSInteger boughtCount = self.boughtProductIdArray?self.boughtProductIdArray.count:0;
    BOOL hasMore = (productCount > 9) || (productCount + boughtCount > 9);
    if([self collectionView:self.cvRecommend numberOfItemsInSection:0] == indexPath.row + 1
       && hasMore){
        if(self.delegate){
            [self.delegate kwm_gotoRecommendPage:self];
        }
    }else{
        BUYProduct *product = [self.productArray objectAtIndex:indexPath.row];
        if(self.delegate){
            [self.delegate kwm_onClickProduct:product];
        }
    }

}

@end