#include "displayeffectdbusinterface.h"
#include <QDebug>
#include <QDBusMetaType>
#include <QApplication>

namespace {
const char *wlcom_dbus_service = "com.kylin.Wlcom";
const char *effect_debus_path = "/com/kylin/Wlcom/Effect";
const char *effect_dbus_interface = "com.kylin.Wlcom.Effect";
}

// 重载操作符以便D-Bus可以序列化/反序列化这个结构体
QDBusArgument &operator<<(QDBusArgument &argument, const EffectOption &effect) {
    argument.beginStructure();
    argument << effect.name << effect.value << effect.enabled;
    argument.endStructure();
    return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, EffectOption &effect) {
    argument.beginStructure();
    argument >> effect.name >> effect.value >> effect.enabled;
    argument.endStructure();
    return argument;
}

EffectDbusInterface::EffectDbusInterface()
    : dbusInterface(wlcom_dbus_service, effect_debus_path, effect_dbus_interface, QDBusConnection::sessionBus())
{
    qDebug() << "EffectDbusInterface::EffectDbusInterface()";
    registerMetaType();
    initActiveEffects();
}

void EffectDbusInterface::initActiveEffects() 
{
     // 调用ListAllEffects方法（无参数）
    qDebug() << "EffectDbusInterface::initActiveEffects()";
    QDBusReply<QList<EffectOption>> reply = dbusInterface.call("ListAllEffects");
    
    // 检查调用是否成功
    if (!reply.isValid()) {
        qDebug() << "D-Bus调用失败:" << reply.error().message();
        return;
    }
    
    // 获取返回的效果列表
    m_enabledEffects = reply.value();
    qDebug() << "m_enabledEffects size: " << m_enabledEffects.size();
    m_enabledEffects.erase(std::remove_if(m_enabledEffects.begin(), m_enabledEffects.end(), 
    [](EffectOption effect) { return effect.enabled == false; }), 
    m_enabledEffects.end());

    // 解析并显示每个效果的信息
    for (int i = 0; i < m_enabledEffects.size(); ++i) {
        const EffectOption &effect = m_enabledEffects.at(i);
        qDebug() << "effect" << i + 1 << ":";
        qDebug() << "  name:" << effect.name;
        qDebug() << "  value:" << effect.value;
        qDebug() << "  enabled:" << (effect.enabled ? "是" : "否");
        qDebug() << "----------------------------------------";
    }
    
}

void EffectDbusInterface::changeEffectEffects(bool status)
{
    qDebug() << QString("EffectDbusInterface::changeEffectEffects status %1").arg(status ? "true" : "false");
    for (const auto &effect : m_enabledEffects) {
        enableEffect(effect.name, status);
    }
}

void EffectDbusInterface::registerMetaType()
{
    qDebug() << "EffectDbusInterface::registerMetaType()";
    qRegisterMetaType<EffectOption>("EffectOption");
    qRegisterMetaType<EffectOptionList>("EffectOptionList");
    qDBusRegisterMetaType<EffectOption>();
    qDBusRegisterMetaType<EffectOptionList>();
}

void EffectDbusInterface::printEffectStatus()
{
    qDebug() << "EffectDbusInterface::printEffectStatus()";
    for (const auto &effect : m_enabledEffects) {
        qDebug() << QString("effect name: %1, value: %2, enabled %3").arg(effect.name).arg(effect.value).arg(effect.enabled ? "true" : "false");
        qDebug() << "************************";
    }
}

bool EffectDbusInterface::enableEffect(QString effect, bool status)
{
    qDebug() << QString("EffectDbusInterface::enableEffect effect: %1, status: %2").arg(effect).arg(status ? "true" : "false");
     QDBusReply<QVariant> reply = dbusInterface.call("EnableEffect", effect, status);
    if (!reply.isValid()) {
        qDebug() << "enableEffect failed, " << reply.error();
        return false;
    }
    return true;
}
