/**
 * @file interact_wakeup.c
 * @brief 唤醒词交互模式：连续对话，仅唤醒词打断 TTS
 */

#include "interact/interact_wakeup.h"
#include "engine_internal.h"
#include "protocol/protocol_commands.h"
#include "protocol/agent_message.h"
#include "hal_websocket.h"
#include "hal_log.h"

#define TAG "WAKEUP"

static int wakeup_enter(engine_internal_ctx_t *ctx)
{
    BD_LOGI(TAG, "进入唤醒词模式");
    protocol_send_asr_enable_realtime(ctx);
    protocol_send_auto_int_disable(ctx);
    return 0;
}

static int wakeup_exit(engine_internal_ctx_t *ctx)
{
    BD_LOGI(TAG, "退出唤醒词模式");
    return 0;
}

/* 按键打断 */
static void wakeup_on_key_press(engine_internal_ctx_t *ctx)
{
    if (!ctx) {
        return;
    }

    BD_LOGI(TAG, "按键触发 (模拟唤醒)");

    if (ctx->state == AIAGENT_STATE_SPEAKING ||
        ctx->state == AIAGENT_STATE_PROCESSING) {
        protocol_send_break_begin(ctx, 1000);
    }
}

static void wakeup_on_key_release(engine_internal_ctx_t *ctx)
{
}

static void wakeup_on_key_long_press(engine_internal_ctx_t *ctx)
{
}

static void wakeup_on_tts_start(engine_internal_ctx_t *ctx)
{
    BD_LOGD(TAG, "TTS开始");
}

static void wakeup_on_tts_end(engine_internal_ctx_t *ctx)
{
    BD_LOGD(TAG, "TTS结束");
}

/* 唤醒词触发打断 */
static void wakeup_on_wakeword(engine_internal_ctx_t *ctx)
{
    if (!ctx) {
        return;
    }
    BD_LOGI(TAG, "唤醒词触发");
    protocol_send_break_begin(ctx, 1000);
}

const interact_mode_ops_t interact_wakeup_ops = {
    .name              = "Wakeup",
    .enter             = wakeup_enter,
    .exit              = wakeup_exit,
    .on_key_press      = wakeup_on_key_press,
    .on_key_release    = wakeup_on_key_release,
    .on_key_long_press = wakeup_on_key_long_press,
    .on_tts_start      = wakeup_on_tts_start,
    .on_tts_end        = wakeup_on_tts_end,
    .on_wakeword       = wakeup_on_wakeword,
};
