#include "voicechatspeechcontrolproxy.h"
#include <QMetaObject>
#include <ukui-log4qt.h>

VoiceChatSpeechControlProxy::VoiceChatSpeechControlProxy(VoiceChatSpeech *voiceChatSpeech, QObject *parent)
    : QObject(parent)
    , m_voiceChatSpeech(voiceChatSpeech)
{
    initSignalForwarding();
}

VoiceChatSpeechControlProxy::~VoiceChatSpeechControlProxy()
{
    // 代理对象不需要清理 VoiceChatSpeech，它由外部管理
}

void VoiceChatSpeechControlProxy::setVoiceChatSpeech(VoiceChatSpeech *voiceChatSpeech)
{
    if (m_voiceChatSpeech == voiceChatSpeech) {
        return;
    }

    // 断开旧对象的信号连接
    if (m_voiceChatSpeech) {
        disconnect(m_voiceChatSpeech, nullptr, this, nullptr);
    }

    m_voiceChatSpeech = voiceChatSpeech;
    
    // 重新连接信号
    initSignalForwarding();
}

void VoiceChatSpeechControlProxy::initSignalForwarding()
{
    if (!m_voiceChatSpeech) {
        return;
    }

    // 转发所有信号
    connect(m_voiceChatSpeech, &VoiceChatSpeech::sigReceiveSpeechRecognitionMessage, 
            this, &VoiceChatSpeechControlProxy::sigReceiveSpeechRecognitionMessage);
    connect(m_voiceChatSpeech, &VoiceChatSpeech::sigErrorMsg, 
            this, &VoiceChatSpeechControlProxy::sigErrorMsg);
    connect(m_voiceChatSpeech, &VoiceChatSpeech::sigCallbackErrorMsg, 
            this, &VoiceChatSpeechControlProxy::sigCallbackErrorMsg);
    connect(m_voiceChatSpeech, &VoiceChatSpeech::sigNoModel, 
            this, &VoiceChatSpeechControlProxy::sigNoModel);
    connect(m_voiceChatSpeech, &VoiceChatSpeech::sigCurSpeechVolume, 
            this, &VoiceChatSpeechControlProxy::sigCurSpeechVolume);
    connect(m_voiceChatSpeech, &VoiceChatSpeech::speechRecognitionStarted, 
            this, &VoiceChatSpeechControlProxy::speechRecognitionStarted);
}

bool VoiceChatSpeechControlProxy::initSpeechSession()
{
    if (!m_voiceChatSpeech) {
        KyInfo() << "VoiceChatSpeechControlProxy: VoiceChatSpeech is null";
        return false;
    }

    // 使用 QMetaObject::invokeMethod 异步调用
    bool result = QMetaObject::invokeMethod(m_voiceChatSpeech, 
                                            "initSpeechSession",
                                            Qt::QueuedConnection);
    if (!result) {
        KyInfo() << "VoiceChatSpeechControlProxy: Failed to invoke initSpeechSession";
    }
    return result;
}

void VoiceChatSpeechControlProxy::closeSpeechSession()
{
    if (!m_voiceChatSpeech) {
        KyInfo() << "VoiceChatSpeechControlProxy: VoiceChatSpeech is null";
        return;
    }

    bool result = QMetaObject::invokeMethod(m_voiceChatSpeech, 
                                            "closeSpeechSession",
                                            Qt::QueuedConnection);
    if (!result) {
        KyInfo() << "VoiceChatSpeechControlProxy: Failed to invoke closeSpeechSession";
    }
}

bool VoiceChatSpeechControlProxy::startSpeechRecognition()
{
    if (!m_voiceChatSpeech) {
        KyInfo() << "VoiceChatSpeechControlProxy: VoiceChatSpeech is null";
        return false;
    }

    bool result = QMetaObject::invokeMethod(m_voiceChatSpeech, 
                                            "startSpeechRecognition",
                                            Qt::QueuedConnection);
    if (!result) {
        KyInfo() << "VoiceChatSpeechControlProxy: Failed to invoke startSpeechRecognition";
    }
    return result;
}

void VoiceChatSpeechControlProxy::pauseSpeechRecognition()
{
    if (!m_voiceChatSpeech) {
        KyInfo() << "VoiceChatSpeechControlProxy: VoiceChatSpeech is null";
        return;
    }

    bool result = QMetaObject::invokeMethod(m_voiceChatSpeech, 
                                            "pauseSpeechRecognition",
                                            Qt::QueuedConnection);
    if (!result) {
        KyInfo() << "VoiceChatSpeechControlProxy: Failed to invoke pauseSpeechRecognition";
    }
}

void VoiceChatSpeechControlProxy::continueSpeechRecognition()
{
    if (!m_voiceChatSpeech) {
        KyInfo() << "VoiceChatSpeechControlProxy: VoiceChatSpeech is null";
        return;
    }

    bool result = QMetaObject::invokeMethod(m_voiceChatSpeech, 
                                            "continueSpeechRecognition",
                                            Qt::QueuedConnection);
    if (!result) {
        KyInfo() << "VoiceChatSpeechControlProxy: Failed to invoke continueSpeechRecognition";
    }
}

void VoiceChatSpeechControlProxy::stopSpeechRecognition()
{
    if (!m_voiceChatSpeech) {
        KyInfo() << "VoiceChatSpeechControlProxy: VoiceChatSpeech is null";
        return;
    }

    bool result = QMetaObject::invokeMethod(m_voiceChatSpeech, 
                                            "stopSpeechRecognition",
                                            Qt::QueuedConnection);
    if (!result) {
        KyInfo() << "VoiceChatSpeechControlProxy: Failed to invoke stopSpeechRecognition";
    }
}

