/*
 * 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/>.
 *
 */
#ifndef UPM_GSETTINGS_H
#define UPM_GSETTINGS_H

#include <QObject>
#include <QProcess>
#include <QGSettings>
#include <QVariant>

//power gsettings 配置
#define POWER_MANAGER_SETTINGS   "org.ukui.power-manager"

//电源策略(0:节能 1:平衡 2:性能)
#define GSETTINGS_KEY_POWER_POLICY_AC          "powerPolicyAc"
#define GSETTINGS_KEY_POWER_POLICY_BATTERY     "powerPolicyBattery"
#define GSETTINGS_KEY_POWER_POLICY_BATTERY_BACKUP  "powerPolicyBatteryBackup"
//空闲关闭显示器
#define GSETTINGS_KEY_SLEEP_DISPLAY_AC         "sleepDisplayAc"
#define GSETTINGS_KEY_SLEEP_DISPLAY_BATTERY    "sleepDisplayBattery"

//空闲睡眠
#define GSETTINGS_KEY_SLEEP_COMPUTER_AC        "sleepComputerAc"
#define GSETTINGS_KEY_SLEEP_COMPUTER_BATTERY   "sleepComputerBattery"
//空闲降低屏幕亮度
#define GSETTINGS_KEY_IDLE_BRIGHTNESS          "idleBrightness"        //空闲状态亮度,降低为当前值的百分之多少
//空闲时切换电源策略
#define GSETTINGS_KEY_IDLE_POWER_POLICY_AUTO   "idlePowerPolicyAuto"

//盒盖事件触发操作
#define GSETTINGS_KEY_BUTTON_LID_AC            "buttonLidAc"
#define GSETTINGS_KEY_BUTTON_LID_BATTERY       "buttonLidBattery"

//低电量百分百通知
#define GSETTINGS_KEY_PERCENTAGE_LOW           "percentageLow"
//极低电量
#define GSETTINGS_KEY_PERCENTAGE_ACTION        "percentageAction"
//极低电量时执行
#define GSETTINGS_KEY_ACTION_CRITICAL_BATTERY  "actionCriticalBattery"

//开启节能模式
#define GSETTINGS_KEY_BATTERY_SAVE_SWITCH      "batterySaveSwitch"
//低电量时自动开启节能模式
#define GSETTINGS_KEY_LOW_BATTERY_AUTO_SAVE    "lowBatteryAutoSave"
//节能模式降低亮度
#define GSETTINGS_KEY_BATTERY_SAVE_REDUCE_BRIGHTNESS    "batterySaveReduceBrightness"
//低电量时自动开启软冻模式
#define GSETTINGS_KEY_LOW_BATTERY_AUTO_SOFT_FREEZE_MODE    "lowBatteryAutoSoftFreezeMode"

//亮度值
#define GSETTINGS_KEY_BRIGHTNESS_AC            "brightnessAc"

#define PREPROCESS_POWER_MODE   "preprocessPowerMode"
//process gsettings 配置
#define PROCESS_MANAGER_SETTINGS   "org.ukui.process-manager"
#define GSETTINGS_KEY_RESOURCE_LIMIT_ENABLED   "resourceLimitEnabled"

#define SOFT_FREEZE_MODE    "org.ukui.process-manager.soft-freeze-mode"
#define GSETTINGS_KEY_SOFT_FREEZE_MODE_ENABLED    "enabled"

#define GSETTINGS_KEY_FRACTIONAL_FREEZING_BACKUP  "fractionalFreezingBackup"

class UpmGsettings : public QObject
{
    Q_OBJECT
public:
    explicit UpmGsettings(QObject *parent = nullptr);
    ~UpmGsettings();
    static UpmGsettings* self();

    typedef void (UpmGsettings::*pSignalFun)(QVariant);
private:
    QGSettings *m_powerGsettings;
    QStringList m_allPowerKey;
    QGSettings *m_processGsettings;
    QStringList m_allProcessKey;
    QGSettings *m_softFreezeModeGsettings;
    QStringList m_allSoftFreezeModeKey;
    QHash<QString, QVariant> m_qHashValue;
    QHash<QString, pSignalFun> m_qHashSignalsFuns;
    QHash<QString, QVariant::Type> m_qHashValueType;

    void initGsettingsConfig();
    void addConfigMonitor(const QString &configName, pSignalFun signalFun, QVariant::Type type);

signals:
    void powerPolicyAcChanged(QVariant);
    void powerPolicyBatteryChanged(QVariant);
    void powerPolicyBatteryBackupChanged(QVariant);

    void sleepDisplayAcChanged(QVariant);
    void sleepDisplayBatteryChanged(QVariant);

    void sleepComputerAcChanged(QVariant);
    void sleepComputerBatteryChanged(QVariant);

    void idleBrightnessChanged(QVariant);
    void idlePowerPolicyAutoChanged(QVariant);

    void buttonLidAcChanged(QVariant);
    void buttonLidBatteryChanged(QVariant);

    void percentageLowChanged(QVariant);
    void percentageActionChanged(QVariant);
    void actionCriticalBatteryChanged(QVariant);

    void batterySaveSwitchChanged(QVariant);
    void lowBatteryAutoSaveChanged(QVariant);
    void batterySaveReduceBrightnessChanged(QVariant);
    void lowBatteryAutoSoftFreezeModeChanged(QVariant);

    void brightnessAcChanged(QVariant);

    void resourceLimitEnabledChanged(QVariant);
    void softFreezeModeEnabledChanged(QVariant);

private slots:
    void dealGsettingsKeyChanged(const QString &key);

public:
    void setGsettingsConfig(const QString &configName, QVariant value);
    bool getGsettingsConfig(const QString &configName, QVariant &value);
    QVariant::Type getValueType(const QString &configName);
};

#endif // UPM_GSETTINGS_H
