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

#import "NvStyleView.h"
#import "NVHeader.h"
#import "NvPositionListView.h"
#import "NvCaptionStyleItem.h"
#import "NvsTimelineCaption.h"
#import "NvMoreDownloadViewController.h"
#import "NvTimelineCaptionUtils.h"
#import "NvTimelineUtils.h"

@interface NvStyleView()

@property (nonatomic, strong) UIButton *styleButton;
@property (nonatomic, strong) UIButton *colorButton;
@property (nonatomic, strong) UIButton *strokeButton;
@property (nonatomic, strong) UIButton *fontButton;
@property (nonatomic, strong) UIButton *positionButton;
@property (nonatomic, strong) UIView *styleLine;
@property (nonatomic, strong) UIView *colorLine;
@property (nonatomic, strong) UIView *strokeLine;
@property (nonatomic, strong) UIView *fontLine;
@property (nonatomic, strong) UIView *sizeLine;
@property (nonatomic, strong) UIView *positionLine;
@property (nonatomic, strong) UIButton *okButton;
@property (nonatomic, strong) UIButton *applyButton;
@property (nonatomic, strong) UILabel *styleApplyLabel;

@property (nonatomic, strong) NvCaptionInfoModel *currentModel;//字幕修改的数据

@property (nonatomic, strong) UIView *line;

@end

@implementation NvStyleView

- (void)dealloc {
    NSLog(@"%s",__func__);
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor nv_colorWithHexRGB:@"#242728"];
        [self initSubViews];
    }
    return self;
}

// MARK: NvStyleListViewDelegate
// 选择样式
- (void)selectStyle:(NvCaptionStyleItem *)item isApplyToAllCaption:(BOOL)isApplyToAllCaption {
    if (isApplyToAllCaption) {
        NSMutableArray *captions = self.models;
        NvsTimelineCaption *nextCaption = [self.timeline getFirstCaption];
        int i = 0;
        do {
            if (nextCaption.category == NvsThemeCategory && nextCaption.roleInTheme != NvsRoleInThemeGeneral) {
                
            }else{
                NvCaptionInfoModel *info = captions[i];
                //            float size = [nextCaption getFontSize];
                //            CGPoint transform = [nextCaption getCaptionTranslation];
                [nextCaption applyCaptionStyle:item.packageId];
                //            [nextCaption setFontSize:size];
                //            [nextCaption setCaptionTranslation:transform];
                info.styleId = item.packageId;
                info.isBold = [nextCaption getBold];
                info.isItalic = [nextCaption getItalic];
                info.fontSize = [nextCaption getFontSize];
                info.translation = [nextCaption getCaptionTranslation];
                info.rotation = [nextCaption getRotationZ];
                info.alignment = [nextCaption getTextAlignment];
                info.color = [UIColor colorWithRed:[nextCaption getTextColor].r green:[nextCaption getTextColor].g blue:[nextCaption getTextColor].b alpha:[nextCaption getTextColor].a];
                i++;
            }
            nextCaption = [self.timeline getNextCaption:nextCaption];
        } while (nextCaption);
    } else {
        NvCaptionInfoModel *info = self.model;
        info.styleId = item.packageId;
//        float size = [self.caption getFontSize];
        NvsTimelineCaption *nextCaption = self.caption;
//        NSLog(@"---%@", NSStringFromCGPoint([nextCaption getCaptionTranslation]));
        [nextCaption applyCaptionStyle:item.packageId];
//        NSLog(@"===%@", NSStringFromCGPoint([nextCaption getCaptionTranslation]));
        info.isBold = [nextCaption getBold];
        info.isItalic = [nextCaption getItalic];
        info.fontSize = [nextCaption getFontSize];
        info.rotation = [nextCaption getRotationZ];
        info.alignment = [nextCaption getTextAlignment];
        info.color = [UIColor colorWithRed:[nextCaption getTextColor].r green:[nextCaption getTextColor].g blue:[nextCaption getTextColor].b alpha:[nextCaption getTextColor].a];
        info.translation = [nextCaption getCaptionTranslation];
    }

    [self.delegate nvseekTimeline];
    [self.delegate applyStyleUpdateDefaultData:self.caption];
    
}
// 更多样式点击
- (void)moreStyleClick {
    if ([self.delegate respondsToSelector:@selector(moreStyleClick)]) {
        [self.delegate moreStyleClick];
    }
}
// 更多字体点击
- (void)moreFontClick {
    if ([self.delegate respondsToSelector:@selector(moreFontClick)]) {
        [self.delegate moreFontClick];
    }
}

// MARK: NvColorListViewDelegate
// 选择颜色
- (void)selectColor:(NvCaptionColorItem *)item isApplyToAllCaption:(BOOL)isApplyToAllCaption {
    if (isApplyToAllCaption) {
        NSMutableArray *captions = self.models;
        for (NvCaptionInfoModel *info in captions) {
            info.color = item.color;
        }
        
        NvsTimelineCaption *nextCaption = [self.timeline getFirstCaption];
        do {
            if (nextCaption.category == NvsThemeCategory && nextCaption.roleInTheme != NvsRoleInThemeGeneral) {
                
            }else{
                NvsColor color = [nextCaption getTextColor];
                const CGFloat* colors = CGColorGetComponents(item.color.CGColor);
                if (colors!=NULL) {
                    color.r = colors[0];
                    color.g = colors[1];
                    color.b = colors[2];
                    color.a = colors[3];
                    [nextCaption setTextColor:&color];
                }
            }
            nextCaption = [self.timeline getNextCaption:nextCaption];
        } while (nextCaption);

    } else {
        NvCaptionInfoModel *info = self.model;
        info.color = item.color;
        
        NvsTimelineCaption *lastCaption = self.caption;
        NvsColor color = [lastCaption getTextColor];
        NSMutableArray *colors = [self changeUIColorToRGB:item.color];
        if (colors.count == 4) {
            color.r = [colors[0] integerValue]/255.0;
            color.g = [colors[1] integerValue]/255.0;
            color.b = [colors[2] integerValue]/255.0;
            color.a = [colors[3] integerValue]/255.0;
            [lastCaption setTextColor:&color];
        }
    }
    
    [self.delegate nvseekTimeline];
}

- (NSMutableArray *) changeUIColorToRGB:(UIColor *)color
{
    NSMutableArray *RGBStrValueArr = [[NSMutableArray alloc] init];
    NSString *RGBStr = nil;
    //获得RGB值描述
    NSString *RGBValue = [NSString stringWithFormat:@"%@",color];
    //将RGB值描述分隔成字符串
    NSArray *RGBArr = [RGBValue componentsSeparatedByString:@" "];
    //获取红色值
    int r = [[RGBArr objectAtIndex:1] intValue] * 255;
    RGBStr = [NSString stringWithFormat:@"%d",r];
    [RGBStrValueArr addObject:RGBStr];
    //获取绿色值
    int g = [[RGBArr objectAtIndex:2] intValue] * 255;
    RGBStr = [NSString stringWithFormat:@"%d",g];
    [RGBStrValueArr addObject:RGBStr];
    //获取蓝色值
    int b = [[RGBArr objectAtIndex:3] intValue] * 255;
    RGBStr = [NSString stringWithFormat:@"%d",b];
    [RGBStrValueArr addObject:RGBStr];
    
    int a = [[RGBArr objectAtIndex:4] intValue] * 255;
    RGBStr = [NSString stringWithFormat:@"%d",a];
    [RGBStrValueArr addObject:RGBStr];
    //返回保存RGB值的数组
    return RGBStrValueArr;
}

// 滑动透明度
- (void)alphaChanged:(float)value isApplyToAllCaption:(BOOL)isApplyToAllCaption {
    if (isApplyToAllCaption) {
        NSMutableArray *captions = self.models;
        for (NvCaptionInfoModel *info in captions) {
            CGFloat r,g,b,a;
            [info.color getRed:&r green:&g blue:&b alpha:&a];
            info.color = [UIColor colorWithRed:r green:g blue:b alpha:value];
        }
        
        NvsTimelineCaption *nextCaption = [self.timeline getFirstCaption];
        do {
            if (nextCaption.category == NvsThemeCategory && nextCaption.roleInTheme != NvsRoleInThemeGeneral) {
                
            }else{
                NvsColor color = [nextCaption getTextColor];
                color.a = value;
                [nextCaption setTextColor:&color];
            }
            nextCaption = [self.timeline getNextCaption:nextCaption];
        } while (nextCaption);

    } else {
        NvCaptionInfoModel *info = self.model;
        CGFloat r,g,b,a;
        [info.color getRed:&r green:&g blue:&b alpha:&a];
        info.color = [UIColor colorWithRed:r green:g blue:b alpha:value];
        
        NvsTimelineCaption *lastCaption = self.caption;
        NvsColor color = [lastCaption getTextColor];
        color.a = value;
        [lastCaption setTextColor:&color];
    }
//    [NvTimelineUtils resetCaption:self.timeline captionDataArray:[[NvTimelineData sharedInstance] captionDataArray]];
    
    [self.delegate nvseekTimeline];
}

// MARK: NvStrokeListViewDelegate
// 选择描边
- (void)selectStroke:(NvCaptionStrokeItem *)item isApplyToAllCaption:(BOOL)isApplyToAllCaption {
    if (isApplyToAllCaption) {
        NSMutableArray *captions = self.models;
        for (NvCaptionInfoModel *info in captions) {
            if (item.isNone) {
                info.isDrawOutline = NO;
            } else {
                info.isDrawOutline = YES;
                info.outlineColor = [UIColor colorWithRed:item.color.r green:item.color.g blue:item.color.b alpha:item.color.a];
                info.outlineWidth = 10;
            }
        }
        
        NvsTimelineCaption *nextCaption = [self.timeline getFirstCaption];
        do {
            if (nextCaption.category == NvsThemeCategory && nextCaption.roleInTheme != NvsRoleInThemeGeneral) {
                
            }else{
                if (item.isNone) {
                    [nextCaption setDrawOutline:NO];
                } else {
                    NvsColor color = [nextCaption getOutlineColor];
                    
                    color.r = item.color.r;
                    color.g = item.color.g;
                    color.b = item.color.b;
                    [nextCaption setDrawOutline:YES];
                    [nextCaption setOutlineColor:&color];
                    [nextCaption setOutlineWidth:10];
                    
                }
            }
            nextCaption = [self.timeline getNextCaption:nextCaption];
        } while (nextCaption);
        
    } else {
        NvCaptionInfoModel *info = self.model;
        if (item.isNone) {
            info.isDrawOutline = NO;
        } else {
            info.isDrawOutline = YES;
            info.outlineColor = [UIColor colorWithRed:item.color.r green:item.color.g blue:item.color.b alpha:item.color.a];
            info.outlineWidth = 10;
        }
        
        NvsTimelineCaption *lastCaption = self.caption;
        if (item.isNone) {
            [lastCaption setDrawOutline:NO];
        } else {
            NvsColor color = [lastCaption getOutlineColor];

            color.r = item.color.r;
            color.g = item.color.g;
            color.b = item.color.b;
            color.a = 1;
            [lastCaption setDrawOutline:YES];
            [lastCaption setOutlineColor:&color];
            [lastCaption setOutlineWidth:10];
            
        }
        
    }
//    [NvTimelineUtils resetCaption:self.timeline captionDataArray:[[NvTimelineData sharedInstance] captionDataArray]];
    
    [self.delegate nvseekTimeline];
}
// 选择描边的宽度
- (void)selectStroke:(NvCaptionStrokeItem *)item withWidth:(CGFloat)width isApplyToAllCaption:(BOOL)isApplyToAllCaption {
    if (isApplyToAllCaption) {
        NSMutableArray *captions = self.models;
        for (NvCaptionInfoModel *info in captions) {
            info.outlineWidth = width*10;
        }
        
        NvsTimelineCaption *nextCaption = [self.timeline getFirstCaption];
        do {
            if (nextCaption.category == NvsThemeCategory && nextCaption.roleInTheme != NvsRoleInThemeGeneral) {
                
            }else{
                [nextCaption setOutlineWidth:width*10];
            }
            nextCaption = [self.timeline getNextCaption:nextCaption];
        } while (nextCaption);
        
    } else {
        NvCaptionInfoModel *info = self.model;
        info.outlineWidth = width*10;

        NvsTimelineCaption *lastCaption = self.caption;
        [lastCaption setOutlineWidth:width*10];
    }

    [self.delegate nvseekTimeline];
}
// 选择描边的透明度
- (void)selectStroke:(NvCaptionStrokeItem *)item withAlpha:(CGFloat)alpha isApplyToAllCaption:(BOOL)isApplyToAllCaption {
    if (isApplyToAllCaption) {
        NSMutableArray *captions = self.models;
        for (NvCaptionInfoModel *info in captions) {
            UIColor *color = info.outlineColor;
            CGFloat r,g,b,a;
            [color getRed:&r green:&g blue:&b alpha:&a];
            info.outlineColor = [UIColor colorWithRed:r green:g blue:b alpha:alpha];
        }
        
        NvsTimelineCaption *nextCaption = [self.timeline getFirstCaption];
        do {
            if (nextCaption.category == NvsThemeCategory && nextCaption.roleInTheme != NvsRoleInThemeGeneral) {
                
            }else{
                NvsColor color = [nextCaption getOutlineColor];
                
                color.a = alpha;
                [nextCaption setOutlineColor:&color];
            }
            nextCaption = [self.timeline getNextCaption:nextCaption];
        } while (nextCaption);

    } else {
        NvCaptionInfoModel *info = self.model;
        UIColor *color = info.outlineColor;
        CGFloat r,g,b,a;
        [color getRed:&r green:&g blue:&b alpha:&a];
        info.outlineColor = [UIColor colorWithRed:r green:g blue:b alpha:alpha];
        
        NvsTimelineCaption *lastCaption = self.caption;
        NvsColor color1 = [lastCaption getOutlineColor];

        color1.a = alpha;
        [lastCaption setOutlineColor:&color1];
        

    }
//    [NvTimelineUtils resetCaption:self.timeline captionDataArray:[[NvTimelineData sharedInstance] captionDataArray]];
    
    [self.delegate nvseekTimeline];
}

// MARK: NvFontListViewDelegate
// 选择字体粗体
- (void)nvFontListView:(NvFontListView *)nvFontListView blodClick:(UIButton *)sender isApplyToAllCaption:(BOOL)isApplyToAllCaption {
    if (isApplyToAllCaption) {
        NSMutableArray *captions = self.models;
        for (NvCaptionInfoModel *info in captions) {
            if (sender.selected) {
                info.isBold = YES;
            } else {
                info.isBold = NO;
            }
        }
        
        NvsTimelineCaption *nextCaption = [self.timeline getFirstCaption];
        do {
            if (nextCaption.category == NvsThemeCategory && nextCaption.roleInTheme != NvsRoleInThemeGeneral) {
                
            }else{
               [nextCaption setBold:sender.selected];
            }
            nextCaption = [self.timeline getNextCaption:nextCaption];
        } while (nextCaption);
        
    } else {
        NvCaptionInfoModel *info = self.model;
        if (sender.selected) {
            info.isBold = YES;
        } else {
            info.isBold = NO;
        }
        
        NvsTimelineCaption *lastCaption = self.caption;
        [lastCaption setBold:sender.selected];
    }
//    [NvTimelineUtils resetCaption:self.timeline captionDataArray:[[NvTimelineData sharedInstance] captionDataArray]];
    
    [self.delegate nvseekTimeline];
}
// 选择字体斜体
- (void)nvFontListView:(NvFontListView *)nvFontListView italicClick:(UIButton *)sender isApplyToAllCaption:(BOOL)isApplyToAllCaption {
    if (isApplyToAllCaption) {
        NSMutableArray *captions = self.models;
        for (NvCaptionInfoModel *info in captions) {
            if (sender.selected) {
                info.isItalic = YES;
            } else {
                info.isItalic = NO;
            }
        }
        
        NvsTimelineCaption *nextCaption = [self.timeline getFirstCaption];
        do {
            if (nextCaption.category == NvsThemeCategory && nextCaption.roleInTheme != NvsRoleInThemeGeneral) {
                
            }else{
                [nextCaption setItalic:sender.selected];
            }
            
            nextCaption = [self.timeline getNextCaption:nextCaption];
        } while (nextCaption);
        
    } else {
        NvCaptionInfoModel *info = self.model;
        if (sender.selected) {
            info.isItalic = YES;
        } else {
            info.isItalic = NO;
        }
        
        NvsTimelineCaption *lastCaption = self.caption;
        [lastCaption setItalic:sender.selected];
    }
//    [NvTimelineUtils resetCaption:self.timeline captionDataArray:[[NvTimelineData sharedInstance] captionDataArray]];
    
    [self.delegate nvseekTimeline];
}
// 选择字体阴影
- (void)nvFontListView:(NvFontListView *)nvFontListView shadowClick:(UIButton *)sender isApplyToAllCaption:(BOOL)isApplyToAllCaption {
    if (isApplyToAllCaption) {
        NSMutableArray *captions = self.models;
        for (NvCaptionInfoModel *info in captions) {
            if (sender.selected) {
                info.isDrawShadow = YES;
            } else {
                info.isDrawShadow = NO;
            }
        }
        
        NvsTimelineCaption *nextCaption = [self.timeline getFirstCaption];
        do {
            if (nextCaption.category == NvsThemeCategory && nextCaption.roleInTheme != NvsRoleInThemeGeneral) {
                
            }else{
                [nextCaption setDrawShadow:sender.selected];
                [nextCaption setDrawShadow:sender.selected];
                NvsColor color = {0,0,0,0.5};
                [nextCaption setShadowColor:&color];
                [nextCaption setShadowOffset:CGPointMake(10, -10)];
            }
            
            nextCaption = [self.timeline getNextCaption:nextCaption];
        } while (nextCaption);
        
    } else {
        NvCaptionInfoModel *info = self.model;
        if (sender.selected) {
            info.isDrawShadow = YES;
        } else {
            info.isDrawShadow = NO;
        }
        //设置阴影的默认数据
        NvsTimelineCaption *lastCaption = self.caption;
        [lastCaption setDrawShadow:sender.selected];
        NvsColor color = {0,0,0,0.5};
        [lastCaption setShadowColor:&color];
        [lastCaption setShadowOffset:CGPointMake(10, -10)];
    }
//    [NvTimelineUtils resetCaption:self.timeline captionDataArray:[[NvTimelineData sharedInstance] captionDataArray]];
    
    [self.delegate nvseekTimeline];
}
// 选择字体
- (void)selectFont:(NvCaptionFontItem *)item isApplyToAllCaption:(BOOL)isApplyToAllCaption {
    if (isApplyToAllCaption) {
        NSMutableArray *captions = self.models;
        for (NvCaptionInfoModel *info in captions) {
            info.fontFilePath = item.fontPath;
        }
        
        NvsTimelineCaption *nextCaption = [self.timeline getFirstCaption];
        do {
            if (nextCaption.category == NvsThemeCategory && nextCaption.roleInTheme != NvsRoleInThemeGeneral) {
                
            }else{
                [nextCaption setFontWithFilePath:item.fontPath];
            }
            
            nextCaption = [self.timeline getNextCaption:nextCaption];
        } while (nextCaption);

    } else {
        NvCaptionInfoModel *info = self.model;
        info.fontFilePath = item.fontPath;
        
        NvsTimelineCaption *lastCaption = self.caption;
        [lastCaption setFontWithFilePath:item.fontPath];
    }
//    [NvTimelineUtils resetCaption:self.timeline captionDataArray:[[NvTimelineData sharedInstance] captionDataArray]];
    
    [self.delegate nvseekTimeline];
}
// MARK: NvSizeListViewDelegate
// 选择大小
//- (void)nvSizeListView:(NvSizeListView *)nvSizeListView size:(CGFloat)fontSize isApplyToAllCaption:(BOOL)isApplyToAllCaption {
//    if (isApplyToAllCaption) {
//        NSMutableArray *captions = self.models;
//        for (NvCaptionInfoModel *info in captions) {
//            info.fontSize = fontSize;
//        }
//
//        NvsTimelineCaption *nextCaption = [self.timeline getFirstCaption];
//        do {
//            [nextCaption setFontSize:fontSize];
//            nextCaption = [self.timeline getNextCaption:nextCaption];
//        } while (nextCaption);
//
//    } else {
//        NvCaptionInfoModel *info = self.model;
//        info.fontSize = fontSize;
//
//        [self.caption setFontSize:fontSize];
//        [self.delegate updateCaptionView:self.caption];
//    }
////    [NvTimelineUtils resetCaption:self.timeline captionDataArray:[[NvTimelineData sharedInstance] captionDataArray]];
//
//    [self.delegate nvseekTimeline];
//}

// MARK: NvPositionListViewDelegate
// 选择位置靠左
- (void)nvPositionListView:(NvPositionListView *)nvPositionListView leftClick:(UIButton *)sender isApplyToAllCaption:(BOOL)isApplyToAllCaption {
    if (isApplyToAllCaption) {

        NvsTimelineCaption *nextCaption = [self.timeline getFirstCaption];
        int i =0;
        do {
            if (nextCaption.category == NvsThemeCategory && nextCaption.roleInTheme != NvsRoleInThemeGeneral) {
                
            }else{
                NvCaptionInfoModel *info = self.models[i];
                NSArray *list = [nextCaption getBoundingRectangleVertices];
                CGPoint minx = [self getLeftMinPointFrom:list];
                
                float xOffset = -(self.timeline.videoRes.imageWidth/2 + minx.x);
                [nextCaption translateCaption:CGPointMake(xOffset, 0)];
                info.translation = [nextCaption getCaptionTranslation];
                i++;
            }
            
            nextCaption = [self.timeline getNextCaption:nextCaption];
        } while (nextCaption);
        
    } else {
        NvCaptionInfoModel *info = self.model;
        NSArray *list = [self.caption getBoundingRectangleVertices];
        CGPoint minx = [self getLeftMinPointFrom:list];
        
        float xOffset = -(self.timeline.videoRes.imageWidth/2 + minx.x);
        [self.caption translateCaption:CGPointMake(xOffset, 0)];
        info.translation = [self.caption getCaptionTranslation];

    }

    [self.delegate nvseekTimeline];
}

// 选择位置靠右
- (void)nvPositionListView:(NvPositionListView *)nvPositionListView rightClick:(UIButton *)sender isApplyToAllCaption:(BOOL)isApplyToAllCaption {
    if (isApplyToAllCaption) {

        NvsTimelineCaption *nextCaption = [self.timeline getFirstCaption];
        int i=0;
        do {
            if (nextCaption.category == NvsThemeCategory && nextCaption.roleInTheme != NvsRoleInThemeGeneral) {
                
            }else{
                NSArray *list = [nextCaption getBoundingRectangleVertices];
                CGPoint maxP = [self getRightMaxPointFrom:list];
                float xOffset = self.timeline.videoRes.imageWidth/2 - maxP.x;;
                [nextCaption translateCaption:CGPointMake(xOffset, 0)];
                
                NvCaptionInfoModel *info = self.models[i];
                info.translation = [nextCaption getCaptionTranslation];
                i++;
            }
            nextCaption = [self.timeline getNextCaption:nextCaption];
        } while (nextCaption);
        
    } else {
        NvCaptionInfoModel *info = self.model;

        NSArray *list = [self.caption getBoundingRectangleVertices];
        CGPoint maxP = [self getRightMaxPointFrom:list];
        float xOffset = self.timeline.videoRes.imageWidth/2 - maxP.x;;
        [self.caption translateCaption:CGPointMake(xOffset, 0)];
        info.translation = [self.caption getCaptionTranslation];
        
    }
    
    [self.delegate nvseekTimeline];
}
// 选择位置靠上
- (void)nvPositionListView:(NvPositionListView *)nvPositionListView upClick:(UIButton *)sender isApplyToAllCaption:(BOOL)isApplyToAllCaption {
    if (isApplyToAllCaption) {

        NvsTimelineCaption *nextCaption = [self.timeline getFirstCaption];
        int i = 0;
        do {
            if (nextCaption.category == NvsThemeCategory && nextCaption.roleInTheme != NvsRoleInThemeGeneral) {
                
            }else{
                NSArray *list = [nextCaption getBoundingRectangleVertices];
                CGPoint miny = [self getTopMinPointFrom:list];
                CGPoint maxy = [self getBottomMaxPointFrom:list];
                float y_dis = maxy.y - miny.y;
                float yOffset = self.timeline.videoRes.imageHeight/2 - miny.y - y_dis;
                [nextCaption translateCaption:CGPointMake(0, yOffset)];
                
                NvCaptionInfoModel *info = self.models[i];
                info.translation = [nextCaption getCaptionTranslation];
                i++;
            }
            nextCaption = [self.timeline getNextCaption:nextCaption];
        } while (nextCaption);
        
    } else {
        NvCaptionInfoModel *info = self.model;
        
        NSArray *list = [self.caption getBoundingRectangleVertices];
        CGPoint miny = [self getTopMinPointFrom:list];
        CGPoint maxy = [self getBottomMaxPointFrom:list];
        
        float y_dis = maxy.y - miny.y;
        
        float yOffset = self.timeline.videoRes.imageHeight/2 - miny.y - y_dis;
        [self.caption translateCaption:CGPointMake(0, yOffset)];
        info.translation = [self.caption getCaptionTranslation];

    }
    
    [self.delegate nvseekTimeline];
}
// 选择位置靠下
- (void)nvPositionListView:(NvPositionListView *)nvPositionListView downClick:(UIButton *)sender isApplyToAllCaption:(BOOL)isApplyToAllCaption {
    if (isApplyToAllCaption) {

        NvsTimelineCaption *nextCaption = [self.timeline getFirstCaption];
        int i = 0;
        do {
            if (nextCaption.category == NvsThemeCategory && nextCaption.roleInTheme != NvsRoleInThemeGeneral) {
                
            }else{
                NSArray *list = [nextCaption getBoundingRectangleVertices];
                CGPoint miny = [self getTopMinPointFrom:list];
                CGPoint maxy = [self getBottomMaxPointFrom:list];
                
                float y_dis = maxy.y - miny.y;
                
                float yOffset = -(self.timeline.videoRes.imageHeight/2 + maxy.y - y_dis);
                [nextCaption translateCaption:CGPointMake(0, yOffset)];
                
                NvCaptionInfoModel *info = self.models[i];
                info.translation = [nextCaption getCaptionTranslation];
                
                i++;
            }
            nextCaption = [self.timeline getNextCaption:nextCaption];
        } while (nextCaption);
    } else {
        NvCaptionInfoModel *info = self.model;

        NSArray *list = [self.caption getBoundingRectangleVertices];
        CGPoint miny = [self getTopMinPointFrom:list];
        CGPoint maxy = [self getBottomMaxPointFrom:list];
        
        float y_dis = maxy.y - miny.y;
        
        float yOffset = -(self.timeline.videoRes.imageHeight/2 + maxy.y - y_dis);
        [self.caption translateCaption:CGPointMake(0, yOffset)];
        info.translation = [self.caption getCaptionTranslation];
    }
//    [NvTimelineUtils resetCaption:self.timeline captionDataArray:[[NvTimelineData sharedInstance] captionDataArray]];
    
    [self.delegate nvseekTimeline];
}
// 选择位置垂直居中
- (void)nvPositionListView:(NvPositionListView *)nvPositionListView verticalClick:(UIButton *)sender isApplyToAllCaption:(BOOL)isApplyToAllCaption {
    if (isApplyToAllCaption) {
        NvsTimelineCaption *nextCaption = [self.timeline getFirstCaption];
        int i = 0;
        do {
            if (nextCaption.category == NvsThemeCategory && nextCaption.roleInTheme != NvsRoleInThemeGeneral) {
                
            }else{
                NSArray *list = [nextCaption getBoundingRectangleVertices];
                CGPoint miny = [self getTopMinPointFrom:list];
                CGPoint maxy = [self getBottomMaxPointFrom:list];
                
                float yOffset = -((maxy.y - miny.y)/2 + miny.y);
                [nextCaption translateCaption:CGPointMake(0, yOffset)];
                
                NvCaptionInfoModel *info = self.models[i];
                info.translation = [nextCaption getCaptionTranslation];
                i++;
            }
            
            nextCaption = [self.timeline getNextCaption:nextCaption];
        } while (nextCaption);

    } else {
        NvCaptionInfoModel *info = self.model;

        NSArray *list = [self.caption getBoundingRectangleVertices];
        CGPoint miny = [self getTopMinPointFrom:list];
        CGPoint maxy = [self getBottomMaxPointFrom:list];

        float yOffset = -((maxy.y - miny.y)/2 + miny.y);
        [self.caption translateCaption:CGPointMake(0, yOffset)];
        info.translation = [self.caption getCaptionTranslation];
        
    }
    
    [self.delegate nvseekTimeline];
}
// 选择位置水平居中
- (void)nvPositionListView:(NvPositionListView *)nvPositionListView horizontalClick:(UIButton *)sender isApplyToAllCaption:(BOOL)isApplyToAllCaption {
    if (isApplyToAllCaption) {
        
        NvsTimelineCaption *nextCaption = [self.timeline getFirstCaption];
        int i = 0;
        do {
            if (nextCaption.category == NvsThemeCategory && nextCaption.roleInTheme != NvsRoleInThemeGeneral) {
                
            }else{
                NSArray *list = [nextCaption getBoundingRectangleVertices];
                CGPoint minx = [self getLeftMinPointFrom:list];
                CGPoint maxx = [self getRightMaxPointFrom:list];
                
                float xOffset = -((maxx.x - minx.x)/2 + minx.x);
                [nextCaption translateCaption:CGPointMake(xOffset, 0)];
                
                NvCaptionInfoModel *info = self.models[i];
                info.translation = [nextCaption getCaptionTranslation];
                i++;
            }
            
            nextCaption = [self.timeline getNextCaption:nextCaption];
        } while (nextCaption);

    } else {
        NvCaptionInfoModel *info = self.model;

        NSArray *list = [self.caption getBoundingRectangleVertices];
        CGPoint minx = [self getLeftMinPointFrom:list];
        CGPoint maxx = [self getRightMaxPointFrom:list];
        
        float xOffset = -((maxx.x - minx.x)/2 + minx.x);
        [self.caption translateCaption:CGPointMake(xOffset, 0)];
        info.translation = [self.caption getCaptionTranslation];
    }
    
    [self.delegate nvseekTimeline];
}

- (CGPoint)getLeftMinPointFrom:(NSArray *)array {
    float leftx = [[array firstObject] CGPointValue].x;
    float lefty = [[array firstObject] CGPointValue].y;
    for (NSValue *v in array) {
        CGPoint p = [v CGPointValue];
        if (leftx > p.x) {
            leftx = p.x;
            lefty = p.y;
        }
    }
    return CGPointMake(leftx, lefty);
}

- (CGPoint)getRightMaxPointFrom:(NSArray *)array {
    float rightx = [[array firstObject] CGPointValue].x;
    float righty = [[array firstObject] CGPointValue].y;
    for (NSValue *v in array) {
        CGPoint p = [v CGPointValue];
        if (p.x > rightx) {
            rightx = p.x;
            righty = p.y;
        }
    }
    return CGPointMake(rightx, righty);
}

- (CGPoint)getTopMinPointFrom:(NSArray *)array {
    float leftx = [[array firstObject] CGPointValue].x;
    float lefty = [[array firstObject] CGPointValue].y;
    for (NSValue *v in array) {
        CGPoint p = [v CGPointValue];
        if (lefty > p.y) {
            lefty = p.y;
            leftx = p.x;
        }
    }
    return CGPointMake(leftx, lefty);
}

- (CGPoint)getBottomMaxPointFrom:(NSArray *)array {
    float rightx = [[array firstObject] CGPointValue].x;
    float righty = [[array firstObject] CGPointValue].y;
    for (NSValue *v in array) {
        CGPoint p = [v CGPointValue];
        if (p.y > righty) {
            righty = p.y;
            rightx = p.x;
        }
    }
    return CGPointMake(rightx, righty);
}

- (void)applyChangeAll {
    //样式
    if (self.styleListView.applyButton.selected) {
        [self selectStyle:self.styleListView.currentItem isApplyToAllCaption:YES];
    }
    //颜色
    if (self.colorListView.applyButton.selected) {
        [self selectColor:self.colorListView.currentItem isApplyToAllCaption:YES];
        [self alphaChanged:self.colorListView.slider.value isApplyToAllCaption:YES];
    }
    //描边
    if (self.strokeListView.applyButton.selected) {
        [self selectStroke:self.strokeListView.currentItem isApplyToAllCaption:YES];
        [self selectStroke:self.strokeListView.currentItem withAlpha:self.strokeListView.slider.value isApplyToAllCaption:YES];
        [self selectStroke:self.strokeListView.currentItem withWidth:self.strokeListView.widthSlider.value isApplyToAllCaption:YES];
    }
    //字体
    if (self.fontListView.applyButton.selected) {
        [self selectFont:self.fontListView.currentItem isApplyToAllCaption:YES];
        [self nvFontListView:self.fontListView blodClick:self.fontListView.boldButton isApplyToAllCaption:YES];
        [self nvFontListView:self.fontListView italicClick:self.fontListView.italicButton isApplyToAllCaption:YES];
        [self nvFontListView:self.fontListView shadowClick:self.fontListView.shadowButton isApplyToAllCaption:YES];
    }
    //位置
    if (self.positionListView.applyButton.selected) {
        switch (self.positionListView.type) {
            case None:
                break;
            case Left:
                [self nvPositionListView:self.positionListView leftClick:self.positionListView.leftButton isApplyToAllCaption:YES];
                break;
            case Right:
                [self nvPositionListView:self.positionListView rightClick:self.positionListView.rightButton isApplyToAllCaption:YES];
                break;
            case Up:
                [self nvPositionListView:self.positionListView upClick:self.positionListView.upButton isApplyToAllCaption:YES];
                break;
            case Down:
                [self nvPositionListView:self.positionListView downClick:self.positionListView.downButton isApplyToAllCaption:YES];
                break;
            case Vertical:
                [self nvPositionListView:self.positionListView verticalClick:self.positionListView.verticalButton isApplyToAllCaption:YES];
                break;
            case Horizontal:
                [self nvPositionListView:self.positionListView horizontalClick:self.positionListView.horizontalButton isApplyToAllCaption:YES];
                break;
            default:
                break;
        }
    }
    [self.delegate nvseekTimeline];
}

- (void)stylePlay {
    if ([self.delegate respondsToSelector:@selector(stylePlayFromTime:)]) {
        [self.delegate stylePlayFromTime:self.caption.inPoint];
    }
}

// MARK: 点击分类按钮事件
- (void)selectTitleButton:(UIButton *)button {
    NSArray *buttons = @[self.styleButton,self.colorButton,self.strokeButton,self.fontButton,self.positionButton];
    NSArray *lines = @[self.styleLine,self.colorLine,self.strokeLine,self.fontLine,self.positionLine];
    for (int i = 0; i < buttons.count; i++) {
        UIButton *currentButton = buttons[i];
        UIView *currentLine = lines[i];
        if (currentButton == button) {
            [currentButton setTitleColor:[UIColor nv_colorWithHexRGB:@"#4A90E2"] forState:UIControlStateNormal];
            currentLine.backgroundColor = [UIColor nv_colorWithHexRGB:@"#4A90E2"];
        } else {
            [currentButton setTitleColor:[UIColor nv_colorWithHexARGB:@"#80909293"] forState:UIControlStateNormal];
            currentLine.backgroundColor = [UIColor nv_colorWithHexARGB:@"#80909293"];
        }
    }
    if (button == self.styleButton) {
        [self showCurrentView:self.styleListView];
    } else if (button == self.colorButton) {
        [self showCurrentView:self.colorListView];
    } else if (button == self.strokeButton) {
        [self showCurrentView:self.strokeListView];
    } else if (button == self.fontButton) {
        [self showCurrentView:self.fontListView];
    } else if (button == self.positionButton) {
        [self showCurrentView:self.positionListView];
    }
}

- (void)showCurrentView:(UIView *)currentView {
    NSArray *viewArray = @[self.styleListView,self.colorListView,self.strokeListView,self.fontListView,self.positionListView];
    for (UIView *view in viewArray) {
        if (view == currentView) {
            view.hidden = NO;
        } else {
            view.hidden = YES;
        }
    }
}

- (void)setHidden:(BOOL)hidden {
    [super setHidden:hidden];
    if (hidden == NO) {
        [self selectTitleButton:self.styleButton];
    }
}

- (void)initSubViews {
    __weak typeof(self)weakSelf = self;
    self.styleButton = [UIButton nv_buttonWithTitle:@"样式" textColor:[UIColor nv_colorWithHexRGB:@"#4A90E2"] fontSize:12];
    [self.styleButton nv_BtnClickHandler:^{
        [weakSelf selectTitleButton:weakSelf.styleButton];
    }];
    self.colorButton = [UIButton nv_buttonWithTitle:@"颜色" textColor:[UIColor nv_colorWithHexARGB:@"#80909293"] fontSize:12];
    [self.colorButton nv_BtnClickHandler:^{
        [weakSelf selectTitleButton:weakSelf.colorButton];
    }];
    self.strokeButton = [UIButton nv_buttonWithTitle:@"描边" textColor:[UIColor nv_colorWithHexARGB:@"#80909293"] fontSize:12];
    [self.strokeButton nv_BtnClickHandler:^{
        [weakSelf selectTitleButton:weakSelf.strokeButton];
    }];
    self.fontButton = [UIButton nv_buttonWithTitle:@"字体" textColor:[UIColor nv_colorWithHexARGB:@"#80909293"] fontSize:12];
    [self.fontButton nv_BtnClickHandler:^{
        [weakSelf selectTitleButton:weakSelf.fontButton];
    }];
    self.positionButton = [UIButton nv_buttonWithTitle:@"位置" textColor:[UIColor nv_colorWithHexARGB:@"#80909293"] fontSize:12];
    [self.positionButton nv_BtnClickHandler:^{
        [weakSelf selectTitleButton:weakSelf.positionButton];
    }];
    self.styleLine = [UIView new];
    self.styleLine.backgroundColor = [UIColor nv_colorWithHexRGB:@"#4A90E2"];
    self.colorLine = [UIView new];
    self.colorLine.backgroundColor = [UIColor nv_colorWithHexRGB:@"#909293"];
    self.strokeLine = [UIView new];
    self.strokeLine.backgroundColor = [UIColor nv_colorWithHexRGB:@"#909293"];
    self.fontLine = [UIView new];
    self.fontLine.backgroundColor = [UIColor nv_colorWithHexRGB:@"#909293"];
    self.sizeLine = [UIView new];
    self.sizeLine.backgroundColor = [UIColor nv_colorWithHexRGB:@"#909293"];
    self.positionLine = [UIView new];
    self.positionLine.backgroundColor = [UIColor nv_colorWithHexRGB:@"#909293"];
    self.applyButton = [UIButton nv_buttonWithTitle:nil textColor:nil fontSize:-1 image:[NvUtils imageWithName:@"NvNoselectbutton"]];
    [self.applyButton setImage:[NvUtils imageWithName:@"Nvselectbutton"] forState:UIControlStateSelected];
    self.styleApplyLabel = [UILabel nv_labelWithText:@"将样式应用到所有字幕" fontSize:10 textColor:[UIColor nv_colorWithHexARGB:@"#CCFFFFFF"]];
    
    [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"];
        }
        if ([weakSelf.delegate respondsToSelector:@selector(applyStyleToAllCaption:)]) {
            [weakSelf.delegate applyStyleToAllCaption:weakSelf.applyButton.selected];
        }
    }];
    
    [self addSubview:self.styleButton];
    [self addSubview:self.colorButton];
    [self addSubview:self.strokeButton];
    [self addSubview:self.fontButton];
//    [self addSubview:self.sizeButton];
    [self addSubview:self.positionButton];
    [self addSubview:self.styleLine];
    [self addSubview:self.colorLine];
    [self addSubview:self.strokeLine];
    [self addSubview:self.fontLine];
    [self addSubview:self.sizeLine];
    [self addSubview:self.positionLine];
    self.okButton = [UIButton nv_buttonWithTitle:nil textColor:nil fontSize:-1 image:[NvUtils imageWithName:@"Nvcheck - material"]];
    [self addSubview:self.okButton];
    
    // 添加子Views
    self.styleListView = [NvStyleListView new];
    self.styleListView.delegate = self;
    self.colorListView = [NvColorListview new];
    self.colorListView.delegate = self;
    self.strokeListView = [NvStrokeListView new];
    self.strokeListView.delegate = self;
    self.fontListView = [NvFontListView new];
    self.fontListView.delegate = self;
    self.positionListView = [NvPositionListView new];
    self.positionListView.delegate = self;
    [self addSubview:self.styleListView];
    [self addSubview:self.colorListView];
    [self addSubview:self.strokeListView];
    [self addSubview:self.fontListView];
    [self addSubview:self.positionListView];
    
    [self.okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.width.equalTo(@(25*SCREANSCALEHEIGHT));
        make.height.equalTo(@(20*SCREANSCALEHEIGHT));
        if (@available(iOS 11.0, *)) {
            make.bottom.equalTo(self.mas_safeAreaLayoutGuideBottom).offset(-15*SCREANSCALEHEIGHT);
        } else {
            make.bottom.equalTo(@(-15*SCREANSCALEHEIGHT));
        }
    }];
    
    [self.okButton nv_BtnClickHandler:^{
        [weakSelf applyChangeAll];
        if ([weakSelf.delegate respondsToSelector:@selector(styleOkClick)]) {
            [weakSelf.delegate styleOkClick];
        }
    }];
    
    [self.styleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self).multipliedBy(1.0/5);
        make.height.equalTo(@(36*SCREANSCALE));
        make.top.left.equalTo(@0);
    }];
    [self.colorButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.styleButton);
        make.top.equalTo(@0);
        make.left.equalTo(self.styleButton.mas_right);
        make.height.equalTo(self.styleButton.mas_height);
    }];
    [self.strokeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.styleButton);
        make.top.equalTo(@0);
        make.left.equalTo(self.colorButton.mas_right);
        make.height.equalTo(self.styleButton.mas_height);
    }];
    [self.fontButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.styleButton);
        make.top.equalTo(@0);
        make.left.equalTo(self.strokeButton.mas_right);
        make.height.equalTo(self.styleButton.mas_height);
    }];

    [self.positionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.styleButton);
        make.top.equalTo(@0);
        make.left.equalTo(self.fontButton.mas_right);
        make.height.equalTo(self.styleButton.mas_height);
    }];
    [self.styleLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.styleButton);
        make.bottom.equalTo(self.styleButton.mas_bottom);
        make.height.equalTo(@1);
        make.left.equalTo(@0);
    }];
    [self.colorLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.styleButton);
        make.bottom.equalTo(self.styleButton.mas_bottom);
        make.height.equalTo(@1);
        make.left.equalTo(self.styleLine.mas_right);
    }];
    [self.strokeLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.styleButton);
        make.bottom.equalTo(self.styleButton.mas_bottom);
        make.height.equalTo(@1);
        make.left.equalTo(self.colorLine.mas_right);
    }];
    [self.fontLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.styleButton);
        make.bottom.equalTo(self.styleButton.mas_bottom);
        make.height.equalTo(@1);
        make.left.equalTo(self.strokeLine.mas_right);
    }];

    [self.positionLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.styleButton);
        make.bottom.equalTo(self.styleButton.mas_bottom);
        make.height.equalTo(@1);
        make.left.equalTo(self.fontLine.mas_right);
    }];
    [self.styleListView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(self.styleLine.mas_bottom);
        make.bottom.equalTo(self.okButton.mas_top);
    }];
    [self.colorListView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.styleListView);
    }];
    [self.strokeListView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.styleListView);
    }];
    [self.fontListView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.styleListView);
    }];
    [self.positionListView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.styleListView);
    }];
    [self bringSubviewToFront:self.styleListView];
    
    self.line = [UIView new];
    self.line.backgroundColor = [UIColor nv_colorWithHexARGB:@"#1AFFFFFF"];
    [self addSubview:self.line];
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.height.equalTo(@1);
        make.bottom.equalTo(self.okButton.mas_top).offset(-12*SCREANSCALE);
    }];
}

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

@end
