//
//  NvStrokeListView.m
//  SDKDemo
//
//  Created by Meicam on 2018/6/6.
//  Copyright © 2018年 刘东旭. All rights reserved.
//

#import "NvStrokeListView.h"
#import "NVHeader.h"
#import "NvStrokeCollectionViewCell.h"

@interface NvStrokeListView()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UILabel *alphaLabel;
@property (nonatomic, strong) UILabel *widthLabel;
@property (nonatomic, strong) UILabel *styleApplyLabel;
@property (nonatomic, strong) NvButton *okButton;

@end

@implementation NvStrokeListView

- (void)dealloc {
//    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor nv_colorWithHexRGB:@"#242728"];
        self.layer.masksToBounds = YES;
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.itemSize = CGSizeMake(25*SCREANSCALE, 25*SCREANSCALE);
        flowLayout.minimumLineSpacing = 29*SCREANSCALE;
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.backgroundColor = [UIColor nv_colorWithHexRGB:@"#242728"];
        [self addSubview:self.collectionView];
        [self.collectionView registerClass:[NvStrokeCollectionViewCell class] forCellWithReuseIdentifier:@"NvStrokeCollectionViewCell"];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(13*SCREANSCALE);
            make.top.equalTo(@(20*SCREANSCALEHEIGHT));
            make.right.equalTo(@(-13*SCREANSCALE));
            make.height.equalTo(@(25*SCREANSCALE));
        }];
        
        self.widthLabel = [UILabel nv_labelWithText:@"宽度" fontSize:12 textColor:[UIColor nv_colorWithHexARGB:@"#CCFFFFFF"]];
        self.widthLabel.alpha = 0.8;
        self.widthLabel.font = [NvUtils regularFontWithSize:12];
        [self addSubview:self.widthLabel];
        [self.widthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(13*SCREANSCALE));
            make.top.equalTo(self.collectionView.mas_bottom).offset(15*SCREANSCALEHEIGHT);
        }];
        
        self.alphaLabel = [UILabel nv_labelWithText:@"不透明度" fontSize:12 textColor:[UIColor nv_colorWithHexARGB:@"#CCFFFFFF"]];
        self.alphaLabel.alpha = 0.8;
        self.alphaLabel.font = [NvUtils regularFontWithSize:12];
        [self addSubview:self.alphaLabel];
        [self.alphaLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(13*SCREANSCALE));
            make.top.equalTo(self.widthLabel.mas_bottom).offset(12*SCREANSCALEHEIGHT);
        }];
        
        self.slider = [[UISlider alloc] init];
        [self.slider setThumbImage:[NvUtils imageWithName:@"NvSliderIcon"] forState:UIControlStateNormal];
        self.slider.minimumTrackTintColor = [UIColor nv_colorWithHexARGB:@"#66FFFFFF"];
        self.slider.maximumTrackTintColor = [UIColor nv_colorWithHexARGB:@"#66FFFFFF"];
        self.slider.value = 1;
        [self addSubview:self.slider];
        [self.slider mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.alphaLabel.mas_right).offset(19*SCREANSCALE);
            make.right.equalTo(@(-17*SCREANSCALE));
            make.centerY.equalTo(self.alphaLabel);
        }];
        [self.slider addTarget:self action:@selector(alphaSliderChanged:) forControlEvents:UIControlEventValueChanged];
        self.widthSlider = [[UISlider alloc] init];
        self.widthSlider.value = 1;
        [self.widthSlider setThumbImage:[NvUtils imageWithName:@"NvSliderIcon"] forState:UIControlStateNormal];
        [self addSubview:self.widthSlider];
        [self.widthSlider mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.alphaLabel.mas_right).offset(19*SCREANSCALE);
            make.right.equalTo(@(-17*SCREANSCALE));
            make.centerY.equalTo(self.widthLabel);
        }];
        [self.widthSlider addTarget:self action:@selector(widthSliderChanged:) forControlEvents:UIControlEventValueChanged];
        self.applyButton = [NvButton nv_buttonWithTitle:nil textColor:nil fontSize:-1 image:[NvUtils imageWithName:@"NvNoApplyAll"]];
        [self.applyButton setImage:[NvUtils imageWithName:@"NvApplyAll"] forState:UIControlStateSelected];
        self.styleApplyLabel = [UILabel nv_labelWithText:@"将样式应用到所有字幕" fontSize:10 textColor:[UIColor whiteColor]];
        self.styleApplyLabel.font = [NvUtils regularFontWithSize:10];
        self.styleApplyLabel.alpha = 0.8;
        __weak typeof(self)weakSelf = self;
        [self.applyButton nv_BtnClickHandler:^{
            weakSelf.applyButton.selected = !weakSelf.applyButton.selected;
            if (weakSelf.applyButton.selected) {
                weakSelf.styleApplyLabel.textColor = [UIColor nv_colorWithHexRGB:@"#4A90E2"];
            } else {
                weakSelf.styleApplyLabel.textColor = [UIColor nv_colorWithHexRGB:@"#FFFFFF"];
            }
        }];
        
        [self addSubview:self.applyButton];
        [self addSubview:self.styleApplyLabel];

        [self.applyButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(13*SCREANSCALE));
            make.bottom.equalTo(self).offset(-20*SCREANSCALE);
            make.width.height.equalTo(@(15*SCREANSCALE));
        }];
        [self.styleApplyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self.applyButton.mas_centerY);
            make.left.equalTo(self.applyButton.mas_right).offset(7*SCREANSCALE);
        }];

        self.dataSource = [NSMutableArray new];
        NvCaptionStrokeItem *whiteItem = [NvCaptionStrokeItem new];
        whiteItem.isSelect = YES;
        whiteItem.isNone = YES;
        NvColor white = {0,0,0,0};
        whiteItem.color = white;
        [self.dataSource addObject:whiteItem];
        NvCaptionStrokeItem *blackItem = [NvCaptionStrokeItem new];
        blackItem.isSelect = NO;
        NvColor blackcolor = {0,0,0,1};
        blackItem.color = blackcolor;
        [self.dataSource addObject:blackItem];
        NvCaptionStrokeItem *redItem = [NvCaptionStrokeItem new];
        redItem.isSelect = NO;
        NvColor redcolor = {1,0,0,1};
        redItem.color = redcolor;
        [self.dataSource addObject:redItem];
        NvCaptionStrokeItem *yellowItem = [NvCaptionStrokeItem new];
        yellowItem.isSelect = NO;
        NvColor yellowcolor = {0,1,0,1};
        yellowItem.color = yellowcolor;
        [self.dataSource addObject:yellowItem];
        NvCaptionStrokeItem *greenItem = [NvCaptionStrokeItem new];
        greenItem.isSelect = NO;
        NvColor greencolor = {1,1,0,1};
        greenItem.color = greencolor;
        [self.dataSource addObject:greenItem];
        NvCaptionStrokeItem *blueItem = [NvCaptionStrokeItem new];
        blueItem.isSelect = NO;
        NvColor bluecolor = {0,0,1,1};
        blueItem.color = bluecolor;
        [self.dataSource addObject:blueItem];
        NvCaptionStrokeItem *purpleItem = [NvCaptionStrokeItem new];
        purpleItem.isSelect = NO;
        NvColor purplecolor = {1,0,1,1};
        purpleItem.color = purplecolor;
        [self.dataSource addObject:purpleItem];
        [self.collectionView reloadData];
        [self.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionNone];
    }
    return self;
}

//设置默认数据
- (void)setDefaultDataSource:(NSMutableArray *)dataSource width:(float)width alpha:(float)value {
    self.dataSource = dataSource;
    [self.collectionView reloadData];
    self.currentItem = nil;
    for (int i = 0; i < dataSource.count; i++) {
        NvCaptionStrokeItem *item = self.dataSource[i];
        if (item.isSelect) {
            self.currentItem = item;
        }
    }
    self.slider.value = value;
    self.widthSlider.value = width;
    self.applyButton.selected = NO;
    self.styleApplyLabel.textColor = [UIColor nv_colorWithHexRGB:@"#FFFFFF"];
}
//透明度slider
- (void)alphaSliderChanged:(UISlider *)slider {
    if ([self.delegate respondsToSelector:@selector(selectStroke:withAlpha:isApplyToAllCaption:)]) {
        [self.delegate selectStroke:self.currentItem withAlpha:slider.value isApplyToAllCaption:NO];
    }
}
//宽度slider
- (void)widthSliderChanged:(UISlider *)slider {
    if ([self.delegate respondsToSelector:@selector(selectStroke:withWidth:isApplyToAllCaption:)]) {
        [self.delegate selectStroke:self.currentItem withWidth:slider.value isApplyToAllCaption:NO];
    }
}

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

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

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    for (NvCaptionStrokeItem *item in self.dataSource) {
        item.isSelect = NO;
    }
    NvCaptionStrokeItem *item = self.dataSource[indexPath.item];
    item.isSelect = YES;
    NvStrokeCollectionViewCell *cell = (NvStrokeCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [cell renderCellWithItem:self.dataSource[indexPath.item]];
    self.currentItem = item;
    if (indexPath.item == 0) {
        self.currentItem.isNone = YES;
    } else {
        self.currentItem.isNone = NO;
    }
    if ([self.delegate respondsToSelector:@selector(selectStroke:isApplyToAllCaption:)] && self.currentItem) {
        [self.delegate selectStroke:self.currentItem isApplyToAllCaption:NO];
    }
    if ([self.delegate respondsToSelector:@selector(selectStroke:withAlpha:isApplyToAllCaption:)]) {
        [self.delegate selectStroke:self.currentItem withAlpha:self.slider.value isApplyToAllCaption:NO];
    }
    if ([self.delegate respondsToSelector:@selector(selectStroke:withWidth:isApplyToAllCaption:)]) {
        [self.delegate selectStroke:self.currentItem withWidth:self.widthSlider.value isApplyToAllCaption:NO];
    }
    [self.collectionView reloadData];
    //    [collectionView reloadItemsAtIndexPaths:@[indexPath]];
}


- (CGSize)intrinsicContentSize {
    return CGSizeMake(SCREEN_WDITH, 87*SCREANSCALE);
}

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

@end
