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

#import "NvColorListview.h"
#import "NvColorCollectionViewCell.h"

@interface NvColorListview()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong)UICollectionView *collectionView;
@property (nonatomic, strong) UILabel *alphaLabel;
@property (nonatomic, strong) UILabel *alphaNumLabel;

@property (nonatomic, strong) UILabel *styleApplyLabel;
@property (nonatomic, strong) NvButton *okButton;

@end

@implementation NvColorListview

- (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:[NvColorCollectionViewCell class] forCellWithReuseIdentifier:@"NvColorCollectionViewCell"];
        [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.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.collectionView.mas_bottom).offset(20*SCREANSCALEHEIGHT);
        }];
        
        self.alphaNumLabel = [UILabel nv_labelWithText:@"100" fontSize:12 textColor:[UIColor whiteColor]];
        self.alphaNumLabel.alpha = 0.8;
        self.alphaNumLabel.hidden = YES;
        self.alphaNumLabel.font = [NvUtils regularFontWithSize:12];
        [self addSubview:self.alphaNumLabel];
        [self.alphaNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(@(-17*SCREANSCALE));
            make.centerY.equalTo(self.alphaLabel);
            make.width.equalTo(@(25*SCREANSCALE));
        }];
        
        self.slider = [[UISlider alloc] init];
        self.slider.minimumTrackTintColor = [UIColor nv_colorWithHexARGB:@"#66FFFFFF"];
        self.slider.maximumTrackTintColor = [UIColor nv_colorWithHexARGB:@"#66FFFFFF"];
        [self.slider setThumbImage:[NvUtils imageWithName:@"NvSliderIcon"] forState:UIControlStateNormal];
        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(self).offset(-17*SCREANSCALE);
            make.centerY.equalTo(self.alphaLabel);
        }];
        
        [self.slider addTarget:self action:@selector(sliderValueChanged:) 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 nv_colorWithHexARGB:@"#CCFFFFFF"]];
        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];
        NvCaptionColorItem *whiteItem = [NvCaptionColorItem new];
        whiteItem.isSelect = NO;
        whiteItem.color = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        [self.dataSource addObject:whiteItem];
        NvCaptionColorItem *blackItem = [NvCaptionColorItem new];
        blackItem.isSelect = NO;
        blackItem.color = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
        [self.dataSource addObject:blackItem];
        NvCaptionColorItem *redItem = [NvCaptionColorItem new];
        redItem.isSelect = NO;
        redItem.color = [UIColor colorWithRed:1 green:0 blue:0 alpha:1];
        [self.dataSource addObject:redItem];
        NvCaptionColorItem *yellowItem = [NvCaptionColorItem new];
        yellowItem.isSelect = NO;
        yellowItem.color = [UIColor yellowColor];
        [self.dataSource addObject:yellowItem];
        NvCaptionColorItem *greenItem = [NvCaptionColorItem new];
        greenItem.isSelect = NO;
        greenItem.color = [UIColor greenColor];
        [self.dataSource addObject:greenItem];
        NvCaptionColorItem *blueItem = [NvCaptionColorItem new];
        blueItem.isSelect = NO;
        blueItem.color = [UIColor blueColor];
        [self.dataSource addObject:blueItem];
        NvCaptionColorItem *purpleItem = [NvCaptionColorItem new];
        purpleItem.isSelect = NO;
        purpleItem.color = [UIColor purpleColor];
        [self.dataSource addObject:purpleItem];
        [self.collectionView reloadData];
        self.currentItem = whiteItem;
    }
    return self;
}


// 刷新列表用于外界设置默认数据
- (void)setDefaultDataSource:(NSMutableArray *)dataSource alpha:(float)value {
    self.dataSource = dataSource;
    self.currentItem = nil;
    for (NvCaptionColorItem *item in dataSource) {
        if (item.isSelect) {
            self.currentItem = item;
        }
    }
    [self.collectionView reloadData];
    self.slider.value = value;
    self.alphaNumLabel.text = [NSString stringWithFormat:@"%ld",(NSInteger)(self.slider.value*100)];
    self.applyButton.selected = NO;
    self.styleApplyLabel.textColor = [UIColor nv_colorWithHexRGB:@"#FFFFFF"];
}

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

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

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    for (NvCaptionColorItem *item in self.dataSource) {
        item.isSelect = NO;
    }
    NvCaptionColorItem *item = self.dataSource[indexPath.item];
    item.isSelect = YES;
    self.currentItem = item;
    [self.collectionView reloadData];
    if ([self.delegate respondsToSelector:@selector(selectColor:isApplyToAllCaption:)] && self.currentItem) {
        [self.delegate selectColor:self.currentItem isApplyToAllCaption:NO];
    }
    if ([self.delegate respondsToSelector:@selector(alphaChanged:isApplyToAllCaption:)]) {
        [self.delegate alphaChanged:self.slider.value isApplyToAllCaption:NO];
    }
}

- (void)sliderValueChanged:(UISlider *)slider {
    self.alphaNumLabel.text = [NSString stringWithFormat:@"%ld",(NSInteger)(slider.value*100)];
    if ([self.delegate respondsToSelector:@selector(alphaChanged:isApplyToAllCaption:)]) {
        [self.delegate alphaChanged:slider.value isApplyToAllCaption:NO];
    }
}

- (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
