//
//  ERTCBeautySkinFilterView.m
//  ERTCDemo
//
//  Created by wangjinlong23 on 2022/8/16.
//

#import "ERTCBeautySkinFilterView.h"
#import "ERTCBeautyItemCollectionViewCell.h"

@interface ERTCBeautySkinFilterView()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation ERTCBeautySkinFilterView

-(instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self setupCollectionView];
    }
    
    return self;
}

-(void)reloadFilterView {
    [self.collectionView reloadData];
}

-(void)setModels:(NSArray<ERTCBeautyModel *> *)models {
    _models = models;
    [self reloadFilterView];
}

#pragma mark - UICollectionViewDelegate// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ERTCBeautyItemCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[ERTCBeautyItemCollectionViewCell cellReuseId]
                                                                                        forIndexPath:indexPath];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    ERTCBeautyModel *model = self.models[indexPath.row];
    ERTCBeautyItemCollectionViewCell *vCell = (ERTCBeautyItemCollectionViewCell *)cell;
    vCell.model = model;
}

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ERTCBeautyModel *model = self.models[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(filterSkinViewDidSelectedFilter:filterView:)]) {
        [self.delegate filterSkinViewDidSelectedFilter:model filterView:self];
    }
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.models.count;
}

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

#pragma mark - setupCollectionView
- (void)setupCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    [self.collectionView registerClass:[ERTCBeautyItemCollectionViewCell class] forCellWithReuseIdentifier:[ERTCBeautyItemCollectionViewCell cellReuseId]];

    self.collectionView.backgroundColor = self.backgroundColor;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;

    [self addSubview:self.collectionView];

    [self.collectionView ysmas_makeConstraints:^(YSMASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
}
@end
