//
//  NvCaptureFilterView.m
//  SDKDemo
//
//  Created by ms20180425 on 2018/11/29.
 //
//

#import "NvCaptureFilterView.h"
#import "NvHeader.h"
#import "NvCaptureFilterCell.h"
#import "NvCaptureFilterModel.h"

@interface NvCaptureFilterView()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UIView *topView;       //上半部分视图
@property (nonatomic, strong) UIView *bottomView;    //下半部分视图
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UISlider *strengthSlider;//滤镜强度
@property (nonatomic, strong) UICollectionView *collectionView;//滤镜滑动视图

@end

@implementation NvCaptureFilterView

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

#pragma mark 添加子视图
- (void)addSubviews:(CGRect)rect{
    CGFloat topViewHeight = 50 * SCREANSCALE;
    CGFloat bottomViewHeight = rect.size.height - topViewHeight;
    
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, rect.size.width, topViewHeight)];
    self.topView.backgroundColor = UIColor.clearColor;
    self.topView.hidden = YES;
    [self addSubview:self.topView];

    self.strengthSlider = [[UISlider alloc]init];
    [self.strengthSlider setMinimumValue:0.0];
    [self.strengthSlider setMaximumValue:1.0];
    self.strengthSlider.minimumTrackTintColor = [UIColor nv_colorWithHexARGB:@"#CCFFFFFF"];
    self.strengthSlider.maximumTrackTintColor = [UIColor nv_colorWithHexARGB:@"#CCFFFFFF"];
    [self.strengthSlider setThumbImage:[NvUtils imageWithName:@"NvsliderWhite"] forState:UIControlStateNormal];
    [self.strengthSlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
    [self.topView addSubview:self.strengthSlider];
    [self.strengthSlider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.topView.mas_bottom).offset(-21 * SCREANSCALE);
        make.centerX.equalTo(self.topView.mas_centerX);
        make.width.offset(281 * SCREANSCALE);
        make.height.offset(10 * SCREANSCALE);
    }];
    
    self.bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, self.topView.frame.size.height, rect.size.width, bottomViewHeight)];
    self.bottomView.backgroundColor = [UIColor nv_colorWithHexARGB:@"#99000000"];
    [self addSubview:self.bottomView];
    
    UIButton *moreB = [UIButton buttonWithType:UIButtonTypeCustom];
    [moreB setImage:[NvUtils imageWithName:@"NvsFilterMore"] forState:UIControlStateNormal];
    [moreB addTarget:self action:@selector(moreClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.bottomView addSubview:moreB];
    [moreB mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bottomView).offset(30 * SCREANSCALE);
        make.left.equalTo(self.bottomView).offset(13 * SCREANSCALE);
        make.width.offset(35 * SCREANSCALE);
        make.height.offset(25 * SCREANSCALE);
    }];
    
    UILabel *moreL = [[UILabel alloc]init];
    moreL.text = NSLocalizedString(@"更多", @"更多");
    moreL.textColor = [UIColor nv_colorWithHexARGB:@"#CCFFFFFF"];
    moreL.font = [NvUtils regularFontWithSize:11];
    [self.bottomView addSubview:moreL];
    [moreL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(moreB.mas_bottom).offset(19 * SCREANSCALE);
        make.centerX.equalTo(moreB.mas_centerX);
    }];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(49*SCREANSCALE, 75*SCREANSCALE);
    layout.minimumLineSpacing = 10*SCREANSCALE;
    layout.minimumInteritemSpacing = 0;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0,0,0,0) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    [self.collectionView registerClass:[NvCaptureFilterCell class] forCellWithReuseIdentifier:@"NvCaptureFilterCell"];
    [self.bottomView addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bottomView.mas_top).offset(18 * SCREANSCALE);
        make.left.equalTo(moreB.mas_right).offset(40 * SCREANSCALE);
        make.right.equalTo(self.bottomView.mas_right);
        make.height.offset(75 * SCREANSCALE);
    }];
}

#pragma mark moreClick点击事件
- (void)moreClick:(UIButton *)sender{
    [self.delegate NvCaptureFilterView:self moreClick:sender];
}

#pragma mark 滤镜强度调节
- (void)sliderValueChanged:(UISlider *)slider{
    [self.delegate NvCaptureFilterView:self sliderValueChanged:slider];
}

#pragma mark 配置数据源,并且刷新视图
- (void)configDataSource:(NSMutableArray *)array{
    self.dataArray = array;
    [self reloadDataSource];
}

#pragma mark 更新数据源，不刷新视图
- (void)updateDataSource:(NSMutableArray *)array{
    self.dataArray = array;
    [self reloadDataSource];
}

#pragma mark 刷新视图
- (void)reloadDataSource{
    [self.collectionView reloadData];
}


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

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NvCaptureFilterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NvCaptureFilterCell" forIndexPath:indexPath];
    [cell renderCellWithModel:self.dataArray[indexPath.item]];
    return cell;
}

#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    for (NvCaptureFilterModel *model in self.dataArray) {
        model.selected = NO;
    }
    
    NvCaptureFilterModel *model = self.dataArray[indexPath.item];
    model.selected = YES;
    
    if (indexPath.item == 0) {
        self.topView.hidden = YES;
    }else{
        self.topView.hidden = NO;
        self.strengthSlider.value = 1;
    }
    
    [self.delegate NvCaptureFilterView:self withModel:model];
    [collectionView reloadData];
}


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

@end
