#include "server_addr.h"
#include "log.h"
#include "config_engine.h"
#include "libuv_dbus.h"

const char* g_ServerModuleStr[] = 
{
    "YUNXIN_MODULE",
    "VOICE_MODULE",
    "VOICE_AI_MODULE",
    "SERVER_MODULE",
    "LOG_MODULE",
    "MARK_POINT_MODULE",
    "TTS_MODULE",
    "DC_MODULE",
    "UNKNOWN_MODULE"
};

const char* g_ServerModeStr[] = 
{
    "DEV_MODE",
    "TEST_MODE",
    "PUBLISH_MODE",
    "PUBLISH_PREBUILD",
    "UNKNOWN_MODE"
};

const char* g_KeyMapStr[] =
{
    "VOICE_APP_KEY",
    "VOICE_APP_SECRET",
    "UNKNOWN_KEYMAP"
};

const char* SvrModuleStr(SERVER_MODULE_TYPE module)
{
    return g_ServerModuleStr[module];
}

const char* SvrModeStr(SERVER_MODE_TYPE mode)
{
    return g_ServerModeStr[mode];
}

const char* g_VoiceKeyMap[VOICE_MAX][MAX_MODE] = {
    {   // VOICE_APP_KEY
        "vbox-dev",
        "vbox-dev",
        "vbox-online",
        "vbox-online",
    },
    {   // VOICE_APP_SECRET
        "b1ec33c03df80ea3035bc9ccaa4af09c",
        "b1ec33c03df80ea3035bc9ccaa4af09c",
        "8714d6de1c83f21dda5fc9a905a59ac1",
        "8714d6de1c83f21dda5fc9a905a59ac1",
    },
};

const char* g_ServerAddr[MAX_MODULE][MAX_MODE] = {
    {   // YUNXIN_MODULE
        "2e37bc56a9b7ec3f6b8f41f60b81eb92",
        "2e37bc56a9b7ec3f6b8f41f60b81eb92",
        "dbb00213c23ea3709aae12ceb4c4e54e",
        "7fc939cdb26ec2fa343b7c47a0617190",
    },
    {   // VOICE_MODULE
        "ws://vbox-test.netease.com/netty/websocket",
        "ws://vbox-test.netease.com/netty3/websocket",
        "wss://vbox-asr.3.163.com/websocket",
        "ws://vbox-test.netease.com/asr/websocket",
    },
    {   // VOICE_AI_MODULE
        "http://api.multimedia.netease.com/imgtest/yqbot31_8686/",
        "http://api.multimedia.netease.com/imgtest/yqbot29_8686/",
        "https://vbox-smart.3.163.com/ ",
        "http://api.multimedia.netease.com/imgtest/yqbot30_8888/",
    },
    {   // SERVER_MODULE
        "http://api.multimedia.netease.com/imgtest/yqbot27_7677/",
        "http://api.multimedia.netease.com/imgtest/yqbot31_7676/",
        "https://vbox-server.3.163.com/",
        "http://api.multimedia.netease.com/imgtest/yqbot22_7678/",
    },
    {   // LOG_MODULE
        "http://vbox-log-test.netease.com/logCollectDev/vbox/logCollect/uploadLog",
        "http://vbox-log-test.netease.com/logCollect/vbox/logCollect/uploadLog",
        "https://vbox-log.3.163.com/vbox/logCollect/uploadLog",
        "http://vbox-log-test.netease.com/logCollectOnline/vbox/logCollect/uploadLog",
    },
    {   // MARK_POINT_MODULE
        "http://vbox-log-test.netease.com/buriedDev/vbox/log/add",
        "http://vbox-log-test.netease.com/buriedTest/vbox/log/add",
        "https://vbox-log.3.163.com/vbox/log/add",
        "http://vbox-log-test.netease.com/buriedOnline/vbox/log/add",
    },
    {   // TTS_MODULE
        "http://api.openai.netease.com/vbox-tts-dev/vbox/tts/transform",
        "http://api.openai.netease.com/vbox-tts-test/vbox/tts/transform",
        "https://vbox-tts.3.163.com/vbox/tts/transform",
        "http://api.openai.netease.com/vbox-tts-online/vbox/tts/transform",
    },
    {   // DC_MODULE
        "http://api.multimedia.netease.com/imgtest/yqbot27_7677/vbox/uploadFile",
        "http://api.multimedia.netease.com/imgtest/yqbot31_7676/vbox/uploadFile",
        "https://vbox-server.3.163.com/vbox/uploadFile",
        "http://api.multimedia.netease.com/imgtest/yqbot22_7678/vbox/uploadFile",
    },
};

static unsigned int g_SvrMode = PUBLISH_MODE;

void DumpCurServerAddr(const char* pTags)
{
    if(pTags && strlen(pTags) > 0)
    {
        LOG_EX2(LOG_Info, "%s\t Current Server Mode: %s\n", pTags, SvrModeStr(g_SvrMode));
    }
    else
    {
        LOG_EX2(LOG_Info, "Current Server Mode: %s\n", SvrModeStr(g_SvrMode));
    }

#if 0
    LOG_EX2(LOG_Info, "Voice Key = [%s], Secret = [%s]\n", 
            GetCurVoiceKeyValue(VOICE_APP_KEY), GetCurVoiceKeyValue(VOICE_APP_SECRET));

    LOG_EX2(LOG_Info, "--------------------------------------------------------"
            "----------------------------------------------\n");

    LOG_EX2(LOG_Info, "|      Module       |                                   "
                       "Server URL                                   |\n");
    
    LOG_EX2(LOG_Info, "--------------------------------------------------------"
            "----------------------------------------------\n");

    for(int i = 0; i < MAX_MODULE; i++)
    {
        LOG_EX2(LOG_Info, "| %-17s | %-78s |\n", SvrModuleStr(i), GetCurServerAddr(i));
    }

    LOG_EX2(LOG_Info, "--------------------------------------------------------"
            "----------------------------------------------\n");
#endif

}

SERVER_MODE_TYPE GetCurrentServerMode(void)
{
    return g_SvrMode;
}

void SetCurrentServerMode(SERVER_MODE_TYPE mode)
{
    if(mode >= 0 && mode < MAX_MODE) 
    {
        if(g_SvrMode != mode)
        {
            LOG_EX(LOG_Debug, "Change server mode from %s(%d) to %s(%d)\n",
                   g_ServerModeStr[g_SvrMode], g_SvrMode, g_ServerModeStr[mode], mode);

            g_SvrMode = mode;

            CfgSetIntValue("ServerMode", mode);
        }
        else
        {
            LOG_EX(LOG_Warn, "Current mode is %s(%d) yet\n", g_ServerModeStr[mode], mode);
        }
    }
    else
    {
        LOG_EX(LOG_Error, "Unknown Mode: %d, current mode: %s(%d)\n", 
               mode, g_ServerModeStr[g_SvrMode], g_SvrMode);
    }
}

char* GetCurServerAddr(SERVER_MODULE_TYPE module)
{
    if(module >= 0 && module < MAX_MODULE)
    {
        return (char*)g_ServerAddr[module][g_SvrMode];
    }
    else
    {
        LOG_EX(LOG_Error, "Unknown Module: %s(%d)\n", g_ServerModuleStr[module], module);
        return "";
    }
}

char* GetCurVoiceKeyValue(VOICE_KEYMAP_TYPE keyMap)
{
    if(keyMap >= 0 && keyMap < VOICE_MAX)
    {
        return (char*)g_VoiceKeyMap[keyMap][g_SvrMode];
    }
    else
    {
        LOG_EX(LOG_Error, "Unknown KeyMap Type: %s(%d)\n", g_KeyMapStr[keyMap], keyMap);
        return "";
    }
}

static PDBUS_MSG_PACK __dusOnMsg(uv_loop_t* pLoop, DBusConnection* pConn, PDBUS_MSG_PACK pMsg)
{
    return NULL;
}

static void uvLoopProc(void *pParams)
{
    RunUVLoop(GetDBusDefaultLoop());
    pthread_detach(pthread_self());
}

void ServerManagerInit(void)
{
    int ret = 0;
    uv_thread_t uvThread;
    LOG_EX(LOG_Warn, "+++++++++++++++++++++++System Uninit\n");

    DBusConnection* pBus = DBusWithLibuvInit(GetDBusDefaultLoop(), 
                                                g_pModInfoTable[MODULE_SKINS].modAliase, 
                                             __dusOnMsg, 
                                             NULL, 
                                             NULL, //KeyEventCb, 
                                             &ret);

    if(pBus == NULL)
    {
        LOG_EX(LOG_Error, "DBusWithLibuvInit Error: %d\n", ret);
        return;
    }   

    uv_thread_create(&uvThread, uvLoopProc, NULL);
}
