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

#import "NvStyleListView.h"
#import "NvStyleCollectionViewCell.h"

@interface NvStyleListView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIButton *moreButton;
@property (nonatomic, strong) UILabel *moreLabel;
@property (nonatomic, strong) UILabel *styleApplyLabel;
@property (nonatomic, strong) NvButton *okButton;

@end

@implementation NvStyleListView

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

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor nv_colorWithHexRGB:@"#242728"];
        self.moreButton = [UIButton nv_buttonWithTitle:@"" textColor:nil fontSize:-1 image:[NvUtils imageWithName:@"NvsFilterMore"]];
        [self addSubview:self.moreButton];
        __weak typeof(self)weakSelf = self;
        [self.moreButton nv_BtnClickHandler:^{
            if ([weakSelf.delegate respondsToSelector:@selector(moreStyleClick)]) {
                [weakSelf.delegate moreStyleClick];
            }
        }];
        self.moreLabel = [UILabel nv_labelWithText:@"更多" fontSize:12 textColor:[UIColor nv_colorWithHexARGB:@"#CCFFFFFF"]];
        [self addSubview:self.moreLabel];
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.itemSize = CGSizeMake(77*SCREANSCALE, 78*SCREANSCALE);
        flowLayout.minimumInteritemSpacing = 8*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:[NvStyleCollectionViewCell class] forCellWithReuseIdentifier:@"NvStyleCollectionViewCell"];
        [self.collectionView setShowsHorizontalScrollIndicator:NO];
        [self.moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(8*SCREANSCALE));
            make.top.equalTo(@(10*SCREANSCALEHEIGHT));
            make.width.equalTo(@(57*SCREANSCALE));
            make.height.equalTo(@(49*SCREANSCALE));
        }];
        [self.moreLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.moreButton.mas_bottom).offset(8*SCREANSCALE);
            make.centerX.equalTo(self.moreButton);
        }];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.moreButton.mas_right).offset(8*SCREANSCALE);
            make.top.equalTo(@(8*SCREANSCALEHEIGHT));
            make.right.equalTo(@(-8*SCREANSCALE));
            make.bottom.equalTo(self.moreLabel.mas_bottom);
        }];
        
        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;
        [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);
        }];
        
    }
    return self;
}

// 刷新列表用于外界设置默认数据
- (void)renderListWithItems:(NSMutableArray <NvCaptionStyleItem *>*)dataSource {
    self.dataSource = dataSource;
    [self.collectionView reloadData];
    self.applyButton.selected = NO;
    self.currentItem = nil;
    for (int i = 0; i < dataSource.count; i++) {
        NvCaptionStyleItem *item = self.dataSource[i];
        if (item.isSelect) {
            self.currentItem = item;
        }
    }
    
    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 {
    NvStyleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NvStyleCollectionViewCell" forIndexPath:indexPath];
    [cell renderCellWithItem:self.dataSource[indexPath.item]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    for (NvCaptionStyleItem *item in self.dataSource) {
        item.isSelect = NO;
    }
    
    NvCaptionStyleItem *item = self.dataSource[indexPath.item];
    item.isSelect = YES;

    self.currentItem = item;
    if ([self.delegate respondsToSelector:@selector(selectStyle:isApplyToAllCaption:)]) {
        [self.delegate selectStyle:self.currentItem isApplyToAllCaption:NO];
    }
    dispatch_async(dispatch_get_main_queue(), ^{
        if ([self.delegate respondsToSelector:@selector(stylePlay)]) {
            [self.delegate stylePlay];
        }
    });
    [self.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
