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

#import "ERTCBeautyBlurFilterView.h"
#import "ERTCBeautyItemBlurCollectionViewCell.h"

@interface ERTCBeautyBlurFilterView ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@end

@implementation ERTCBeautyBlurFilterView

-(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 {
    ERTCBeautyItemBlurCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:[ERTCBeautyItemBlurCollectionViewCell cellReuseId]
                                                                                        forIndexPath:indexPath];
    
    return cell;
}

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

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
//    NSLog(@"----didEndDisplayingCell %d ----", (int)indexPath.row);
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    ERTCBeautyModel *model = self.models[indexPath.row];
    if ([self.delegate respondsToSelector:@selector(filterBlurViewDidSelectedFilter:filterView:)]) {
        [self.delegate filterBlurViewDidSelectedFilter: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, 83);
    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:[ERTCBeautyItemBlurCollectionViewCell class] forCellWithReuseIdentifier:[ERTCBeautyItemBlurCollectionViewCell cellReuseId]];

    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;

    [self addSubview:self.collectionView];

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


@end
