#include <stdio.h>
#include "baidu_chat_agents_engine.h"

static BaiduChatAgentEngine *g_engine = NULL;

// Callback implementations
void onErrorCallback(int errCode, const char* errMsg) {
    printf("Error occurred. Code: %d, Message: %s\n", errCode, errMsg);
}

void onCallStateChangeCallback(AGentCallState state) {
    printf("Call state changed: %d\n", state);
}

void onConnectionStateChangeCallback(AGentConnectState state) {
    printf("Connection state changed: %d\n", state);
}

void onUserAsrSubtitleCallback(const char* text, bool isFinal) {
    printf("User ASR subtitle: %s (isFinal: %d)\n", text, isFinal);
}
void onAIAgentSubtitleCallback(const char* text, bool isFinal) {
    // time_last_asr = timer_get_ms();
    printf("AIAgent ASR subtitle: %d %s (isFinal: %d)\n",timer_get_ms(), text, isFinal);
}

void onAIAgentSpeaking(bool speeking) {

}

void onFunctionCall(const char* id, const char* params) {
    printf("onFunctionCall, id: %s, params: %s\n", id, params);
}

void  onAudioPlayerOp(const char* path, bool start) {
    // 开启音频播放
    printf("开启或者关闭音频播放 %s  start:%d", path, start);
}


void onMediaSetup(void) {
    printf("Media setup completed.\n");
}

void onAudioData(const uint8_t* data, size_t len) {
    printf("Received audio data of length: %zu\n", len);
}

void onVideoData(const uint8_t* data, size_t len, int width, int height) {
    printf("Received video data of length: %zu, width: %d, height: %d\n", len, width, height);
}

void setUserParameters(AgentEngineParams *params) {
    strncpy(params->agent_platform_url, SERVER_HOST_ONLINE, sizeof(params->agent_platform_url) - 1);
    strncpy(params->appid, BDCloudDefaultRTCAppID, sizeof(params->appid) - 1);
    strncpy(params->cer, "./a.cer", sizeof(params->cer) - 1);
    strncpy(params->workflow, "VoiceChat", sizeof(params->workflow) - 1);
    params->instance_id = 10373;
    params->verbose = true;
    params->enable_internal_device =true;
    params->enable_local_agent =true;
    params->enable_voice_interrupt = true;
    params->level_voice_interrupt = 80;
    strncpy(params->llm, "LLMRacing", sizeof(params->llm) - 1);
    strncpy(params->lang, "zh", sizeof(params->lang) - 1);
    params->AudioInChannel = 1;
    params->AudioInFrequency = 16000;
}

void baidu_chat_agent_demo_open(void* arg) {
    BaiduChatAgentEvent events = {
        .onError = onErrorCallback,
        .onCallStateChange = onCallStateChangeCallback,
        .onConnectionStateChange = onConnectionStateChangeCallback,
        .onUserAsrSubtitle = onUserAsrSubtitleCallback,
        .onAIAgentSubtitle = onAIAgentSubtitleCallback,
        .onAIAgentSpeaking = onAIAgentSpeaking,
        .onFunctionCall = onFunctionCall,
        .onAudioPlayerOp  = onAudioPlayerOp,
        .onMediaSetup = onMediaSetup,
        .onAudioData = onAudioData,
        .onVideoData = onVideoData,
    };
    printf("baidu_chat_agent_demo_open BBBBBBBBBBBBBegin\n");

	struct AgentEngineParams* pAgentEngineParams = (struct AgentEngineParams*)arg;

    BaiduChatAgentEngine* engine = baidu_create_chat_agent_engine(&events);
    if (!engine) {
        printf("Engine initialization failed.FFFFFFFFFFFFFFFFFFFFFailed\n");
        return;
    }
    g_engine = engine; // 保存全局变量供后续使用

    AgentEngineParams agentParams;
    memset(&agentParams, 0, sizeof(agentParams));
    setUserParameters(&agentParams);

    if (pAgentEngineParams) {
		agentParams.enable_local_agent = pAgentEngineParams->enable_local_agent;
		snprintf(agentParams.appid, sizeof(agentParams.appid), pAgentEngineParams->appid);
        snprintf(agentParams.remote_params, sizeof(agentParams.remote_params), pAgentEngineParams->remote_params);
        free(pAgentEngineParams);
    }

    printf("baidu_chat_agent_demo_open EEEEEEEEnd local:%d\n", agentParams.enable_local_agent);
    int result = baidu_chat_agent_engine_init(engine, &agentParams);
    if (result != 200) {
        printf("Failed to log in. Error code: %d\n", result);
        baidu_chat_agent_engine_destroy(engine);
        g_engine = NULL;
        return;
    }

    baidu_chat_agent_engine_call(engine);
}

void baidu_chat_agent_demo_close(void) {
    printf("baidu_chat_agent_demo_close\n");
    if (g_engine) {
        baidu_chat_agent_engine_destroy(g_engine);
        g_engine = NULL;
    }
}
