//
//  NvVideoSlider.m
//  NvCheez
//
//  Created by 刘东旭 on 2017/12/5.
//  Copyright © 2017年 刘东旭. All rights reserved.
//

#import "NvVideoSlider.h"
#import "NvsMultiThumbnailSequenceView.h"
#import <math.h>

#define NV_TIME_BASE 1000000

#define NV_TIME_MIN 3000000

#define SliderWidth 15

typedef enum {
    UNKOWN,
    LEFTSLIDER,
    RIGHTSLIDER,
}SliderDir;

@interface NvVideoSliderInfo()



@end

@implementation NvVideoSliderInfo
@end


@interface NvVideoSlider () <NvVideoSliderViewDelegate, UIScrollViewDelegate>{
    UIView *topLine;
    UIView *bottomLine;
    UIView *leftLayerView;
    UIView *rightLayerView;
}
@property (nonatomic, strong) UIImageView *leftSliderView;
@property (nonatomic, strong) UIImageView *rightSliderView;
@property (nonatomic,assign) int padding;
@property (nonatomic, assign) SliderDir sliderDir;
@end


@implementation NvVideoSlider {
    NvsMultiThumbnailSequenceView* sequenceView;
    UIView* timeAxis;
    NSMutableArray* timeSpanItemArray;
    double pointsPerMicrosecond;
    int64_t minDraggedTimeSpanDuration;
    NvVideoSliderView* selectedTimeSpanItem;
    bool changingTimelinePosFromScrollingClipReel;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setBackgroundColor:[UIColor blackColor]];
        CGFloat width = frame.size.width;
        pointsPerMicrosecond = width/35/NV_TIME_BASE;
        sequenceView = [[NvsMultiThumbnailSequenceView alloc] initWithFrame:frame];
        sequenceView.delegate = self;
        sequenceView.thumbnailImageFillMode = NvsThumbnailFillModeAspectCrop;
        sequenceView.thumbnailAspectRatio = 0.5;
        sequenceView.pointsPerMicrosecond = pointsPerMicrosecond;
        sequenceView.startPadding = 35;
        sequenceView.endPadding = sequenceView.startPadding;
        [sequenceView setBackgroundColor:[UIColor clearColor]];
        [self addSubview:sequenceView];
        timeAxis = [[UIView alloc] initWithFrame:CGRectMake(35, -3, 2, frame.size.height+6)];
        [timeAxis setBackgroundColor:[UIColor clearColor]];
        [self addSubview:timeAxis];
        
        //默认设置距离左边15
        self.padding = 15;
//        UIColor *color = [UIColor colorWithRed:248.0/255 green:213.0/255 blue:72.0/255 alpha:1];
        //添加左滑块
        self.leftSliderView = [[UIImageView alloc] initWithFrame:CGRectMake(self.padding, -2, SliderWidth, frame.size.height+4)];
        self.leftSliderView.image = [UIImage imageNamed:@"NvTailoringLeft_on"];
        self.leftSliderView.userInteractionEnabled = YES;
        [self addSubview:self.leftSliderView];
        //添加右滑块
        self.rightSliderView = [[UIImageView alloc] initWithFrame:CGRectMake(frame.size.width-SliderWidth-self.padding-110, -2, SliderWidth, frame.size.height+4)];
        self.rightSliderView.image = [UIImage imageNamed:@"NvTailoringRight_on"];
        self.rightSliderView.userInteractionEnabled = YES;
        [self addSubview:self.rightSliderView];
        //上边黄色条
        topLine = [[UIView alloc] initWithFrame:CGRectMake(self.leftSliderView.frame.origin.x+self.leftSliderView.frame.size.width, 0, self.rightSliderView.frame.origin.x-(self.leftSliderView.frame.origin.x+self.leftSliderView.frame.size.width), 2)];
        topLine.backgroundColor = [UIColor clearColor];
        [self addSubview:topLine];
        [self insertSubview:topLine belowSubview:self.leftSliderView];
        //下边黄色条
        bottomLine = [[UIView alloc] initWithFrame:CGRectMake(self.leftSliderView.frame.origin.x+self.leftSliderView.frame.size.width, self.frame.size.height-2, self.rightSliderView.frame.origin.x-(self.leftSliderView.frame.origin.x+self.leftSliderView.frame.size.width), 2)];
        bottomLine.backgroundColor = [UIColor clearColor];
        [self addSubview:bottomLine];
        [self insertSubview:bottomLine belowSubview:self.leftSliderView];
        [self bringSubviewToFront:timeAxis];
        
        //添加蒙层
        leftLayerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, self.leftSliderView.frame.size.height)];
        leftLayerView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
        [sequenceView addSubview:leftLayerView];
        rightLayerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, self.leftSliderView.frame.size.height)];
        rightLayerView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.7];
        [sequenceView addSubview:rightLayerView];
        
    }
    return self;
}

//timeline时间转偏移量
- (float)offsetXFromTimeline:(int64_t)time {
    return [sequenceView mapXFromTimelinePos:time];
}

//将偏移量转timeline上的时间
- (int64_t)timelineFromOffset:(float)offsetX {
    return [sequenceView mapTimelinePosFromX:offsetX];
}

- (void)setSequencePointsPerMicrosecond:(int)duration {
    CGFloat width = self.frame.size.width;
    sequenceView.pointsPerMicrosecond = width/duration/NV_TIME_BASE;
}

- (void)initTimelineEditor:(NSArray*) timelineEditorInfos timelineDuration:(int64_t)timelineDuration {
    if (timelineEditorInfos == nil)
        return;
    NSMutableArray* descArray = [NSMutableArray array];
    for (int i=0; i<timelineEditorInfos.count; i++) {
        NvVideoSliderInfo* info = (NvVideoSliderInfo*)timelineEditorInfos[i];
        NvsThumbnailSequenceDesc* desc = [[NvsThumbnailSequenceDesc alloc] init];
        desc.mediaFilePath = info.mediaFilePath;
        desc.inPoint = info.inPoint;
        desc.outPoint = info.outPoint;
        desc.trimIn = info.trimIn;
        desc.trimOut = info.trimOut;
        desc.stillImageHint = info.stillImageHint;
        [descArray addObject:desc];
    }
    sequenceView.descArray = descArray;
    
    self.timelineDuration = timelineDuration;
    //判断时间是否超过最大时长秒数
    if (self.timelineDuration >= self.maximumDuration) {
        float x = [self offsetXFromTimeline:self.maximumDuration];
        self.rightSliderView.frame = CGRectMake(x, -2, SliderWidth, self.frame.size.height+4);
        sequenceView.endPadding = self.frame.size.width-self.rightSliderView.frame.origin.x;
        topLine.frame = CGRectMake(self.leftSliderView.frame.origin.x+SliderWidth/2, 0, self.rightSliderView.frame.origin.x-self.leftSliderView.frame.origin.x, 2);
        bottomLine.frame = CGRectMake(self.leftSliderView.frame.origin.x+SliderWidth/2, self.leftSliderView.frame.size.height-2, self.rightSliderView.frame.origin.x-self.leftSliderView.frame.origin.x, 2);
    } else {
        float x = [self offsetXFromTimeline:self.timelineDuration];
        self.rightSliderView.frame = CGRectMake(x, -2, SliderWidth, self.frame.size.height+4);
        topLine.frame = CGRectMake(self.leftSliderView.frame.origin.x+SliderWidth/2, 0, self.rightSliderView.frame.origin.x-self.leftSliderView.frame.origin.x, 2);
        bottomLine.frame = CGRectMake(self.leftSliderView.frame.origin.x+SliderWidth/2, self.leftSliderView.frame.size.height-2, self.rightSliderView.frame.origin.x-self.leftSliderView.frame.origin.x, 2);
    }
    
    rightLayerView.frame = CGRectMake(self.rightSliderView.frame.origin.x+SliderWidth, 0, self.frame.size.width-(self.rightSliderView.frame.origin.x+SliderWidth), self.leftSliderView.frame.size.height);
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    //获取手指位置判断在哪个滑块上
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    //按住了左滑块
    if (CGRectContainsPoint(self.leftSliderView.frame, touchPoint)) {
        _sliderDir = LEFTSLIDER;
        int64_t time = [self timelineFromOffset:self.leftSliderView.frame.origin.x+self.leftSliderView.frame.size.width];
        int64_t timeout = [self timelineFromOffset:self.rightSliderView.frame.origin.x];
        if ([self.delegate respondsToSelector:@selector(timelineEditor:dragHandleStarted:trimOut:)]) {
            [self.delegate timelineEditor:self.leftSliderView dragHandleStarted:time trimOut:timeout];
        }
        return;
    }
    //按住了右滑块
    if (CGRectContainsPoint(self.rightSliderView.frame, touchPoint)) {
        _sliderDir = RIGHTSLIDER;
        int64_t timeOut = [self timelineFromOffset:self.rightSliderView.frame.origin.x];
        int64_t timetrimin = [self timelineFromOffset:self.leftSliderView.frame.origin.x+self.leftSliderView.frame.size.width];
        if ([self.delegate respondsToSelector:@selector(timelineEditor:dragHandleStarted:trimOut:)]) {
            [self.delegate timelineEditor:self.leftSliderView dragHandleStarted:timetrimin trimOut:timeOut];
        }
        return;
    }
    _sliderDir = UNKOWN;
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self];
    //判断手指是否划出去
    if (touchPoint.x<=SliderWidth/2+self.padding) {
        touchPoint.x = SliderWidth/2+self.padding;
    }
    if (touchPoint.x>=self.frame.size.width-SliderWidth/2-self.padding) {
        touchPoint.x = self.frame.size.width-SliderWidth/2-self.padding;
    }
    touchPoint.y = self.frame.size.height/2;
    
    //左右滑块放到对应手指位置
    if (_sliderDir == LEFTSLIDER) {
        float maxOffsetx = [self offsetXFromTimeline:self.maximumDuration]+sequenceView.contentOffset.x-sequenceView.startPadding;
        float minOffsetx = [self offsetXFromTimeline:NV_TIME_MIN]+sequenceView.contentOffset.x-sequenceView.startPadding;
        if ((self.rightSliderView.center.x - touchPoint.x - SliderWidth) >= maxOffsetx) {
            return;
        }
        if ((self.rightSliderView.center.x-SliderWidth - touchPoint.x) <= minOffsetx) {
            return;
        }
        
        self.leftSliderView.center = touchPoint;
        CGPoint pL = [self convertPoint:CGPointMake(self.leftSliderView.frame.origin.x, 0) toView:sequenceView];
        leftLayerView.frame = CGRectMake(0, 0, pL.x, self.leftSliderView.frame.size.height);
        timeAxis.frame = CGRectMake(self.leftSliderView.center.x+self.leftSliderView.frame.size.width/2, -3, 2, self.leftSliderView.frame.size.height+6);
        int64_t time = [self timelineFromOffset:self.leftSliderView.frame.origin.x+self.leftSliderView.frame.size.width];
        int64_t timeout = [self timelineFromOffset:self.rightSliderView.frame.origin.x];
        
        if ([self.delegate respondsToSelector:@selector(timelineEditor:draggingHandle:trimOut:)]) {
            [self.delegate timelineEditor:self.leftSliderView draggingHandle:time trimOut:timeout];
        }
    } else if (self.sliderDir == RIGHTSLIDER) {
        //大于最大值
        float maxOffsetx = [self offsetXFromTimeline:self.maximumDuration]+sequenceView.contentOffset.x-sequenceView.startPadding;
        float minOffsetx = [self offsetXFromTimeline:NV_TIME_MIN]+sequenceView.contentOffset.x-sequenceView.startPadding;
        if ((touchPoint.x-SliderWidth - self.leftSliderView.center.x) >= maxOffsetx) {
            return;
        }
        //小于1秒
        if ((touchPoint.x-SliderWidth - self.leftSliderView.center.x) <= minOffsetx) {
            return;
        }
        //超出总时间
        if ([self timelineFromOffset:touchPoint.x-SliderWidth/2]>=self.timelineDuration) {
            self.rightSliderView.center = CGPointMake([self offsetXFromTimeline:self.timelineDuration]+SliderWidth/2, self.rightSliderView.center.y);
        } else {
            self.rightSliderView.center = touchPoint;
        }
        sequenceView.endPadding = self.frame.size.width-self.rightSliderView.frame.origin.x;
        CGPoint pR = [self convertPoint:CGPointMake(self.rightSliderView.frame.origin.x+SliderWidth, 0) toView:sequenceView];
        rightLayerView.frame = CGRectMake(pR.x, 0, self.frame.size.width-(self.rightSliderView.frame.origin.x+SliderWidth), self.leftSliderView.frame.size.height);
        timeAxis.frame = CGRectMake(self.leftSliderView.center.x+self.leftSliderView.frame.size.width/2, -3, 2, self.leftSliderView.frame.size.height+6);
        
        int64_t timeOut = [self timelineFromOffset:self.rightSliderView.frame.origin.x];
        int64_t timeIn = [self timelineFromOffset:self.leftSliderView.frame.origin.x+self.leftSliderView.frame.size.width];

        if ([self.delegate respondsToSelector:@selector(timelineEditor:draggingHandle:trimOut:)]) {
            [self.delegate timelineEditor:self.rightSliderView draggingHandle:timeIn trimOut:timeOut];
        }
    } else {
        return;
    }
    //设置上下两条线的frame
    topLine.frame = CGRectMake(self.leftSliderView.frame.origin.x+SliderWidth/2, 0, self.rightSliderView.frame.origin.x-self.leftSliderView.frame.origin.x, 2);
    bottomLine.frame = CGRectMake(self.leftSliderView.frame.origin.x+SliderWidth/2, self.leftSliderView.frame.size.height-2, self.rightSliderView.frame.origin.x-self.leftSliderView.frame.origin.x, 2);
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    
    if (_sliderDir == LEFTSLIDER) {
        int64_t timeIn = [self timelineFromOffset:self.leftSliderView.frame.origin.x+self.leftSliderView.frame.size.width];
        int64_t timeOut = [self timelineFromOffset:self.rightSliderView.frame.origin.x];
        if ([self.delegate respondsToSelector:@selector(timelineEditor:dragHandleEnded:trimOut:)]) {
            [self.delegate timelineEditor:self.leftSliderView dragHandleEnded:timeIn trimOut:timeOut];
        }
    } else if (_sliderDir == RIGHTSLIDER) {
        int64_t timeIn = [self timelineFromOffset:self.leftSliderView.frame.origin.x+self.leftSliderView.frame.size.width];
        int64_t timeOut = [self timelineFromOffset:self.rightSliderView.frame.origin.x];
        if ([self.delegate respondsToSelector:@selector(timelineEditor:dragHandleEnded:trimOut:)]) {
            [self.delegate timelineEditor:self.rightSliderView dragHandleEnded:timeIn trimOut:timeOut];
        }
    }
    
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    if (_sliderDir == LEFTSLIDER) {
        int64_t timeIn = [self timelineFromOffset:self.leftSliderView.frame.origin.x+self.leftSliderView.frame.size.width];
        int64_t timeOut = [self timelineFromOffset:self.rightSliderView.frame.origin.x];
        if ([self.delegate respondsToSelector:@selector(timelineEditor:dragHandleEnded:trimOut:)]) {
            [self.delegate timelineEditor:self.leftSliderView dragHandleEnded:timeIn trimOut:timeOut];
        }
    } else if (_sliderDir == RIGHTSLIDER) {
        int64_t timeIn = [self timelineFromOffset:self.leftSliderView.frame.origin.x+self.leftSliderView.frame.size.width];
        int64_t timeOut = [self timelineFromOffset:self.rightSliderView.frame.origin.x];
        if ([self.delegate respondsToSelector:@selector(timelineEditor:dragHandleEnded:trimOut:)]) {
            [self.delegate timelineEditor:self.rightSliderView dragHandleEnded:timeIn trimOut:timeOut];
        }
    }
}

#pragma mark - scroll delegate

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    [self setTimespanMiddleHandlePosition:[self timelineFromOffset:self.leftSliderView.frame.origin.x+self.leftSliderView.frame.size.width]];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    int64_t time = [self timelineFromOffset:self.leftSliderView.frame.origin.x+self.leftSliderView.frame.size.width];
    [self setTimespanMiddleHandlePosition:time];
    
    CGPoint pL = [self convertPoint:CGPointMake(self.leftSliderView.frame.origin.x, 0) toView:sequenceView];
    CGPoint pR = [self convertPoint:CGPointMake(self.rightSliderView.frame.origin.x+SliderWidth, 0) toView:sequenceView];
    leftLayerView.frame = CGRectMake(0, 0, pL.x, self.leftSliderView.frame.size.height);
    rightLayerView.frame = CGRectMake(pR.x, 0, self.frame.size.width-(self.rightSliderView.frame.origin.x+SliderWidth), self.leftSliderView.frame.size.height);
    
    BOOL isResponds = [self.delegate respondsToSelector:@selector(timelineEditor: dragScrollingTimeline:)];
    if (isResponds) {
        [self.delegate timelineEditor:self dragScrollingTimeline:time];
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (decelerate)
        return;
    int64_t time = [self timelineFromOffset:self.leftSliderView.frame.origin.x+self.leftSliderView.frame.size.width];
    [self setTimespanMiddleHandlePosition:time];
    
    BOOL isResponds = [self.delegate respondsToSelector:@selector(timelineEditor: dragScrollTimelineEnded:)];
    if (isResponds)
        [self.delegate timelineEditor:self dragScrollTimelineEnded:time];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    int64_t time = [self timelineFromOffset:self.leftSliderView.frame.origin.x+self.leftSliderView.frame.size.width];
    [self setTimespanMiddleHandlePosition:time];
    
    BOOL isResponds = [self.delegate respondsToSelector:@selector(timelineEditor: dragScrollTimelineEnded:)];
    if (isResponds)
        [self.delegate timelineEditor:self dragScrollTimelineEnded:time];
}


- (void)layoutSubviews {
    [super layoutSubviews];
    sequenceView.frame = self.bounds;

}
//设置中间白线的位置
- (void)setTimespanMiddleHandlePosition:(int64_t)position {

    float x = [self offsetXFromTimeline:position];
    timeAxis.frame = CGRectMake(x, -3, 2, self.leftSliderView.frame.size.height+6);
    
}
@end
