/*
 * 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/>.
 */

#include "servererror.h"
#include "util.h"

namespace {

const std::map<int, xunfei_speech_server_error::EngineErrorCode>
    asrErrorCodeMap = {{10005,
                        {ai_engine::EngineErrorCategory::Initialization,
                         ai_engine::SpeechEngineErrorCode::Unauthorized}},
                       {10006,
                        {ai_engine::EngineErrorCategory::InputError,
                         ai_engine::SpeechEngineErrorCode::TooLittleParams}},
                       {10007,
                        {ai_engine::EngineErrorCategory::InputError,
                         ai_engine::SpeechEngineErrorCode::InvalidParamType}},
                       {10010,
                        {ai_engine::EngineErrorCategory::Initialization,
                         ai_engine::SpeechEngineErrorCode::Unauthorized}},
                       {10014,
                        {ai_engine::EngineErrorCategory::OutputError,
                         ai_engine::SpeechEngineErrorCode::ServerNoResponse}},
                       {10019,
                        {ai_engine::EngineErrorCategory::OutputError,
                         ai_engine::SpeechEngineErrorCode::ServerNoResponse}},
                       {10043,
                        {ai_engine::EngineErrorCategory::InputError,
                         ai_engine::SpeechEngineErrorCode::InvalidParamType}},
                       {10101,
                        {ai_engine::EngineErrorCategory::InputError,
                         ai_engine::SpeechEngineErrorCode::TooManyData}},
                       {10114,
                        {ai_engine::EngineErrorCategory::InputError,
                         ai_engine::SpeechEngineErrorCode::TooManyData}},
                       {10139,
                        {ai_engine::EngineErrorCategory::InputError,
                         ai_engine::SpeechEngineErrorCode::InvalidParamType}},
                       {10313,
                        {ai_engine::EngineErrorCategory::InputError,
                         ai_engine::SpeechEngineErrorCode::Unauthorized}},
                       {10317,
                        {ai_engine::EngineErrorCategory::InputError,
                         ai_engine::SpeechEngineErrorCode::InvalidParamType}},
                       {11200,
                        {ai_engine::EngineErrorCategory::InputError,
                         ai_engine::SpeechEngineErrorCode::Unauthorized}},
                       {11201,
                        {ai_engine::EngineErrorCategory::InputError,
                         ai_engine::SpeechEngineErrorCode::TooFrequentRequest}},
                       {10160,
                        {ai_engine::EngineErrorCategory::InputError,
                         ai_engine::SpeechEngineErrorCode::InvalidParamType}},
                       {10161,
                        {ai_engine::EngineErrorCategory::InputError,
                         ai_engine::SpeechEngineErrorCode::InvalidParamType}},
                       {10163,
                        {ai_engine::EngineErrorCategory::InputError,
                         ai_engine::SpeechEngineErrorCode::InvalidParamType}},
                       {10165,
                        {ai_engine::EngineErrorCategory::InputError,
                         ai_engine::SpeechEngineErrorCode::TooLittleParams}},
                       {10200,
                        {ai_engine::EngineErrorCategory::OutputError,
                         ai_engine::SpeechEngineErrorCode::ServerNoResponse}}};

const std::map<int, xunfei_speech_server_error::EngineErrorCode>
    realtimeAsrErrorCodeMap = {
        {10105,
         {ai_engine::EngineErrorCategory::InputError,
          ai_engine::SpeechEngineErrorCode::Unauthorized}},
        {10106,
         {ai_engine::EngineErrorCategory::InputError,
          ai_engine::SpeechEngineErrorCode::TooLittleParams}},
        {10107,
         {ai_engine::EngineErrorCategory::InputError,
          ai_engine::SpeechEngineErrorCode::InvalidParamType}},
        {10110,
         {ai_engine::EngineErrorCategory::InputError,
          ai_engine::SpeechEngineErrorCode::Unauthorized}},
        {10700,
         {ai_engine::EngineErrorCategory::OutputError,
          ai_engine::SpeechEngineErrorCode::NoServerService}},
        {10202,
         {ai_engine::EngineErrorCategory::Initialization,
          ai_engine::SpeechEngineErrorCode::FailedToConnectServer}},
        {10204,
         {ai_engine::EngineErrorCategory::OutputError,
          ai_engine::SpeechEngineErrorCode::NoServerService}},
        {10205,
         {ai_engine::EngineErrorCategory::OutputError,
          ai_engine::SpeechEngineErrorCode::NoServerService}},
        {16003,
         {ai_engine::EngineErrorCategory::OutputError,
          ai_engine::SpeechEngineErrorCode::NoServerService}},
        {10800,
         {ai_engine::EngineErrorCategory::InputError,
          ai_engine::SpeechEngineErrorCode::TooFrequentRequest}}};

const std::map<int, xunfei_speech_server_error::EngineErrorCode>
    ttsErrorCodeMap = {
        {10005,
         {ai_engine::EngineErrorCategory::Initialization,
          ai_engine::SpeechEngineErrorCode::Unauthorized}},
        {10006,
         {ai_engine::EngineErrorCategory::InputError,
          ai_engine::SpeechEngineErrorCode::TooLittleParams}},
        {10007,
         {ai_engine::EngineErrorCategory::InputError,
          ai_engine::SpeechEngineErrorCode::InvalidParamType}},
        {10010,
         {ai_engine::EngineErrorCategory::Initialization,
          ai_engine::SpeechEngineErrorCode::Unauthorized}},
        {10109,
         {ai_engine::EngineErrorCategory::InputError,
          ai_engine::SpeechEngineErrorCode::TooManyData}},
        {10019,
         {ai_engine::EngineErrorCategory::OutputError,
          ai_engine::SpeechEngineErrorCode::ServerNoResponse}},
        {10101,
         {ai_engine::EngineErrorCategory::InputError,
          ai_engine::SpeechEngineErrorCode::TooManyData}},
        {10313,
         {ai_engine::EngineErrorCategory::InputError,
          ai_engine::SpeechEngineErrorCode::Unauthorized}},
        {10317,
         {ai_engine::EngineErrorCategory::InputError,
          ai_engine::SpeechEngineErrorCode::InvalidParamType}},
        {11200,
         {ai_engine::EngineErrorCategory::InputError,
          ai_engine::SpeechEngineErrorCode::Unauthorized}},
        {11201,
         {ai_engine::EngineErrorCategory::InputError,
          ai_engine::SpeechEngineErrorCode::TooFrequentRequest}},
        {10160,
         {ai_engine::EngineErrorCategory::InputError,
          ai_engine::SpeechEngineErrorCode::InvalidParamType}},
        {10161,
         {ai_engine::EngineErrorCategory::InputError,
          ai_engine::SpeechEngineErrorCode::InvalidParamType}},
        {10163,
         {ai_engine::EngineErrorCategory::InputError,
          ai_engine::SpeechEngineErrorCode::InvalidParamType}},
        {10200,
         {ai_engine::EngineErrorCategory::OutputError,
          ai_engine::SpeechEngineErrorCode::ServerNoResponse}},
        {10222,
         {ai_engine::EngineErrorCategory::OutputError,
          ai_engine::SpeechEngineErrorCode::NetworkLatencyTooLong}}};

}; // namespace

int xunfei_speech_server_error::parseErrorCode(const std::string &result) {
    Json::Value obj = xunfei_speech_util::formatJsonFromString(result);
    if (!obj.isObject() || obj.isNull()) {
        return -1;
    }
    if (obj.isMember("code") && obj.isMember("message")) {
        return obj["code"].asInt();
    }
    if (obj.isMember("code") && obj.isMember("action")) {
        return std::stoi(obj["code"].asString());
    }
    return 0;
}

xunfei_speech_server_error::EngineErrorCode
xunfei_speech_server_error::asrErrorCode2speechResult(int errorCode) {
    if (asrErrorCodeMap.count(errorCode) > 0) {
        return asrErrorCodeMap.at(errorCode);
    }

    return {ai_engine::EngineErrorCategory::OutputError,
            ai_engine::SpeechEngineErrorCode::UnsupportedScript};
}

xunfei_speech_server_error::EngineErrorCode
xunfei_speech_server_error::realtimeAsrErrorCode2speechResult(int errorCode) {
    if (realtimeAsrErrorCodeMap.count(errorCode) > 0) {
        return realtimeAsrErrorCodeMap.at(errorCode);
    }

    return {ai_engine::EngineErrorCategory::OutputError,
            ai_engine::SpeechEngineErrorCode::UnsupportedScript};
}

xunfei_speech_server_error::EngineErrorCode
xunfei_speech_server_error::ttsErrorCode2speechResult(int errorCode) {
    if (ttsErrorCodeMap.count(errorCode) > 0) {
        return ttsErrorCodeMap.at(errorCode);
    }

    return {ai_engine::EngineErrorCategory::OutputError,
            ai_engine::SpeechEngineErrorCode::UnsupportedScript};
}
