//
//  NvTimelineStickerUtils.m
//  SDKDemo
//
//  Created by 施周虎 on 2018/6/30.
 //
//

#import "NvTimelineStickerUtils.h"
#import "NvTimelineUtils.h"
#import "NvTimelineData.h"
#import "NvUtils.h"
#import "NvsTimelineAnimatedSticker.h"
#import "NvsStreamingContext.h"

@implementation NvTimelineStickerUtils

+ (NSString *)addSticker:(NvsTimeline *)timeline stickerInfo:(NvStickerInfoModel *)stickerInfo {
    [[[NvTimelineData sharedInstance] stickerDataArray] addObject:stickerInfo];
    [NvTimelineUtils resetSticker:timeline stickerDataArray:[[NvTimelineData sharedInstance] stickerDataArray]];
    return stickerInfo.uuid;
}

+ (void)updateSticker:(NvsTimeline *)timeline stickerInfo:(NvStickerInfoModel *)stickerInfo {
    NvsTimelineAnimatedSticker *stickerEffect = [NvTimelineStickerUtils findStickerObject:timeline stickerInfo:stickerInfo];
    if (stickerEffect == nil)
        return;
    [stickerEffect setScale:stickerInfo.scale];
    [stickerEffect setTranslation:stickerInfo.translation];
    [stickerEffect setRotationZ:stickerInfo.rotation];
    [stickerEffect changeInPoint:stickerInfo.inPoint];
    [stickerEffect changeOutPoint:stickerInfo.outPoint];
    
    NSMutableArray *stickerlist = [[NvTimelineData sharedInstance] stickerDataArray];
    if (stickerlist.count == 0 || [NvUtils isStringEmpty:stickerInfo.uuid]) {
        return;
    }
    for (NvStickerInfoModel* info in stickerlist) {
        if ([stickerInfo.uuid isEqualToString:[(NvStickerInfoModel *)info uuid]]) {
            info.scale = stickerInfo.scale;
            info.translation = stickerInfo.translation;
            info.rotation = stickerInfo.rotation;
            info.inPoint = stickerInfo.inPoint;
            info.outPoint = stickerInfo.outPoint;
            break;
        }
    }
}

+ (NvStickerInfoModel *)removeSticker:(NvsTimeline *)timeline stickerInfo:(NvStickerInfoModel *)stickerInfo {
    NSMutableArray *stickerlist = [[NvTimelineData sharedInstance] stickerDataArray];
    NvStickerInfoModel *nextSticker = nil;
    for (int i = 0; i < stickerlist.count; i++) {
        NvStickerInfoModel *info = stickerlist[i];
        if ([info.uuid isEqualToString:stickerInfo.uuid]) {
            [stickerlist removeObject:info];
            if (i < stickerlist.count) {
                nextSticker = stickerlist[i];
            } else if (stickerlist.count > 0) {
                nextSticker = stickerlist[stickerlist.count-1];
            }
            break;
        }
    }
    [NvTimelineUtils resetSticker:timeline stickerDataArray:[[NvTimelineData sharedInstance] stickerDataArray]];
    return nextSticker;
}

+ (NvStickerInfoModel *)getStickerByPoint:(NvsTimeline *)timeline liveWindow:(NvsLiveWindow *)liveWindow point:(CGPoint)point{
    NvsStreamingContext *context = [NvsStreamingContext sharedInstance];
    NSArray *stickerList = [timeline getAnimatedStickersByTimelinePosition:[context getTimelineCurrentPosition:timeline]];
    if (stickerList.count == 0) {
        return nil;
    }
    
    for (int i = (int)stickerList.count - 1; i >= 0; i--) {
        NvsTimelineAnimatedSticker *sticker = stickerList[i];
        NSArray *array = [sticker 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 (NvStickerInfoModel *)[sticker getAttachment:@"stickerInfoModel"];
        }
    }
    return nil;
}

+ (NSArray *)getStickerBoundingPoints:(NvsTimeline *)timeline liveWindow:(NvsLiveWindow *)liveWindow stickerInfo:(NvStickerInfoModel *)stickerInfo {
    NvsTimelineAnimatedSticker *sticker = [NvTimelineStickerUtils findStickerObject:timeline stickerInfo:stickerInfo];
    NSArray *array = [sticker 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;
}

+ (NvsTimelineAnimatedSticker *)findStickerObject:(NvsTimeline *)timeline stickerInfo:(NvStickerInfoModel *)stickerInfo {
    NvsTimelineAnimatedSticker *sticker = [timeline getFirstAnimatedSticker];
    while (sticker) {
        NvStickerInfoModel *info = (NvStickerInfoModel *)[sticker getAttachment:@"stickerInfoModel"];
        if ([info.uuid isEqualToString:stickerInfo.uuid])
            return sticker;
        sticker = [timeline getNextAnimatedSticker:sticker];
    }
    return nil;
}
@end
