//
//  NvDouyinFilter.m
//  SDKDemo
//
//  Created by 刘东旭 on 2018/9/6.
 //
//

#import "NvDouyinFilter.h"
#import "NVHeader.h"
#import "NvFilterCell.h"

@interface NvDouyinFilter()<UICollectionViewDataSource, UICollectionViewDelegate>

//滤镜视图
@property (nonatomic, strong) UIView *filterPanelView;//最外层
@property (nonatomic, strong) UISlider *filterStrengthSlider;//滤镜强度
@property (nonatomic, strong) UILabel *filterStrengthLabel;//强度
@property (nonatomic, strong) UIButton *filterToustBtn;    //无滤镜点击提示按钮
@property (nonatomic, strong) UICollectionView *filterCollectionView;//滤镜滑动视图

@end

@implementation NvDouyinFilter

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor nv_colorWithHexARGB:@"#99000000"];
        [self addFilterView];
    }
    return self;
}

- (void)setFilterDataSource:(NSMutableArray *)filterDataSource {
    _filterDataSource = filterDataSource;
    [_filterCollectionView reloadData];
}

#pragma mark 添加滤镜视图
- (void)addFilterView{
    _filterPanelView = [[UIView alloc] initWithFrame:self.bounds];
    _filterPanelView.backgroundColor = [UIColor nv_colorWithHexARGB:@"#99000000"];
    [self addSubview:_filterPanelView];
    
    UILabel *filterLabel = [[UILabel alloc]initWithFrame:CGRectMake(174 * SCREANSCALE, 10 * SCREANSCALE, 28 * SCREANSCALE, 20 * SCREANSCALE)];;
    filterLabel.text = @"滤镜";
    filterLabel.font = [UIFont systemFontOfSize:14 * SCREANSCALE];
    filterLabel.textColor = UIColor.whiteColor;
    [filterLabel sizeToFit];
    [_filterPanelView addSubview:filterLabel];
        
    UIButton *moreB = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreB setImage:[NvUtils imageWithName:@"NvsFilterMore"] forState:UIControlStateNormal];
    moreB.frame = CGRectMake(13 * SCREANSCALE, 53 * SCREANSCALE, 35 * SCREANSCALE, 25 * SCREANSCALE);
    [_filterPanelView addSubview:moreB];
    [moreB addTarget:self action:@selector(moreClick:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *moreL = [[UILabel alloc]initWithFrame:CGRectMake(20 * SCREANSCALE, moreB.frame.origin.y + moreB.frame.size.height + 15, 30 * SCREANSCALE, 21 * SCREANSCALE)];
    moreL.text = @"更多";
    moreL.textColor = [UIColor nv_colorWithHexARGB:@"#CCFFFFFF"];
    moreL.font = [NvUtils regularFontWithSize:11];
    [_filterPanelView addSubview:moreL];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(58*SCREANSCALE, 70*SCREANSCALE);
    layout.minimumLineSpacing = 10*SCREANSCALE;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 16*SCREANSCALE, 0, 0);
    _filterCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(60 * SCREANSCALE, 43*SCREANSCALE, SCREEN_WDITH - 60 * SCREANSCALE, 70*SCREANSCALE) collectionViewLayout:layout];
    _filterCollectionView.delegate = self;
    _filterCollectionView.dataSource = self;
    _filterCollectionView.backgroundColor = [UIColor clearColor];
    _filterCollectionView.showsHorizontalScrollIndicator = NO;
    [_filterPanelView addSubview:_filterCollectionView];
    [_filterCollectionView registerClass:[NvFilterCell class] forCellWithReuseIdentifier:@"NvFilterCell"];
}

#pragma mark 更多按钮点击
- (void)moreClick:(UIButton *)sender{
    if ([self.delegate respondsToSelector:@selector(filterViewMoreClick:)]) {
        [self.delegate filterViewMoreClick:self];
    }
}

//#pragma mark 滤镜强度调节
//- (void)sliderValueChanged:(UISlider *)slider{
//    if ([self.delegate respondsToSelector:@selector(filterIntensitChanged:)]) {
//        [self.delegate filterIntensitChanged:slider.value];
//    }
//}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _filterDataSource.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NvFilterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NvFilterCell" forIndexPath:indexPath];
    [cell renderCellWithItem:_filterDataSource[indexPath.item]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    for (NvFilterItem *item in _filterDataSource) {
        item.selected = NO;
    }
    NvFilterItem *item = _filterDataSource[indexPath.row];
    item.selected = YES;
    [_filterCollectionView reloadData];

    if (indexPath.item != 0) {
        _filterToustBtn.hidden = YES;
        _filterStrengthSlider.value = 1;
        _filterStrengthSlider.enabled = YES;
        _filterStrengthLabel.textColor = UIColor.whiteColor;
        [_filterStrengthSlider setThumbImage:[NvUtils imageWithName:@"Nvslider"] forState:UIControlStateNormal];

    }else{
        _filterToustBtn.hidden = NO;
        _filterStrengthSlider.value = 0;
        _filterStrengthSlider.enabled = NO;
        _filterStrengthLabel.textColor = [UIColor nv_colorWithHexRGB:@"#909293"];
        [_filterStrengthSlider setThumbImage:[NvUtils imageWithName:@"Nvslider_off"] forState:UIControlStateNormal];
    }
    
    if ([self.delegate respondsToSelector:@selector(filterView:selectFilter:)]) {
        [self.delegate filterView:self selectFilter:item];
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
