//
//  NvBeautyLogic.m
//  SDKDemo
//
//  Created by 刘东旭 on 2018/11/9.
 //
//

#import "NvBeautyLogic.h"
#import "NvsStreamingContext.h"

#ifdef NV_AI_FU
#import "NvsFaceEffectV1.h"
#import "authpack.h"
#endif

@interface NvBeautyLogic()

@property (nonatomic, strong) NvsStreamingContext *streamingContext;

@end

@implementation NvBeautyLogic

- (instancetype)init {
    self = [super init];
    if (self) {
        self.streamingContext = [NvsStreamingContext sharedInstanceWithFlags:NvsStreamingContextFlag_Support4KEdit];
    }
    return self;
}

- (void)startBeauty {
    if (!self.fx) {
        self.fx = [self.streamingContext appendBuiltinCaptureVideoFx:@"Beauty"];
//        double val = [self.fx getFloatVal:@"Reddening"];
//        [self.fx setFloatVal:@"Reddening" val:val];
        //抖音里默认红润设为0.5
        [self.fx setFloatVal:@"Reddening" val:0.5];
    }
}

- (void)setStrength:(double)strength {
    _strength = strength;
    [self.fx setFloatVal:@"Strength" val:strength];
}

- (void)setReddening:(double)reddening {
    _reddening = reddening;
    [self.fx setFloatVal:@"Reddening" val:reddening];
}

- (void)setWhitening:(double)whitening {
    _whitening = whitening;
    [self.fx setFloatVal:@"Whitening" val:whitening];
}

- (void)closeBeauty {
    if (self.fx) {
        [self.streamingContext removeCaptureVideoFx:self.fx.index];
        self.fx = nil;
    }
}

- (BOOL)beauty {
    return self.fx;
}

- (BOOL)enAbleAI {
#ifdef NV_AI_FU
    return YES;
#endif
    return NO;
}

- (void)startFaceEffect {
#ifdef NV_AI_FU
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        NSString *str = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"NvFaceData.bundle"];
        
        [NvsFaceEffectV1 InitFaceEffectV1:str authPackage:&g_auth_package authSize:sizeof(g_auth_package)];
        [NvsFaceEffectV1 SetMaxFaces:1];
        
    });
    
    self.arfaceVideoFx = [self.streamingContext appendBuiltinCaptureVideoFx:@"Face Effect"];
    NSString *str1 = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"NvBeautification.bundle"];
    [self.arfaceVideoFx setStringVal:@"Beautification Package" val:str1];
    //美型程度
    [self.arfaceVideoFx setFloatVal:@"Face Shape Level" val:4];
    //美型类型（网红，女神这一类的，这里是自定义）
    [self.arfaceVideoFx setMenuVal:@"Face Type" val:@"Custom"];
    self.eyeEnlarging = 0;
    self.cheekThinning = 0;

#endif
}

- (void)setFaceOrnament:(NSString *)faceOrnament {
    _faceOrnament = faceOrnament;
    //道具
    [self.arfaceVideoFx setStringVal:@"Face Ornament" val:faceOrnament];
}

- (void)setEyeEnlarging:(double)eyeEnlarging {
    _eyeEnlarging = eyeEnlarging;
    //大眼
    [self.arfaceVideoFx setFloatVal:@"Eye Enlarging" val:eyeEnlarging];
}

- (void)setCheekThinning:(double)cheekThinning {
    _cheekThinning = cheekThinning;
    //瘦脸
    [self.arfaceVideoFx setFloatVal:@"Cheek Thinning" val:cheekThinning];
}

- (void)closeFaceEffect {
    if (self.arfaceVideoFx) {
        [self.streamingContext removeCaptureVideoFx:self.arfaceVideoFx.index];
        self.arfaceVideoFx = nil;
    }
}

@end
