//
//  NvSDKUtils.m
//  SDKDemo
//
//  Created by 刘东旭 on 2018/10/30.
 //
//

#import "NvSDKUtils.h"
#import "NVDefineConfig.h"

@implementation NvSDKUtils

+ (NvsStreamingContext *)getSDKContext {
    return [NvsStreamingContext sharedInstanceWithFlags:NvsStreamingContextFlag_Support4KEdit];
}

+ (NSString *)getMoreTitleName:(AssetType )type {
    NSString *string = @"";
    switch (type) {
        case ASSET_THEME:
            string = @"更多主题";
            break;
        case ASSET_FILTER:
            string = @"更多滤镜";
            break;
        case ASSET_CAPTION_STYLE:
            string = @"更多字幕样式";
            break;
        case ASSET_ANIMATED_STICKER:
            string = @"更多贴纸";
            break;
        case ASSET_CUSTOM_ANIMATED_STICKER:
            string = @"更多自定义效果";
            break;
        case ASSET_VIDEO_TRANSITION:
            string = @"更多转场";
            break;
        case ASSET_CAPTURE_SCENE:
            string = @"更多拍摄场景";
            break;
        case ASSET_PARTICLE:
            string = @"更多粒子";
            break;
        case ASSET_FACE_STICKER:
            string = @"更多人脸道具";
            break;
        case ASSET_FACE1_STICKER:
            string = @"更多人脸道具";
            break;
        default:
            break;
    }
    return string;
}

+ (NSString *)getAssetAspectRatioString:(int)aspectRatio {
    if (aspectRatio == 127) {
        return @"通用";
    }
    NSString *string = @"";
    if ((aspectRatio&AspectRatio_9v16) == AspectRatio_9v16) {
        string = [string stringByAppendingString:@"9v16,"];
    }
    if ((aspectRatio&AspectRatio_16v9) == AspectRatio_16v9) {
        string = [string stringByAppendingString:@"16v9,"];
    }
    if ((aspectRatio&AspectRatio_3v4) == AspectRatio_3v4) {
        string = [string stringByAppendingString:@"3v4,"];
    }
    if ((aspectRatio&AspectRatio_4v3) == AspectRatio_4v3) {
        string = [string stringByAppendingString:@"4v3,"];
    }
    if ((aspectRatio&AspectRatio_1v1) == AspectRatio_1v1) {
        string = [string stringByAppendingString:@"1v1,"];
    }
    NSLog(@"SDKDemostring:%@----%d---%d",string,aspectRatio&AspectRatio_18v9,aspectRatio&AspectRatio_9v18);
    return [string substringToIndex:string.length - 1];
}

+ (NSString *)getAssetPackageSizeString:(int)packageSize {
    if (packageSize < 1024) {
        return [NSString stringWithFormat:@"<1KB"];
    } else if (packageSize >= 1024 && packageSize < 1024*1024) {
        return [NSString stringWithFormat:@"%dKB", packageSize/1024];
    } else if (packageSize >= 1024*1024 && packageSize < 1024*1024*1024) {
        return [NSString stringWithFormat:@"%.2fMB", (float)packageSize/(1024*1024)];
    } else {
        return [NSString stringWithFormat:@">1GB"];
    }
}

+ (NSString *)getAssetDownloadPath {
    NSString *file = [NSHomeDirectory() stringByAppendingPathComponent:NV_ASSET_DOWNLOAD_PATH];
    if (![[NSFileManager defaultManager] fileExistsAtPath:file isDirectory:nil]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:file withIntermediateDirectories:YES attributes:nil error:nil];
    }
    return file;
}

+ (NSString *)getAssetDownloadPath:(AssetType)assetType {
    NSString *file = [NSHomeDirectory() stringByAppendingPathComponent:NV_ASSET_DOWNLOAD_PATH];
    if (![[NSFileManager defaultManager] fileExistsAtPath:file isDirectory:nil]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:file withIntermediateDirectories:YES attributes:nil error:nil];
    }
    switch (assetType) {
        case ASSET_THEME: {
            NSString *theme = [NSHomeDirectory() stringByAppendingPathComponent:NV_ASSET_DOWNLOAD_PATH_THEME];
            if (![[NSFileManager defaultManager] fileExistsAtPath:theme isDirectory:nil]) {
                [[NSFileManager defaultManager] createDirectoryAtPath:theme withIntermediateDirectories:YES attributes:nil error:nil];
            }
            return theme;
        }
        case ASSET_FILTER: {
            NSString *filter = [NSHomeDirectory() stringByAppendingPathComponent:NV_ASSET_DOWNLOAD_PATH_FILTER];
            if (![[NSFileManager defaultManager] fileExistsAtPath:filter isDirectory:nil]) {
                [[NSFileManager defaultManager] createDirectoryAtPath:filter withIntermediateDirectories:YES attributes:nil error:nil];
            }
            return filter;
        }
        case ASSET_CAPTION_STYLE: {
            NSString *caption = [NSHomeDirectory() stringByAppendingPathComponent:NV_ASSET_DOWNLOAD_PATH_CAPTION];
            if (![[NSFileManager defaultManager] fileExistsAtPath:caption isDirectory:nil]) {
                [[NSFileManager defaultManager] createDirectoryAtPath:caption withIntermediateDirectories:YES attributes:nil error:nil];
            }
            return caption;
        }
        case ASSET_ANIMATED_STICKER: {
            NSString *animatedSticker = [NSHomeDirectory() stringByAppendingPathComponent:NV_ASSET_DOWNLOAD_PATH_ANIMATEDSTICKER];
            if (![[NSFileManager defaultManager] fileExistsAtPath:animatedSticker isDirectory:nil]) {
                [[NSFileManager defaultManager] createDirectoryAtPath:animatedSticker withIntermediateDirectories:YES attributes:nil error:nil];
            }
            return animatedSticker;
        }
        case ASSET_VIDEO_TRANSITION: {
            NSString *transition = [NSHomeDirectory() stringByAppendingPathComponent:NV_ASSET_DOWNLOAD_PATH_TRANSITION];
            if (![[NSFileManager defaultManager] fileExistsAtPath:transition isDirectory:nil]) {
                [[NSFileManager defaultManager] createDirectoryAtPath:transition withIntermediateDirectories:YES attributes:nil error:nil];
            }
            return transition;
        }
        case ASSET_CAPTURE_SCENE: {
            NSString *captureScene = [NSHomeDirectory() stringByAppendingPathComponent:NV_ASSET_DOWNLOAD_PATH_CAPTURE_SCENE];
            if (![[NSFileManager defaultManager] fileExistsAtPath:captureScene isDirectory:nil]) {
                [[NSFileManager defaultManager] createDirectoryAtPath:captureScene withIntermediateDirectories:YES attributes:nil error:nil];
            }
            return captureScene;
        }
        case ASSET_PARTICLE: {
            NSString *particle = [NSHomeDirectory() stringByAppendingPathComponent:NV_ASSET_DOWNLOAD_PATH_PARTICLE];
            if (![[NSFileManager defaultManager] fileExistsAtPath:particle isDirectory:nil]) {
                [[NSFileManager defaultManager] createDirectoryAtPath:particle withIntermediateDirectories:YES attributes:nil error:nil];
            }
            return particle;
        }
        case ASSET_FACE_STICKER: {
            NSString *faceSticker = [NSHomeDirectory() stringByAppendingPathComponent:NV_ASSET_DOWNLOAD_PATH_FACE_STICKER];
            if (![[NSFileManager defaultManager] fileExistsAtPath:faceSticker isDirectory:nil]) {
                [[NSFileManager defaultManager] createDirectoryAtPath:faceSticker withIntermediateDirectories:YES attributes:nil error:nil];
            }
            return faceSticker;
        }
        case ASSET_CUSTOM_ANIMATED_STICKER: {
            NSString *customSticker = [NSHomeDirectory() stringByAppendingPathComponent:NV_ASSET_DOWNLOAD_PATH_CUSTOM_ANIMATED_STICKER];
            if (![[NSFileManager defaultManager] fileExistsAtPath:customSticker isDirectory:nil]) {
                [[NSFileManager defaultManager] createDirectoryAtPath:customSticker withIntermediateDirectories:YES attributes:nil error:nil];
            }
            return customSticker;
        }
        case ASSET_FACE1_STICKER: {
            NSString *face1Sticker = [NSHomeDirectory() stringByAppendingPathComponent:NV_ASSET_DOWNLOAD_PATH_FACE1_STICKER];
            if (![[NSFileManager defaultManager] fileExistsAtPath:face1Sticker isDirectory:nil]) {
                [[NSFileManager defaultManager] createDirectoryAtPath:face1Sticker withIntermediateDirectories:YES attributes:nil error:nil];
            }
            return face1Sticker;
        }
        default:
            break;
    }
    return file;
}

+ (BOOL)isBuiltinFilter:(NSString *)filterName {
    if (!filterName) {
        NSLog(@"滤镜名字为空%@",filterName);
    }
    NvsStreamingContext *context = [NvsStreamingContext sharedInstance];
    NSMutableArray *array = [NSMutableArray arrayWithArray:[context getAllBuiltinVideoFxNames]];
    [array addObject:@"Video Echo"];
    [array addObject:@"Cartoon"];
    
    for (NSString *_Nullable str in array) {
        if (filterName == nil) {
            continue;
        }
        if ([str isEqualToString:filterName]) {
            return YES;
        }
    }
    return NO;
}

+ (int)getAssetVersionWithPath:(NSString *)path {
    NSArray *array = [path componentsSeparatedByString:@"/"];
    if (array.count > 0) {
        NSString *filename = [array lastObject];
        NSArray *arr = [filename componentsSeparatedByString:@"."];
        if (arr.count == 3) {
            return [(NSString *)arr[1] intValue];
        } else {
            return 1;
        }
    } else {
        return 1;
    }
}

+ (NSString *)getEffectColor:(NSString *)fxUUID {
    
    NSString *packagePath = [[[NSBundle bundleForClass:[self class]] bundlePath] stringByAppendingPathComponent:@"douyinPackage"];
    NSString *jsonPath = [packagePath stringByAppendingPathComponent:@"fx.json"];
    NSString *jsontext = [NSString stringWithContentsOfFile:jsonPath encoding:NSUTF8StringEncoding error:nil];
    NSData *data =[jsontext dataUsingEncoding:NSUTF8StringEncoding];
    NSArray *array =[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    
    for (int i = 0; i < array.count; i++) {
        NSDictionary *dic = array[i];
        if ([fxUUID isEqualToString:dic[@"fxid"]]) {
            return dic[@"color"];
        }
    }
    return @"FCB600";
//    if ([fxUUID isEqualToString:NV_EFFECT_UUID_DOUDONG]) {//抖动
//        return NV_EFFECT_COLOR_DOUDONG;
//    } else if ([fxUUID isEqualToString:NV_EFFECT_UUID_HUANJUE]) {//幻觉
//        return NV_EFFECT_COLOR_HUANJUE;
//    } else if ([fxUUID isEqualToString:NV_EFFECT_UUID_LINHUNCHUQIAO]) {//灵魂出窍
//        return NV_EFFECT_COLOR_LINHUNCHUQIAO;
//    } else if ([fxUUID isEqualToString:NV_EFFECT_UUID_JINGXIANG]) {//镜像
//        return NV_EFFECT_COLOR_JINGXIANG;
//    } else if ([fxUUID isEqualToString:NV_EFFECT_UUID_BOLANG]) {//波浪
//        return NV_EFFECT_COLOR_BOLANG;
//    } else if ([fxUUID isEqualToString:NV_EFFECT_UUID_HEIMOFA]) {//黑魔法
//        return NV_EFFECT_COLOR_HEIMOFA;
//    } else if ([fxUUID isEqualToString:@"66A7D54F-79C4-4DCC-82F7-F23949D92E0A"]) {//缩放
//        return @"0B1746";
//    } else if ([fxUUID isEqualToString:@"576A09D7-766B-427F-AF19-E8BD6B6F2F1B"]) {//霓虹
//        return @"32CD32";
//    } else if ([fxUUID isEqualToString:@"D2E1F664-58BB-46E5-ACEC-AC644E1286D8"]) {//闪白
//        return @"FF0000";
//    } else if ([fxUUID isEqualToString:@"0084E862-5D59-4289-AD4A-59BB04C7CC3F"]) {//70s
//        return @"32CD32";
//    } else if ([fxUUID isEqualToString:@"A6D9A119-F5C6-6DD9-D053-1913F40827B5"]) {//粒子
//        return @"00FA9A";
//    } else if ([fxUUID isEqualToString:@"2D943DC3-BB1B-4479-BE46-AE229B360AE9"]) {//毛刺
//        return @"32CD32";
//    } else if ([fxUUID isEqualToString:@"1B1080CC-6E32-E2D7-0C6B-77494225A229"]) {//线性
//        return @"CD00CD";
//    } else if ([fxUUID isEqualToString:@"9AC28816-639F-4A9B-B4BA-4060ABD229A2"]) {//x-signal
//        return @"9F79EE";
//    } else {
//        return NV_EFFECT_COLOR_DOUDONG;
//    }
}

+ (int64_t)getVideoDuration:(NSString *)path {
    NvsStreamingContext *context = [NvsStreamingContext sharedInstance];
    NvsAVFileInfo *fileInfo = [context getAVFileInfo:path];
    return fileInfo.duration;
}

+ (NSString *)getEffectCoverName:(NSString *)fxUUID {
    if ([fxUUID isEqualToString:NV_EFFECT_UUID_DOUDONG]) {
        return @"NvsFxDoudong";
    } else if ([fxUUID isEqualToString:NV_EFFECT_UUID_HUANJUE]) {
        return @"NvsFxHuanjue";
    } else if ([fxUUID isEqualToString:NV_EFFECT_UUID_LINHUNCHUQIAO]) {
        return @"NvsFxLinhunchuqiao";
    } else if ([fxUUID isEqualToString:NV_EFFECT_UUID_JINGXIANG]) {
        return @"NvsFxJingxiang";
    } else if ([fxUUID isEqualToString:NV_EFFECT_UUID_BOLANG]) {
        return @"NvsFxBolang";
    } else if ([fxUUID isEqualToString:NV_EFFECT_UUID_HEIMOFA]) {
        return @"NvsFxBlackmagic";
    } else {
        return @"NvsFxDoudong";
    }
}

+ (NSString *)getFilterCoverName:(NSString *)fxUUID {
    if ([fxUUID isEqualToString:@"None"]) {
        return @"NvsFilterNone";
    } else if ([fxUUID isEqualToString:@"Fresh"]) {
        return @"NvsFilterFresh";
    } else if ([fxUUID isEqualToString:@"Lace"]) {
        return @"NvsFilterLace";
    } else if ([fxUUID isEqualToString:@"Mace"]) {
        return @"NvsFilterMace";
    } else if ([fxUUID isEqualToString:@"Maid"]) {
        return @"NvsFilterMaid";
    } else if ([fxUUID isEqualToString:@"Mall"]) {
        return @"NvsFilterMall";
    } else if ([fxUUID isEqualToString:@"Pinky"]) {
        return @"NvsFilterPinky";
    } else if ([fxUUID isEqualToString:@"Sage"]) {
        return @"NvsFilterSage";
    } else if ([fxUUID isEqualToString:@"Sap"]) {
        return @"NvsFilterSap";
    } else if ([fxUUID isEqualToString:@"Sara"]) {
        return @"NvsFilterSara";
    } else if ([fxUUID isEqualToString:@"Sweet"]) {
        return @"NvsFilterSweet";
    }
    return @"NvsFilterNone";
}

+ (NSString *)getTransitionsCoverName:(NSString *)fxUUID {
    if ([fxUUID isEqualToString:@"Fade"]) {
        return @"fade";
    } else if ([fxUUID isEqualToString:@"Turning"]) {
        return @"turning";
    } else if ([fxUUID isEqualToString:@"Swap"]) {
        return @"swap";
    } else if ([fxUUID isEqualToString:@"Stretch In"]) {
        return @"stretch_in";
    } else if ([fxUUID isEqualToString:@"Page Curl"]) {
        return @"page_curl";
    } else if ([fxUUID isEqualToString:@"Lens Flare"]) {
        return @"lens_flare";
    } else if ([fxUUID isEqualToString:@"Star"]) {
        return @"star";
    } else if ([fxUUID isEqualToString:@"Dip To Black"]) {
        return @"dip_to_black";
    } else if ([fxUUID isEqualToString:@"Dip To White"]) {
        return @"dip_to_white";
    } else if ([fxUUID isEqualToString:@"Push To Right"]) {
        return @"push_to_right";
    } else if ([fxUUID isEqualToString:@"Push To Top"]) {
        return @"push_to_left";
    } else if ([fxUUID isEqualToString:@"Upper Left Into"]) {
        return @"upper_left_into";
    }
    return @"NvsFilterNone";
}

@end
