/*
 * Copyright 2024 KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 */

#ifndef SPEECH_H
#define SPEECH_H

#include <kylin-ai/aiengine.h>
#include <kylin-ai/common/error.h>

#include <map>

#include "utils/logger.h"

typedef enum {
    SPEECH_RECOGNITION_AUDIO_DATA_SIZE_INVALID = 100,
    SPEECH_SYNTHESIS_TEXT_LENGTH_INVALID,
    SPEECH_PARAM_INVALID,
    SPEECH_DEFAULT_MICROPHONE_INVALID,
    SPEECH_UNKNOWN_ERROR,
    SPEECH_UNSUPPORTED_LANGUAGE
} SpeechErrorCode;

namespace engineError {
inline std::map<int, int> speechEngineErrorMap{
    {(int)ai_engine::SpeechEngineErrorCode::NomoreMemory, AiSdkCommonErrorCode::AISDK_SERVICE_ERROR},
    {(int)ai_engine::SpeechEngineErrorCode::FileNotFound, AiSdkCommonErrorCode::AISDK_SERVICE_ERROR},
    {(int)ai_engine::SpeechEngineErrorCode::VersionMismatched, AiSdkCommonErrorCode::AISDK_SERVICE_ERROR},

    {(int)ai_engine::SpeechEngineErrorCode::FilePermissionDenied, AiSdkCommonErrorCode::AISDK_AUTHENTICATION_FAILURE},
    {(int)ai_engine::SpeechEngineErrorCode::NetworkDisconnected, AiSdkCommonErrorCode::AISDK_NET_ERROR},
    {(int)ai_engine::SpeechEngineErrorCode::NetworkLatencyTooLong, AiSdkCommonErrorCode::AISDK_NET_ERROR},

    {(int)ai_engine::SpeechEngineErrorCode::FailedToConnectServer, AiSdkCommonErrorCode::AISDK_SERVICE_ERROR},
    {(int)ai_engine::SpeechEngineErrorCode::ServerNoResponse, AiSdkCommonErrorCode::AISDK_SERVICE_ERROR},
    {(int)ai_engine::SpeechEngineErrorCode::NoServerService, AiSdkCommonErrorCode::AISDK_SERVICE_ERROR},

    {(int)ai_engine::SpeechEngineErrorCode::Unauthorized, AiSdkCommonErrorCode::AISDK_AUTHENTICATION_FAILURE},
    {(int)ai_engine::SpeechEngineErrorCode::TooLittleParams, SpeechErrorCode::SPEECH_SYNTHESIS_TEXT_LENGTH_INVALID},
    {(int)ai_engine::SpeechEngineErrorCode::TooManyParams, SpeechErrorCode::SPEECH_SYNTHESIS_TEXT_LENGTH_INVALID},

    {(int)ai_engine::SpeechEngineErrorCode::InvalidParamType, SpeechErrorCode::SPEECH_PARAM_INVALID},
    {(int)ai_engine::SpeechEngineErrorCode::TooLittleData, SpeechErrorCode::SPEECH_RECOGNITION_AUDIO_DATA_SIZE_INVALID},
    {(int)ai_engine::SpeechEngineErrorCode::TooManyData, SpeechErrorCode::SPEECH_RECOGNITION_AUDIO_DATA_SIZE_INVALID},

    {(int)ai_engine::SpeechEngineErrorCode::TooFrequentRequest, AiSdkCommonErrorCode::AISDK_TOO_MANY_REQUESTS},
    {(int)ai_engine::SpeechEngineErrorCode::TooManyRequest, AiSdkCommonErrorCode::AISDK_TOO_MANY_REQUESTS},

    {(int)ai_engine::SpeechEngineErrorCode::UnsupportedLanguage, SpeechErrorCode::SPEECH_UNSUPPORTED_LANGUAGE},
    {(int)ai_engine::SpeechEngineErrorCode::UnsupportedScript, SpeechErrorCode::SPEECH_UNKNOWN_ERROR},
    {(int)ai_engine::SpeechEngineErrorCode::InvalidWakeupWords, SpeechErrorCode::SPEECH_PARAM_INVALID},
    {(int)ai_engine::SpeechEngineErrorCode::SslCertificateError, AiSdkCommonErrorCode::AISDK_SSL_CERTIFICATE_ERROR},
};

static int getSpeechErrorCode(int engineErrorCode) {
    if (engineError::speechEngineErrorMap.find(engineErrorCode) != engineError::speechEngineErrorMap.end()) {
        return engineError::speechEngineErrorMap.at(engineErrorCode);
    } else {
        return AiSdkCommonErrorCode::AISDK_NO_ERROR;
    }
}

}  // namespace engineError

#endif  // SPEECH_H
