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

VoiceChatReaderControlProxy::VoiceChatReaderControlProxy(VoiceChatReader *voiceChatReader, QObject *parent)
    : QObject(parent)
    , m_voiceChatReader(voiceChatReader)
{
    initSignalForwarding();
}

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

void VoiceChatReaderControlProxy::setVoiceChatReader(VoiceChatReader *voiceChatReader)
{
    if (m_voiceChatReader == voiceChatReader) {
        return;
    }

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

    m_voiceChatReader = voiceChatReader;
    
    // 重新连接信号
    initSignalForwarding();
}

void VoiceChatReaderControlProxy::initSignalForwarding()
{
    if (!m_voiceChatReader) {
        return;
    }

    // 转发所有信号
    connect(m_voiceChatReader, &VoiceChatReader::sigPlayingStart, 
            this, &VoiceChatReaderControlProxy::sigPlayingStart);
    connect(m_voiceChatReader, &VoiceChatReader::sigReadEnd, 
            this, &VoiceChatReaderControlProxy::sigReadEnd);
    connect(m_voiceChatReader, &VoiceChatReader::reply, 
            this, &VoiceChatReaderControlProxy::reply);
    connect(m_voiceChatReader, &VoiceChatReader::sigResetPreIndex, 
            this, &VoiceChatReaderControlProxy::sigResetPreIndex);
    connect(m_voiceChatReader, &VoiceChatReader::sigReceiveReaderMessage, 
            this, &VoiceChatReaderControlProxy::sigReceiveReaderMessage);
}

bool VoiceChatReaderControlProxy::initReaderSession()
{
    if (!m_voiceChatReader) {
        KyInfo() << "VoiceChatReaderControlProxy: VoiceChatReader is null";
        return false;
    }

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

bool VoiceChatReaderControlProxy::closeReaderSession()
{
    if (!m_voiceChatReader) {
        KyInfo() << "VoiceChatReaderControlProxy: VoiceChatReader is null";
        return false;
    }

    bool result = QMetaObject::invokeMethod(m_voiceChatReader, 
                                            "closeReaderSession",
                                            Qt::QueuedConnection);
    if (!result) {
        KyInfo() << "VoiceChatReaderControlProxy: Failed to invoke closeReaderSession";
    }
    return result;
}

void VoiceChatReaderControlProxy::startReading()
{
    if (!m_voiceChatReader) {
        KyInfo() << "VoiceChatReaderControlProxy: VoiceChatReader is null";
        return;
    }

    bool result = QMetaObject::invokeMethod(m_voiceChatReader, 
                                            "startReading",
                                            Qt::QueuedConnection);
    if (!result) {
        KyInfo() << "VoiceChatReaderControlProxy: Failed to invoke startReading";
    }
}

void VoiceChatReaderControlProxy::pauseReading()
{
    if (!m_voiceChatReader) {
        KyInfo() << "VoiceChatReaderControlProxy: VoiceChatReader is null";
        return;
    }

    bool result = QMetaObject::invokeMethod(m_voiceChatReader, 
                                            "pauseReading",
                                            Qt::QueuedConnection);
    if (!result) {
        KyInfo() << "VoiceChatReaderControlProxy: Failed to invoke pauseReading";
    }
}

void VoiceChatReaderControlProxy::stopReading()
{
    if (!m_voiceChatReader) {
        KyInfo() << "VoiceChatReaderControlProxy: VoiceChatReader is null";
        return;
    }

    bool result = QMetaObject::invokeMethod(m_voiceChatReader, 
                                            "stopReading",
                                            Qt::QueuedConnection);
    if (!result) {
        KyInfo() << "VoiceChatReaderControlProxy: Failed to invoke stopReading";
    }
}

void VoiceChatReaderControlProxy::continueReading()
{
    if (!m_voiceChatReader) {
        KyInfo() << "VoiceChatReaderControlProxy: VoiceChatReader is null";
        return;
    }

    bool result = QMetaObject::invokeMethod(m_voiceChatReader, 
                                            "continueReading",
                                            Qt::QueuedConnection);
    if (!result) {
        KyInfo() << "VoiceChatReaderControlProxy: Failed to invoke continueReading";
    }
}

void VoiceChatReaderControlProxy::interruptReading()
{
    if (!m_voiceChatReader) {
        KyInfo() << "VoiceChatReaderControlProxy: VoiceChatReader is null";
        return;
    }

    bool result = QMetaObject::invokeMethod(m_voiceChatReader, 
                                            "interruptReading",
                                            Qt::QueuedConnection);
    if (!result) {
        KyInfo() << "VoiceChatReaderControlProxy: Failed to invoke interruptReading";
    }
}

bool VoiceChatReaderControlProxy::getCurrentReaderStatus()
{
    if (!m_voiceChatReader) {
        KyInfo() << "VoiceChatReaderControlProxy: VoiceChatReader is null";
        return false;
    }

    // 注意：对于需要返回值的调用，如果使用 QueuedConnection，返回值无法获取
    // 这里使用 BlockingQueuedConnection 会阻塞，但可以获取返回值
    bool result = false;
    QMetaObject::invokeMethod(m_voiceChatReader, 
                              "getCurrentReaderStatus",
                              Qt::BlockingQueuedConnection,
                              Q_RETURN_ARG(bool, result));
    return result;
}

bool VoiceChatReaderControlProxy::sendReadingText(const QString &msg, int index)
{
    if (!m_voiceChatReader) {
        KyInfo() << "VoiceChatReaderControlProxy: VoiceChatReader is null";
        return false;
    }

    bool result = QMetaObject::invokeMethod(m_voiceChatReader, 
                                            "sendReadingText",
                                            Qt::QueuedConnection,
                                            Q_ARG(const QString, msg),
                                            Q_ARG(int, index));
    if (!result) {
        KyInfo() << "VoiceChatReaderControlProxy: Failed to invoke sendReadingText";
    }
    return result;
}

bool VoiceChatReaderControlProxy::continueAddReadingText(const QString &msg, int index)
{
    if (!m_voiceChatReader) {
        KyInfo() << "VoiceChatReaderControlProxy: VoiceChatReader is null";
        return false;
    }

    bool result = QMetaObject::invokeMethod(m_voiceChatReader, 
                                            "continueAddReadingText",
                                            Qt::QueuedConnection,
                                            Q_ARG(const QString, msg),
                                            Q_ARG(int, index));
    if (!result) {
        KyInfo() << "VoiceChatReaderControlProxy: Failed to invoke continueAddReadingText";
    }
    return result;
}

int VoiceChatReaderControlProxy::getCurrentReadIndex()
{
    if (!m_voiceChatReader) {
        KyInfo() << "VoiceChatReaderControlProxy: VoiceChatReader is null";
        return -1;
    }

    // 需要返回值，使用 BlockingQueuedConnection
    int result = -1;
    QMetaObject::invokeMethod(m_voiceChatReader, 
                              "getCurrentReadIndex",
                              Qt::BlockingQueuedConnection,
                              Q_RETURN_ARG(int, result));
    return result;
}

bool VoiceChatReaderControlProxy::insertWaitingReadingListItem(const QString &msg)
{
    if (!m_voiceChatReader) {
        KyInfo() << "VoiceChatReaderControlProxy: VoiceChatReader is null";
        return false;
    }

    bool result = QMetaObject::invokeMethod(m_voiceChatReader, 
                                            "insertWaitingReadingListItem",
                                            Qt::QueuedConnection,
                                            Q_ARG(const QString, msg));
    if (!result) {
        KyInfo() << "VoiceChatReaderControlProxy: Failed to invoke insertWaitingReadingListItem";
    }
    return result;
}

void VoiceChatReaderControlProxy::clearWaitingReadingList()
{
    if (!m_voiceChatReader) {
        KyInfo() << "VoiceChatReaderControlProxy: VoiceChatReader is null";
        return;
    }

    bool result = QMetaObject::invokeMethod(m_voiceChatReader, 
                                            "clearWaitingReadingList",
                                            Qt::QueuedConnection);
    if (!result) {
        KyInfo() << "VoiceChatReaderControlProxy: Failed to invoke clearWaitingReadingList";
    }
}

void VoiceChatReaderControlProxy::updateCurReadIndex(const int &index)
{
    if (!m_voiceChatReader) {
        KyInfo() << "VoiceChatReaderControlProxy: VoiceChatReader is null";
        return;
    }

    bool result = QMetaObject::invokeMethod(m_voiceChatReader, 
                                            "updateCurReadIndex",
                                            Qt::QueuedConnection,
                                            Q_ARG(const int, index));
    if (!result) {
        KyInfo() << "VoiceChatReaderControlProxy: Failed to invoke updateCurReadIndex";
    }
}

