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

#import "NvFontListView.h"
#import "NVHeader.h"
#import "NvFontCollectionViewCell.h"

@interface NvFontListView ()<UICollectionViewDelegate, UICollectionViewDataSource>

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

@end

@implementation NvFontListView

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

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor nv_colorWithHexRGB:@"#242728"];
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        flowLayout.itemSize = CGSizeMake(77*SCREANSCALE, 49*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:[NvFontCollectionViewCell class] forCellWithReuseIdentifier:@"NvFontCollectionViewCell"];

        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(8*SCREANSCALE));
            make.top.equalTo(@(8*SCREANSCALEHEIGHT));
            make.right.equalTo(@(-8*SCREANSCALE));
            make.height.equalTo(@(49*SCREANSCALE));
        }];
        
        self.boldButton = [UIButton nv_buttonWithTitle:@"加粗" textColor:[UIColor nv_colorWithHexARGB:@"#CCFFFFFF"] fontSize:12 image:nil];
        self.boldButton.titleLabel.font = [NvUtils regularFontWithSize:12];
        self.italicButton = [UIButton nv_buttonWithTitle:@"斜体" textColor:[UIColor nv_colorWithHexARGB:@"#CCFFFFFF"] fontSize:12 image:nil];
        self.italicButton.titleLabel.font = [NvUtils regularFontWithSize:12];
        self.shadowButton = [UIButton nv_buttonWithTitle:@"阴影" textColor:[UIColor nv_colorWithHexARGB:@"#CCFFFFFF"] fontSize:12 image:nil];
        self.shadowButton.titleLabel.font = [NvUtils regularFontWithSize:12];
        [self.boldButton setBackgroundImage:[NvUtils imageWithName:@"NvCaptionBackGroundButton"] forState:UIControlStateNormal];
        [self.italicButton setBackgroundImage:[NvUtils imageWithName:@"NvCaptionBackGroundButton"] forState:UIControlStateNormal];
        [self.shadowButton setBackgroundImage:[NvUtils imageWithName:@"NvCaptionBackGroundButton"] forState:UIControlStateNormal];
        [self.boldButton setTitleColor:[UIColor nv_colorWithHexRGB:@"#4A90E2"] forState:UIControlStateSelected];
        [self.italicButton setTitleColor:[UIColor nv_colorWithHexRGB:@"#4A90E2"] forState:UIControlStateSelected];
        [self.shadowButton setTitleColor:[UIColor nv_colorWithHexRGB:@"#4A90E2"] forState:UIControlStateSelected];
        self.boldButton.layer.borderWidth = 1;
        self.boldButton.layer.borderColor = [UIColor colorWithWhite:1 alpha:0.8].CGColor;
        self.boldButton.layer.cornerRadius = 20.0/2*SCREANSCALE;
        self.boldButton.layer.masksToBounds = YES;
        self.italicButton.layer.borderWidth = 1;
        self.italicButton.layer.borderColor = [UIColor colorWithWhite:1 alpha:0.8].CGColor;
        self.italicButton.layer.cornerRadius = 20.0/2*SCREANSCALE;
        self.italicButton.layer.masksToBounds = YES;
        self.shadowButton.layer.borderWidth = 1;
        self.shadowButton.layer.borderColor = [UIColor colorWithWhite:1 alpha:0.8].CGColor;
        self.shadowButton.layer.cornerRadius = 20.0/2*SCREANSCALE;
        self.shadowButton.layer.masksToBounds = YES;
        [self addSubview:self.boldButton];
        [self addSubview:self.italicButton];
        [self addSubview:self.shadowButton];
        [self.boldButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(13*SCREANSCALE));
            make.top.equalTo(self.collectionView.mas_bottom).offset(17*SCREANSCALEHEIGHT);
            make.height.equalTo(@(20*SCREANSCALE));
        }];
        [self.italicButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.boldButton.mas_right).offset(18*SCREANSCALE);
            make.top.equalTo(self.boldButton);
            make.height.equalTo(@(20*SCREANSCALE));
        }];
        [self.shadowButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.italicButton.mas_right).offset(18*SCREANSCALE);
            make.top.equalTo(self.boldButton);
            make.height.equalTo(@(20*SCREANSCALE));
        }];
        __weak typeof(self)weakSelf = self;
        [self.boldButton nv_BtnClickHandler:^{
            weakSelf.boldButton.selected = !weakSelf.boldButton.selected;
            
            if (weakSelf.boldButton.selected) {
                weakSelf.boldButton.layer.borderColor = [UIColor nv_colorWithHexRGB:@"#4A90E2"].CGColor;
            } else {
                weakSelf.boldButton.layer.borderColor = [UIColor colorWithWhite:1 alpha:0.8].CGColor;
            }

            if ([weakSelf.delegate respondsToSelector:@selector(nvFontListView:blodClick:isApplyToAllCaption:)]) {
                [weakSelf.delegate nvFontListView:weakSelf blodClick:weakSelf.boldButton isApplyToAllCaption:NO];
            }
        }];
        [self.italicButton nv_BtnClickHandler:^{
            weakSelf.italicButton.selected = !weakSelf.italicButton.selected;
            if (weakSelf.italicButton.selected) {
                weakSelf.italicButton.layer.borderColor = [UIColor nv_colorWithHexRGB:@"#4A90E2"].CGColor;
            } else {
                weakSelf.italicButton.layer.borderColor = [UIColor colorWithWhite:1 alpha:0.8].CGColor;
            }

            if ([weakSelf.delegate respondsToSelector:@selector(nvFontListView:italicClick:isApplyToAllCaption:)]) {
                [weakSelf.delegate nvFontListView:weakSelf italicClick:weakSelf.italicButton isApplyToAllCaption:NO];
            }
        }];
        [self.shadowButton nv_BtnClickHandler:^{
            weakSelf.shadowButton.selected = !weakSelf.shadowButton.selected;
            if (weakSelf.shadowButton.selected) {
                weakSelf.shadowButton.layer.borderColor = [UIColor nv_colorWithHexRGB:@"#4A90E2"].CGColor;
            } else {
                weakSelf.shadowButton.layer.borderColor = [UIColor colorWithWhite:1 alpha:0.8].CGColor;
            }
            if ([weakSelf.delegate respondsToSelector:@selector(nvFontListView:shadowClick:isApplyToAllCaption:)]) {
                [weakSelf.delegate nvFontListView:weakSelf shadowClick:weakSelf.shadowButton isApplyToAllCaption:NO];
            }
        }];
        
        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);
        }];

        self.dataSource = [NSMutableArray new];
        NvCaptionFontItem *item = [NvCaptionFontItem new];
        item.isSelect = YES;
        item.imageUrl = @"NvsFilterNone";
        item.fontPath = @"";
        [self.dataSource addObject:item];
        NvCaptionFontItem *item1 = [NvCaptionFontItem new];
        item1.isSelect = NO;
        item1.imageUrl = @"Nvfont";
        item1.fontPath = [[NSBundle mainBundle] pathForResource:@"font" ofType:@"ttf"];
        [self.dataSource addObject:item1];
        [self.collectionView reloadData];
    }
    return self;
}

- (void)renderListWithItems:(NSMutableArray <NvCaptionFontItem *>*)dataSource {
    self.dataSource = dataSource;
    [self.collectionView reloadData];
}

- (void)setDefauleDataSource:(NSMutableArray *)dataSource {
    self.dataSource = dataSource;
    [self.collectionView reloadData];
    self.currentItem = nil;
    for (int i = 0; i < dataSource.count; i++) {
        NvCaptionFontItem *item = self.dataSource[i];
        if (item.isSelect) {
            self.currentItem = item;
        }
    }
    self.applyButton.selected = NO;
    self.styleApplyLabel.textColor = [UIColor nv_colorWithHexRGB:@"#FFFFFF"];
}

- (void)setDefaultFontBoldButton:(BOOL)isBold italic:(BOOL)isItalic shadow:(BOOL)isShadow {
    self.boldButton.selected = isBold;
    self.italicButton.selected = isItalic;
    self.shadowButton.selected = isShadow;
    if (self.boldButton.selected) {
        self.boldButton.layer.borderColor = [UIColor nv_colorWithHexRGB:@"#4A90E2"].CGColor;
    } else {
        self.boldButton.layer.borderColor = [UIColor whiteColor].CGColor;
    }
    if (self.italicButton.selected) {
        self.italicButton.layer.borderColor = [UIColor nv_colorWithHexRGB:@"#4A90E2"].CGColor;
    } else {
        self.italicButton.layer.borderColor = [UIColor whiteColor].CGColor;
    }
    if (self.shadowButton.selected) {
        self.shadowButton.layer.borderColor = [UIColor nv_colorWithHexRGB:@"#4A90E2"].CGColor;
    } else {
        self.shadowButton.layer.borderColor = [UIColor whiteColor].CGColor;
    }
}

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

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

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    for (NvCaptionFontItem *item in self.dataSource) {
        item.isSelect = NO;
    }
    NvCaptionFontItem *item = self.dataSource[indexPath.item];
    item.isSelect = YES;
    NvFontCollectionViewCell *cell = (NvFontCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
    [cell renderCellWithItem:self.dataSource[indexPath.item]];
    self.currentItem = item;
    if ([self.delegate respondsToSelector:@selector(selectFont:isApplyToAllCaption:)]) {
        [self.delegate selectFont:self.currentItem isApplyToAllCaption:NO];
    }
    [self.collectionView reloadData];
}

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