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

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";

const char *maintain_mode_dbus_service = "com.kylin.MaintainMode";
const char *maintain_mode_dbus_path = "/com/kylin/MaintainMode/Object";
const char *maintain_mode_interface = "com.kylin.MaintainMode.interface";
}


// 重载操作符以便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()
    : m_dbusInterface(wlcom_dbus_service, effect_debus_path, effect_dbus_interface, QDBusConnection::sessionBus())
    , m_maintainModeDbusInterface(maintain_mode_dbus_service, maintain_mode_dbus_path, maintain_mode_interface, QDBusConnection::systemBus())
    , m_effectBootSettings("kylin-process-manager", "effect_manage")
    {
    qDebug() << "EffectDbusInterface::EffectDbusInterface()";
    registerMetaType();
    restoreEffectsStatus();
    //initActiveEffects();
}

void EffectDbusInterface::initActiveEffects() 
{
     // 调用ListAllEffects方法（无参数）
    qDebug() << "EffectDbusInterface::initActiveEffects()";
    QDBusReply<QList<EffectOption>> reply = m_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::suspendEffectEffects(std::vector<std::string> &filterList)
{
    initActiveEffects();
    for (const auto &effect : m_enabledEffects) {
        auto it = std::find(filterList.begin(), filterList.end(), effect.name.toStdString());
        if(it != filterList.end()) continue;
        enableEffect(effect.name, false);
        m_effectBootSettings.setValue(effect.name, false);
    }
    m_effectBootSettings.sync();
}

void EffectDbusInterface::resumeEffectEffects()
{
    restoreEffectsStatus();
}

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() << "************************";
    }
}

QString EffectDbusInterface::getCurrentMaintainMode()
{
    qDebug() << "EffectDbusInterface::getCurrentMaintainMode()";
    QDBusReply<QString> reply = m_maintainModeDbusInterface.call("GetBootMode");
    if (!reply.isValid()) {
        qDebug() << "getCurrentMaintainMode() failed, " << reply.error();
        return QString();
    }
    qDebug() << QString("current maintain mode is %1").arg(reply.value());
    return reply.value();
}

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

void EffectDbusInterface::restoreEffectsStatus()
{
    const QStringList keys = m_effectBootSettings.allKeys();
    for(const QString &k : keys) {
        if(!m_effectBootSettings.value(k).toBool()) {
            enableEffect(k, true);
        }
        m_effectBootSettings.remove(k);
    }
    m_effectBootSettings.sync();
}
