/**
 * @file text_input.c
 * @brief 文本输入处理实现
 */

#include "input/text_input.h"
#include "engine_internal.h"
#include "protocol/agent_message.h"
#include "connection/connection_manager.h"
#include "hal_log.h"
#include <string.h>

#define TAG "TXT"

int text_input_send(engine_internal_ctx_t *ctx, const char *text)
{
    if (!ctx || !text || !text[0]) {
        return -1;
    }

    if (!conn_mgr_is_connected(ctx)) {
        BD_LOGW(TAG, "ws not connected");
        return -1;
    }

    char msg_buf[512];
    int n = agent_msg_create_text(text, msg_buf, sizeof(msg_buf));
    if (n <= 0) {
        BD_LOGE(TAG, "create text msg fail");
        return -1;
    }

    int ret = conn_mgr_send_text(ctx, msg_buf);
    if (ret < 0) {
        BD_LOGE(TAG, "send text fail");
        return -1;
    }

    BD_LOGI(TAG, "text sent: %s", text);
    return 0;
}

int text_input_send_tts(engine_internal_ctx_t *ctx, const char *text)
{
    if (!ctx || !text || !text[0]) {
        return -1;
    }

    if (!conn_mgr_is_connected(ctx)) {
        return -1;
    }

    char msg_buf[512];
    int n = agent_msg_create_tts(text, msg_buf, sizeof(msg_buf));
    if (n <= 0) {
        return -1;
    }

    return conn_mgr_send_text(ctx, msg_buf);
}
