/**
 * @file interact_continuous.c
 * @brief 连续对话交互模式实现
 */

#include "interact/interact_continuous.h"
#include "engine_internal.h"
#include "protocol/protocol_commands.h"
#include "protocol/agent_message.h"
#include "connection/connection_manager.h"
#include "hal_log.h"

#define TAG "CONT"

/**
 * @brief 进入连续对话模式
 *
 * 发送协议命令通知服务端进入连续对话 + 开启自动打断
 */
static int continuous_enter(engine_internal_ctx_t *ctx)
{
    BD_LOGI(TAG, "进入连续对话模式");

    /* 通知服务端进入实时 ASR 模式 */
    protocol_send_asr_enable_realtime(ctx);

    /* 开启自动打断 */
    protocol_send_auto_int_enable(ctx);

    return 0;
}

/**
 * @brief 退出连续对话模式
 */
static int continuous_exit(engine_internal_ctx_t *ctx)
{
    BD_LOGI(TAG, "退出连续对话模式");
    return 0;
}

/**
 * @brief 按键按下 -> 手动打断正在播放的TTS
 */
static void continuous_on_key_press(engine_internal_ctx_t *ctx)
{
    if (!ctx) {
        return;
    }

    /* 连续对话模式下按键用于手动打断 */
    if (ctx->state == AIAGENT_STATE_SPEAKING ||
        ctx->state == AIAGENT_STATE_PROCESSING) {
        char msg_buf[16];
        agent_msg_create_break(msg_buf, sizeof(msg_buf));
        conn_mgr_send_text(ctx, msg_buf);
    }
}

/**
 * @brief 按键松开 - 连续对话模式下无特殊处理
 */
static void continuous_on_key_release(engine_internal_ctx_t *ctx)
{
}

/**
 * @brief 长按 - 连续对话模式下无特殊处理
 */
static void continuous_on_key_long_press(engine_internal_ctx_t *ctx)
{
}

/**
 * @brief TTS开始播放 -（依赖服务端AEC）
 */
static void continuous_on_tts_start(engine_internal_ctx_t *ctx)
{
    BD_LOGD(TAG, "TTS开始 ");
}

/**
 * @brief TTS播放结束
 */
static void continuous_on_tts_end(engine_internal_ctx_t *ctx)
{
    BD_LOGD(TAG, "TTS结束 ");
}

/* 连续对话模式操作表 */
const interact_mode_ops_t interact_continuous_ops = {
    .name              = "Continuous",
    .enter             = continuous_enter,
    .exit              = continuous_exit,
    .on_key_press      = continuous_on_key_press,
    .on_key_release    = continuous_on_key_release,
    .on_key_long_press = continuous_on_key_long_press,
    .on_tts_start      = continuous_on_tts_start,
    .on_tts_end        = continuous_on_tts_end,
    .on_wakeword       = NULL,
};
