/*
 * Copyright (C) 2023, KylinSoft Co., Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */
#include "upm_gsettings.h"

#include <QDebug>

Q_GLOBAL_STATIC(UpmGsettings, s_upmGsettings)

UpmGsettings::UpmGsettings(QObject *parent) : QObject(parent)
{
    const QByteArray powerGsettingsID(POWER_MANAGER_SETTINGS);
    m_powerGsettings = new QGSettings(powerGsettingsID);
    m_allPowerKey = m_powerGsettings->keys();

    const QByteArray processGsettingsID(PROCESS_MANAGER_SETTINGS);
    m_processGsettings = new QGSettings(processGsettingsID);
    m_allProcessKey = m_processGsettings->keys();

    const QByteArray softFreezeModeGsettingsID(SOFT_FREEZE_MODE);
    m_softFreezeModeGsettings = new QGSettings(softFreezeModeGsettingsID);
    m_allSoftFreezeModeKey = m_softFreezeModeGsettings->keys();

    const QByteArray screenSaverSettingsId(SCREEN_SAVER_SETTINGS);
    m_screenSaverGsettings = new QGSettings(screenSaverSettingsId);
    m_allScreenSaverKey.clear();
    m_allScreenSaverKey << GSETTINGS_KEY_SLEEP_ACTIVATION_ENABLED
                        << GSETTINGS_KEY_CLOSE_ACTIVATION_ENABLED;

    initGsettingsConfig();

    connect(m_powerGsettings, &QGSettings::changed, this, &UpmGsettings::dealGsettingsKeyChanged);
    connect(m_processGsettings, &QGSettings::changed, this, &UpmGsettings::dealGsettingsKeyChanged);
    connect(m_softFreezeModeGsettings, &QGSettings::changed, this, &UpmGsettings::dealGsettingsKeyChanged);
    connect(m_screenSaverGsettings, &QGSettings::changed, this, &UpmGsettings::dealGsettingsKeyChanged);
}

UpmGsettings::~UpmGsettings()
{
    delete m_powerGsettings;
    delete m_processGsettings;
    delete m_softFreezeModeGsettings;
    delete m_screenSaverGsettings;
}

UpmGsettings *UpmGsettings::self()
{
    return s_upmGsettings;
}

void UpmGsettings::dealGsettingsKeyChanged(const QString &key)
{
    if (true == m_qHashValue.contains(key)) {
        if (m_allPowerKey.contains(key)) {
            m_qHashValue[key] = m_powerGsettings->get(key);
        } else if (m_allProcessKey.contains(key)) {
            m_qHashValue[key] = m_processGsettings->get(key);
        } else if (m_allSoftFreezeModeKey.contains(key)) {
            m_qHashValue[key] = m_softFreezeModeGsettings->get(key);
        } else if (m_allScreenSaverKey.contains(key)) {
            m_qHashValue[key] = m_screenSaverGsettings->get(key);
        }
        emit (this->*m_qHashSignalsFuns[key])(m_qHashValue[key]);
        qDebug() << "key" << key << "changed";
    } else {
        qInfo() << "this key:" << key << "is not monitored!";
    }
}

void UpmGsettings::setGsettingsConfig(const QString &configName, QVariant value)
{
    if (true == m_qHashValue.contains(configName)) {
        m_qHashValue[configName] = value;
        if (m_allPowerKey.contains(configName)) {
            m_powerGsettings->set(configName, value);
        } else if (m_allProcessKey.contains(configName)) {
            m_processGsettings->set(configName, value);
        } else if (m_allSoftFreezeModeKey.contains(configName)) {
            m_softFreezeModeGsettings->set(configName, value);
        } else if (m_allScreenSaverKey.contains(configName)) {
            m_screenSaverGsettings->set(configName, value);
        }
        qDebug() << "set key:" << configName << "value:" << value;
    } else {
        qInfo() << "this key:" << configName << "is not monitored!";
    }
}

bool UpmGsettings::getGsettingsConfig(const QString &configName, QVariant &value)
{
    qDebug() << "get key:" << configName;
    if (true == m_qHashValue.contains(configName)) {
        value = m_qHashValue[configName];
        return true;
    }
    qInfo() << "this key:" << configName << "is not monitored!";
    return false;
}

QVariant::Type UpmGsettings::getValueType(const QString &configName)
{
    if (m_qHashValueType.contains(configName)) {
        return m_qHashValueType[configName];
    }
    return QVariant::Invalid;
}

void UpmGsettings::addConfigMonitor(const QString &configName, pSignalFun signalFun, QVariant::Type type)
{
    if (m_allPowerKey.contains(configName)) {
        m_qHashValue[configName] = m_powerGsettings->get(configName);
        m_qHashSignalsFuns[configName] = signalFun;
        m_qHashValueType[configName] = type;
        qDebug() << "power gsetting:" << configName << "to be monitored!";
    } else if (m_allProcessKey.contains(configName)) {
        m_qHashValue[configName] = m_processGsettings->get(configName);
        m_qHashSignalsFuns[configName] = signalFun;
        m_qHashValueType[configName] = type;
        qDebug() << "process gsetting:" << configName << "to be monitored!";
    } else if (m_allSoftFreezeModeKey.contains(configName)) {
        m_qHashValue[configName] = m_softFreezeModeGsettings->get(configName);
        m_qHashSignalsFuns[configName] = signalFun;
        m_qHashValueType[configName] = type;
        qDebug() << "soft freeze mode gsetting:" << configName << "to be monitored!";
    } else if (m_allScreenSaverKey.contains(configName)) {
        m_qHashValue[configName] = m_screenSaverGsettings->get(configName);
        m_qHashSignalsFuns[configName] = signalFun;
        m_qHashValueType[configName] = type;
        qDebug() << "screen saver gsetting:" << configName << "to be monitored!";
    } else {
        qInfo() << "without this key:" << configName;
    }
}

void UpmGsettings::initGsettingsConfig()
{
    addConfigMonitor(GSETTINGS_KEY_POWER_POLICY_AC,
                     &UpmGsettings::powerPolicyAcChanged,
                     QVariant::Int);
    addConfigMonitor(GSETTINGS_KEY_POWER_POLICY_BATTERY,
                     &UpmGsettings::powerPolicyBatteryChanged,
                     QVariant::Int);
    addConfigMonitor(GSETTINGS_KEY_POWER_POLICY_BATTERY_BACKUP,
                     &UpmGsettings::powerPolicyBatteryBackupChanged,
                     QVariant::Int);

    addConfigMonitor(GSETTINGS_KEY_SLEEP_DISPLAY_AC,
                     &UpmGsettings::sleepDisplayAcChanged,
                     QVariant::Int);
    addConfigMonitor(GSETTINGS_KEY_SLEEP_DISPLAY_BATTERY,
                     &UpmGsettings::sleepDisplayBatteryChanged,
                     QVariant::Int);

    addConfigMonitor(GSETTINGS_KEY_SLEEP_COMPUTER_AC,
                     &UpmGsettings::sleepComputerAcChanged,
                     QVariant::Int);
    addConfigMonitor(GSETTINGS_KEY_SLEEP_COMPUTER_BATTERY,
                     &UpmGsettings::sleepComputerBatteryChanged,
                     QVariant::Int);

    addConfigMonitor(GSETTINGS_KEY_IDLE_BRIGHTNESS,
                     &UpmGsettings::idleBrightnessChanged,
                     QVariant::Int);
    addConfigMonitor(GSETTINGS_KEY_IDLE_POWER_POLICY_AUTO,
                     &UpmGsettings::idlePowerPolicyAutoChanged,
                     QVariant::Bool);

    addConfigMonitor(GSETTINGS_KEY_BUTTON_LID_AC,
                     &UpmGsettings::buttonLidAcChanged,
                     QVariant::String);
    addConfigMonitor(GSETTINGS_KEY_BUTTON_LID_BATTERY,
                     &UpmGsettings::buttonLidBatteryChanged,
                     QVariant::String);

    addConfigMonitor(GSETTINGS_KEY_PERCENTAGE_LOW,
                     &UpmGsettings::percentageLowChanged,
                     QVariant::Int);
    //极低电量百分比
    addConfigMonitor(GSETTINGS_KEY_PERCENTAGE_ACTION,
                     &UpmGsettings::percentageActionChanged,
                     QVariant::Int);
    //极低电量操作
    addConfigMonitor(GSETTINGS_KEY_ACTION_CRITICAL_BATTERY,
                     &UpmGsettings::actionCriticalBatteryChanged,
                     QVariant::String);

    addConfigMonitor(GSETTINGS_KEY_BATTERY_SAVE_SWITCH,
                     &UpmGsettings::batterySaveSwitchChanged,
                     QVariant::Bool);
    addConfigMonitor(GSETTINGS_KEY_LOW_BATTERY_AUTO_SAVE,
                     &UpmGsettings::lowBatteryAutoSaveChanged,
                     QVariant::Bool);
    addConfigMonitor(GSETTINGS_KEY_BATTERY_SAVE_REDUCE_BRIGHTNESS,
                     &UpmGsettings::batterySaveReduceBrightnessChanged,
                     QVariant::Bool);
    addConfigMonitor(GSETTINGS_KEY_LOW_BATTERY_AUTO_SOFT_FREEZE_MODE,
                     &UpmGsettings::lowBatteryAutoSoftFreezeModeChanged,
                     QVariant::Bool);

    addConfigMonitor(GSETTINGS_KEY_BRIGHTNESS_AC,
                     &UpmGsettings::brightnessAcChanged,
                     QVariant::Int);

    addConfigMonitor(GSETTINGS_KEY_RESOURCE_LIMIT_ENABLED,
                     &UpmGsettings::resourceLimitEnabledChanged,
                     QVariant::Bool);

    addConfigMonitor(GSETTINGS_KEY_SOFT_FREEZE_MODE_ENABLED,
                     &UpmGsettings::softFreezeModeEnabledChanged,
                     QVariant::Bool);

    addConfigMonitor(GSETTINGS_KEY_SLEEP_ACTIVATION_ENABLED,
                     &UpmGsettings::sleepActivationEnabledChanged,
                     QVariant::Bool);
    addConfigMonitor(GSETTINGS_KEY_CLOSE_ACTIVATION_ENABLED,
                     &UpmGsettings::closeActivationEnabledChanged,
                     QVariant::Bool);
}
