#include "globalconfig.h"
#include <QDBusReply>
#include <QDBusMessage>
#include <QDBusMetaType>

#include <QDBusPendingReply>
#include <QDBusConnection>
#include <QDBusError>
#include <QDebug>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonDocument>
#include "common.h"

#define DBUS_GC_NAME    "org.ukui.SettingsDaemon"
#define DBUS_GC_SIGNAL_PATH    "/GlobalSignal"
#define DBUS_GC_SIGNAL "org.ukui.SettingsDaemon.GlobalSignal"

#define DBUS_GC_INTERFACE_PATH "/globalconfig"
#define DBUS_GC_INTERFACE "com.settings.daemon.interface"

/*com.kylin.ukui.SettingsDaemon*/
#define SETTINGS_DAEMON_SYSTEMDBUS_NAME            "com.kylin.ukui.SettingsDaemon"
#define SETTINGS_DAEMON_SYSTEMDBUS_PATH            "/globalconfig"
#define SETTINGS_DAEMON_SYSTEMDBUS_INTERFACE       "com.kylin.ukui.SettingsDaemon.interface"


#define DBUS_POWER_MANAGER_SETTINGS "powerManager"
#define DBUS_POWER_FULL_CONFIG   "config"


GlobalConfig::GlobalConfig(QObject *parent) : QObject(parent)
{

}

GlobalConfig::~GlobalConfig()
{
    if(m_isInit==true){
        delete m_globalBus;
        m_globalBus = nullptr;
        m_isInit = false;
    }

}

void GlobalConfig::initGlobalConfig()
{
    if(m_isInit==true){
        return;
    }

    m_globalBus = new  QDBusInterface(DBUS_GC_NAME, DBUS_GC_SIGNAL_PATH, DBUS_GC_SIGNAL, QDBusConnection::sessionBus());
    QDBusInterface::connect(m_globalBus, SIGNAL(Active(bool)), this, SLOT(userActiveSlot(bool)));
    m_isInit = true;
    qDebug() << "connect success!!!!!!!!!!!!!!!!!!!!" << m_globalBus;
}


void GlobalConfig::userActiveSlot(bool isLogin)
{
    qDebug() << "GlobalConfig user active " << isLogin;
    if(isLogin == 1){
        Q_EMIT(userActiveSignal(true));
    }else{
        Q_EMIT(userActiveSignal(false));
    }
}



bool GlobalConfig::writeGlobalParam(const QString & key, const QVariant &value)
{
    QDBusInterface globalManager(SETTINGS_DAEMON_SYSTEMDBUS_NAME, \
                          SETTINGS_DAEMON_SYSTEMDBUS_PATH, \
                          SETTINGS_DAEMON_SYSTEMDBUS_INTERFACE, \
                          QDBusConnection::systemBus());
    QList<QVariant> arguments;

    arguments<<QVariant(QString::fromLatin1(DBUS_POWER_MANAGER_SETTINGS))<<QVariant(key)<< QVariant::fromValue(QDBusVariant(value));
    QDBusPendingCall reply  = globalManager.asyncCallWithArgumentList("setGlobalConf", arguments);
    qDebug() << __func__ << arguments;
    qDebug() << __func__ << value;
    if (reply.isError()) {
        qDebug() <<"writeGlobalParam:"<< reply.error();
        return false;
    }
    qDebug() <<"writeGlobalParam:"<< key<<": "<<value;
    return true;
}

bool GlobalConfig::readGlobalParam(const QString & key, QVariant &data)
{
    QDBusInterface globalManager(SETTINGS_DAEMON_SYSTEMDBUS_NAME, \
                          SETTINGS_DAEMON_SYSTEMDBUS_PATH, \
                          SETTINGS_DAEMON_SYSTEMDBUS_INTERFACE, \
                          QDBusConnection::systemBus());

    QDBusReply<QVariant> reply = globalManager.call("getGlobalConf", DBUS_POWER_MANAGER_SETTINGS, key);

    if (reply.error().type() == QDBusError::ErrorType::NoError) {
        data = reply.value();
        return true;
    } else {
        qDebug() << "readPowerConf:"<<reply.error();
    }
    return false;
}

bool GlobalConfig::readAllGlobalParam(QVariantMap &oMap)
{
    QDBusInterface globalManager(SETTINGS_DAEMON_SYSTEMDBUS_NAME, \
                          SETTINGS_DAEMON_SYSTEMDBUS_PATH, \
                          SETTINGS_DAEMON_SYSTEMDBUS_INTERFACE, \
                          QDBusConnection::systemBus());

    QDBusReply<QVariantMap> reply = globalManager.call("getSchemaGlobalConf", DBUS_POWER_MANAGER_SETTINGS);

    if (reply.error().type() == QDBusError::ErrorType::NoError) {
        oMap= reply.value();
        return oMap.contains(BRIGHTNESS_AC);
    } else {
        qDebug() << "readAllGlobalParam failed:"<<reply.error();
    }
    return false;
}

bool GlobalConfig::writeAllGlobalParam(QVariantMap &oMap)
{
    QDBusInterface globalManager(SETTINGS_DAEMON_SYSTEMDBUS_NAME, \
                          SETTINGS_DAEMON_SYSTEMDBUS_PATH, \
                          SETTINGS_DAEMON_SYSTEMDBUS_INTERFACE, \
                          QDBusConnection::systemBus());
    QList<QVariant> arguments;

    arguments<<QVariant(QString::fromLatin1(DBUS_POWER_MANAGER_SETTINGS))<<oMap;
    QDBusPendingCall reply  = globalManager.asyncCallWithArgumentList("setSchemaGlobalConf", arguments);
    qDebug() << __func__ << arguments;
    qDebug() << __func__ << oMap;
    if (reply.isError()) {
        qDebug() <<"writeGlobalParam failed:"<< reply.error();
        return false;
    }
    return true;
}

