//
//  AliyunNuiTTSBridge.m
//  suixinkan
//

#import "AliyunNuiTTSBridge.h"
#import <TargetConditionals.h>

#if !TARGET_OS_SIMULATOR && __has_include(<nuisdk/NeoNuiTts.h>)
#import <nuisdk/NeoNuiTts.h>
#define ALIYUN_NUI_TTS_AVAILABLE 1
#else
#define ALIYUN_NUI_TTS_AVAILABLE 0
#endif

#if ALIYUN_NUI_TTS_AVAILABLE
@interface AliyunNuiTTSBridge () <NeoNuiTtsDelegate>
@property (nonatomic, strong, nullable) NeoNuiTts *nui;
@end
#else
@interface AliyunNuiTTSBridge ()
@end
#endif

@implementation AliyunNuiTTSBridge

- (BOOL)sdkAvailable {
#if ALIYUN_NUI_TTS_AVAILABLE
    return YES;
#else
    return NO;
#endif
}

- (NSInteger)initializeWithTicket:(NSString *)ticket logLevel:(NSInteger)logLevel saveLog:(BOOL)saveLog {
#if ALIYUN_NUI_TTS_AVAILABLE
    if (self.nui != nil) {
        [self.nui nui_tts_release];
        self.nui.delegate = nil;
        self.nui = nil;
    }
    NeoNuiTts *nui = [NeoNuiTts get_instance];
    nui.delegate = self;
    NSInteger code = [nui nui_tts_initialize:[ticket UTF8String]
                                    logLevel:(NuiSdkLogLevel)logLevel
                                     saveLog:saveLog];
    if (code == 0) {
        self.nui = nui;
    } else {
        nui.delegate = nil;
        [nui nui_tts_release];
    }
    return code;
#else
    (void)ticket;
    (void)logLevel;
    (void)saveLog;
    return -1;
#endif
}

- (NSInteger)setParam:(NSString *)param value:(NSString *)value {
#if ALIYUN_NUI_TTS_AVAILABLE
    if (self.nui == nil) { return -1; }
    return [self.nui nui_tts_set_param:[param UTF8String] value:[value UTF8String]];
#else
    (void)param;
    (void)value;
    return -1;
#endif
}

- (NSInteger)playWithPriority:(NSString *)priority taskId:(NSString *)taskId text:(NSString *)text {
#if ALIYUN_NUI_TTS_AVAILABLE
    if (self.nui == nil) { return -1; }
    return [self.nui nui_tts_play:[priority UTF8String] taskId:[taskId UTF8String] text:[text UTF8String]];
#else
    (void)priority;
    (void)taskId;
    (void)text;
    return -1;
#endif
}

- (NSInteger)cancelWithTaskId:(NSString *)taskId {
#if ALIYUN_NUI_TTS_AVAILABLE
    if (self.nui == nil) { return -1; }
    const char *task = taskId.length > 0 ? [taskId UTF8String] : NULL;
    return [self.nui nui_tts_cancel:task];
#else
    (void)taskId;
    return -1;
#endif
}

- (NSInteger)releaseEngine {
#if ALIYUN_NUI_TTS_AVAILABLE
    if (self.nui == nil) { return 0; }
    NSInteger code = [self.nui nui_tts_release];
    self.nui.delegate = nil;
    self.nui = nil;
    return code;
#else
    return 0;
#endif
}

- (NSString *)paramValueForKey:(NSString *)key {
#if ALIYUN_NUI_TTS_AVAILABLE
    if (self.nui == nil) { return nil; }
    const char *value = [self.nui nui_tts_get_param:[key UTF8String]];
    if (value == NULL) { return nil; }
    return [NSString stringWithUTF8String:value];
#else
    (void)key;
    return nil;
#endif
}

#if ALIYUN_NUI_TTS_AVAILABLE
- (void)onNuiTtsEventCallback:(NuiSdkTtsEvent)event taskId:(char *)taskid code:(int)code {
    NSString *taskId = taskid != NULL ? [NSString stringWithUTF8String:taskid] : nil;
    [self.delegate aliyunNuiTTSBridge:self didReceiveEvent:event taskId:taskId code:code];
}

- (void)onNuiTtsUserdataCallback:(char *)info infoLen:(int)info_len buffer:(char *)buffer len:(int)len taskId:(char *)task_id {
    if (buffer == NULL || len <= 0) { return; }
    NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)len];
    NSString *taskId = task_id != NULL ? [NSString stringWithUTF8String:task_id] : nil;
    NSString *infoText = nil;
    if (info != NULL && info_len > 0) {
        infoText = [[NSString alloc] initWithBytes:info length:(NSUInteger)info_len encoding:NSUTF8StringEncoding];
    }
    [self.delegate aliyunNuiTTSBridge:self didReceiveAudioData:data taskId:taskId info:infoText];
}
#endif

@end
