//
//  NvTimelineCaptionUtils.m
//  SDKDemo
//
//  Created by shizhouhu on 2018/7/2.
 //
//

#import "NvTimelineCaptionUtils.h"
#import "NvUtils.h"
#import "NvTimelineUtils.h"
#import "NvsTimelineCaption.h"

@implementation NvTimelineCaptionUtils

+ (NSString *)addCaption:(NvsTimeline *)timeline captionInfo:(NvCaptionInfoModel *)captionInfo {
    [[[NvTimelineData sharedInstance] captionDataArray] addObject:captionInfo];
    [NvTimelineUtils resetCaption:timeline captionDataArray:[[NvTimelineData sharedInstance] captionDataArray]];
    return captionInfo.uuid;
}

+ (void)updateCaption:(NvsTimeline *)timeline captionInfo:(NvCaptionInfoModel *)captionInfo {
    NvsTimelineCaption *caption = [NvTimelineCaptionUtils findCaptionObject:timeline captionInfo:captionInfo];
    if (caption == nil)
        return;
    [caption setScaleX:captionInfo.scale];
    [caption setScaleY:captionInfo.scale];
    [caption setCaptionTranslation:captionInfo.translation];
    [caption setRotationZ:captionInfo.rotation];
    [caption changeInPoint:captionInfo.inPoint];
    [caption changeOutPoint:captionInfo.outPoint];
    
    NSMutableArray *captionlist = [[NvTimelineData sharedInstance] captionDataArray];
    if (captionlist.count == 0 || [NvUtils isStringEmpty:captionInfo.uuid]) {
        return;
    }
    for (NvCaptionInfoModel* info in captionlist) {
        if ([captionInfo.uuid isEqualToString:[(NvCaptionInfoModel *)info uuid]]) {
            info.scale = captionInfo.scale;
            info.translation = captionInfo.translation;
            info.rotation = captionInfo.rotation;
            info.inPoint = captionInfo.inPoint;
            info.outPoint = captionInfo.outPoint;
            break;
        }
    }
    [NvTimelineUtils resetCaption:timeline captionDataArray:[[NvTimelineData sharedInstance] captionDataArray]];
}

+ (NvCaptionInfoModel *)removeCaption:(NvsTimeline *)timeline captionInfo:(NvCaptionInfoModel *)captionInfo {
    NSMutableArray *captionlist = [[NvTimelineData sharedInstance] captionDataArray];
    NvCaptionInfoModel *nextCaption = nil;
    for (int i = 0; i < captionlist.count; i++) {
        NvCaptionInfoModel *info = captionlist[i];
        if ([info.uuid isEqualToString:captionInfo.uuid]) {
            [captionlist removeObject:info];
            if (i < captionlist.count) {
                nextCaption = captionlist[i];
            } else if (captionlist.count > 0) {
                nextCaption = captionlist[captionlist.count-1];
            }
            break;
        }
    }
    [NvTimelineUtils resetCaption:timeline captionDataArray:[[NvTimelineData sharedInstance] captionDataArray]];
    return nextCaption;
}

+ (NvCaptionInfoModel *)getCaptionByPoint:(NvsTimeline *)timeline liveWindow:(NvsLiveWindow *)liveWindow point:(CGPoint)point{
    NvsStreamingContext *context = [NvsStreamingContext sharedInstance];
    NSArray *captionList = [timeline getCaptionsByTimelinePosition:[context getTimelineCurrentPosition:timeline]];
    if (captionList.count == 0) {
        return nil;
    }
    
    for (int i = (int)captionList.count - 1; i >= 0; i--) {
        NvsTimelineCaption *caption = captionList[i];
        NSArray *array = [caption getBoundingRectangleVertices];
        NSValue *leftTopValue = array[0];
        NSValue *leftBottomValue = array[1];
        NSValue *rightBottomValue = array[2];
        NSValue *rightTopValue = array[3];
        CGPoint topLeftCorner = [leftTopValue CGPointValue];
        CGPoint bottomLeftCorner = [leftBottomValue CGPointValue];
        CGPoint rightBottomCorner = [rightBottomValue CGPointValue];
        CGPoint rightTopCorner = [rightTopValue CGPointValue];
        
        topLeftCorner = [liveWindow mapCanonicalToView:topLeftCorner];
        rightBottomCorner = [liveWindow mapCanonicalToView:rightBottomCorner];
        bottomLeftCorner = [liveWindow mapCanonicalToView:bottomLeftCorner];
        rightTopCorner = [liveWindow mapCanonicalToView:rightTopCorner];
        
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGMutablePathRef pathRef=CGPathCreateMutable();
        CGPathMoveToPoint(pathRef, NULL, topLeftCorner.x, topLeftCorner.y);
        CGPathAddLineToPoint(pathRef, NULL, bottomLeftCorner.x, bottomLeftCorner.y);
        CGPathAddLineToPoint(pathRef, NULL, rightBottomCorner.x, rightBottomCorner.y);
        CGPathAddLineToPoint(pathRef, NULL, rightTopCorner.x, rightTopCorner.y);
        CGPathCloseSubpath(pathRef);
        CGContextAddPath(ctx, pathRef);
        
        bool isSelected = CGPathContainsPoint(pathRef, nil, point, false);
        CGPathRelease(pathRef);
        if (isSelected) {
            return (NvCaptionInfoModel *)[caption getAttachment:@"captionInfoModel"];
        }
    }
    return nil;
}

+ (NvsRect)getCaptionRect:(NvsTimeline *)timeline captionInfo:(NvCaptionInfoModel *)captionInfo {
    NvsTimelineCaption *caption = [self findCaptionObject:timeline captionInfo:captionInfo];
    return caption.getTextBoundingRect;
}

+ (NvsTimelineCaption *)getTimelineCaption:(NvsTimeline *)timeline captionInfo:(NvCaptionInfoModel *)captionInfo {
    return [self findCaptionObject:timeline captionInfo:captionInfo];
}

+ (NSArray *)getCaptionBoundingPoints:(NvsTimeline *)timeline liveWindow:(NvsLiveWindow *)liveWindow captionInfo:(NvCaptionInfoModel *)captionInfo {
    NvsTimelineCaption *caption = [NvTimelineCaptionUtils findCaptionObject:timeline captionInfo:captionInfo];
    NSArray *array = [caption getBoundingRectangleVertices];
    NSValue *leftTopValue = array[0];
    NSValue *leftBottomValue = array[1];
    NSValue *rightBottomValue = array[2];
    NSValue *rightTopValue = array[3];
    CGPoint topLeftCorner = [leftTopValue CGPointValue];
    CGPoint bottomLeftCorner = [leftBottomValue CGPointValue];
    CGPoint rightBottomCorner = [rightBottomValue CGPointValue];
    CGPoint rightTopCorner = [rightTopValue CGPointValue];
    
    topLeftCorner = [liveWindow mapCanonicalToView:topLeftCorner];
    rightBottomCorner = [liveWindow mapCanonicalToView:rightBottomCorner];
    bottomLeftCorner = [liveWindow mapCanonicalToView:bottomLeftCorner];
    rightTopCorner = [liveWindow mapCanonicalToView:rightTopCorner];
    
    NSMutableArray *newarray = NSMutableArray.new;
    [newarray addObject:[NSValue valueWithCGPoint:topLeftCorner]];
    [newarray addObject:[NSValue valueWithCGPoint:bottomLeftCorner]];
    [newarray addObject:[NSValue valueWithCGPoint:rightBottomCorner]];
    [newarray addObject:[NSValue valueWithCGPoint:rightTopCorner]];
    return newarray;
}

+ (NvsTimelineCaption *)findCaptionObject:(NvsTimeline *)timeline captionInfo:(NvCaptionInfoModel *)captionInfo {
    NvsTimelineCaption *caption = [timeline getFirstCaption];
    while (caption) {
        NvCaptionInfoModel *info = (NvCaptionInfoModel *)[caption getAttachment:@"captionInfoModel"];
        if ([info.uuid isEqualToString:captionInfo.uuid])
            return caption;
        caption = [timeline getNextCaption:caption];
    }
    return nil;
}

@end
