/*
 * 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_fractionalfreezing.h"

UpmFractionalFreezing::UpmFractionalFreezing(QObject *parent) : QObject(parent)
{
    QStringList valueRange = {"true", "false"};
    m_enableFractionalFreezing = UpmCheckConfig::checkConfig(
                GSETTINGS_KEY_RESOURCE_LIMIT_ENABLED,
                false, QVariant::Bool,
                valueRange, false).toBool();
    m_enableSoftFreezeMode = UpmCheckConfig::checkConfig(
                GSETTINGS_KEY_SOFT_FREEZE_MODE_ENABLED,
                false, QVariant::Bool,
                valueRange, false).toBool();
    m_autoEnableSoftFreezeMode = UpmCheckConfig::checkConfig(
                GSETTINGS_KEY_LOW_BATTERY_AUTO_SOFT_FREEZE_MODE,
                false, QVariant::Bool,
                valueRange, false).toBool();

    valueRange.clear();
    valueRange << QString::number(fractionalFreezingNoBackup)
               << QString::number(fractionalFreezingOnSoftFreezeModeOn);
    m_fractionalFreezingBackupState = UpmCheckConfig::checkConfig(
                GSETTINGS_KEY_FRACTIONAL_FREEZING_BACKUP,
                fractionalFreezingNoBackup, QVariant::Int,
                valueRange, true).toInt();

    connect(UpmGsettings::self(), &UpmGsettings::resourceLimitEnabledChanged,
            this, &UpmFractionalFreezing::dealEnableFractionalFreezingChanged);
    connect(UpmGsettings::self(), &UpmGsettings::softFreezeModeEnabledChanged,
            this, &UpmFractionalFreezing::dealEnableSoftFreezeModeChanged);
    connect(UpmGsettings::self(), &UpmGsettings::lowBatteryAutoSoftFreezeModeChanged,
            this, &UpmFractionalFreezing::dealAutoEnableSoftFreezeModeChanged);

    if (true == UpmUpowerDBus::self()->getAcOnlineState()) {
        dealBatteryLowState(false);
    } else {
        double percentage = UpmUpowerDBus::self()->getBatteryPercentage();
        if (UpmUpowerDBus::self()->getBatteryPercentage() > percentage) {
            dealBatteryLowState(false);
        }
    }
}

void UpmFractionalFreezing::dealBatteryLowState(bool batteryLowState)
{
    m_batteryLowState = batteryLowState;

    if (true == m_batteryLowState) {
        if (true == m_autoEnableSoftFreezeMode) {
            if (true == m_enableFractionalFreezing) {
                if (false == m_enableSoftFreezeMode) {
                    UpmGsettings::self()->setGsettingsConfig(GSETTINGS_KEY_SOFT_FREEZE_MODE_ENABLED, true);
                    m_fractionalFreezingBackupState = fractionalFreezingOnSoftFreezeModeOff;
                } else {
                    m_fractionalFreezingBackupState = fractionalFreezingOnSoftFreezeModeOn;
                }
            } else {
                m_fractionalFreezingBackupState = fractionalFreezingOff;
                UpmGsettings::self()->setGsettingsConfig(GSETTINGS_KEY_RESOURCE_LIMIT_ENABLED, true);
                UpmGsettings::self()->setGsettingsConfig(GSETTINGS_KEY_SOFT_FREEZE_MODE_ENABLED, true);
            }
            UpmControlDBus::self()->setUpmBoolConfig(GSETTINGS_KEY_FRACTIONAL_FREEZING_BACKUP,
                                                     m_fractionalFreezingBackupState);
        }
    } else {
        if (true == m_autoEnableSoftFreezeMode) {
            switch (m_fractionalFreezingBackupState) {
                case fractionalFreezingOff:
                    UpmGsettings::self()->setGsettingsConfig(GSETTINGS_KEY_RESOURCE_LIMIT_ENABLED, false);
                    break;
                case fractionalFreezingOnSoftFreezeModeOff:
                    UpmGsettings::self()->setGsettingsConfig(GSETTINGS_KEY_SOFT_FREEZE_MODE_ENABLED, false);
                    break;
                case fractionalFreezingOnSoftFreezeModeOn:
                    break;
                default:
                    break;
            }
            m_fractionalFreezingBackupState = fractionalFreezingNoBackup;
            UpmControlDBus::self()->setUpmBoolConfig(GSETTINGS_KEY_FRACTIONAL_FREEZING_BACKUP,
                                                     m_fractionalFreezingBackupState);
        }
    }
}

void UpmFractionalFreezing::dealEnableFractionalFreezingChanged(QVariant value)
{
    m_enableFractionalFreezing = value.toBool();
    UpmControlDBus::self()->setUpmBoolConfig(GSETTINGS_KEY_RESOURCE_LIMIT_ENABLED,
                                             m_enableFractionalFreezing);
}

void UpmFractionalFreezing::dealEnableSoftFreezeModeChanged(QVariant value)
{
    m_enableSoftFreezeMode = value.toBool();
    UpmControlDBus::self()->setUpmBoolConfig(GSETTINGS_KEY_SOFT_FREEZE_MODE_ENABLED,
                                             m_enableSoftFreezeMode);
}

void UpmFractionalFreezing::dealAutoEnableSoftFreezeModeChanged(QVariant value)
{
    m_autoEnableSoftFreezeMode = value.toBool();
    UpmControlDBus::self()->setUpmBoolConfig(GSETTINGS_KEY_LOW_BATTERY_AUTO_SOFT_FREEZE_MODE,
                                             m_autoEnableSoftFreezeMode);
    if (false == m_autoEnableSoftFreezeMode) {
        m_fractionalFreezingBackupState = fractionalFreezingNoBackup;
        UpmControlDBus::self()->setUpmBoolConfig(GSETTINGS_KEY_FRACTIONAL_FREEZING_BACKUP,
                                                 m_fractionalFreezingBackupState);
    }

    if (true == m_batteryLowState) {
        if (true == m_enableFractionalFreezing) {
            if (false == m_enableSoftFreezeMode) {
                UpmGsettings::self()->setGsettingsConfig(GSETTINGS_KEY_SOFT_FREEZE_MODE_ENABLED, true);
                m_fractionalFreezingBackupState = fractionalFreezingOnSoftFreezeModeOff;
            } else {
                m_fractionalFreezingBackupState = fractionalFreezingOnSoftFreezeModeOn;
            }
        } else {
            m_fractionalFreezingBackupState = fractionalFreezingOff;
            UpmGsettings::self()->setGsettingsConfig(GSETTINGS_KEY_RESOURCE_LIMIT_ENABLED, true);
            UpmGsettings::self()->setGsettingsConfig(GSETTINGS_KEY_SOFT_FREEZE_MODE_ENABLED, true);
        }
        UpmControlDBus::self()->setUpmBoolConfig(GSETTINGS_KEY_FRACTIONAL_FREEZING_BACKUP,
                                                 m_fractionalFreezingBackupState);
    }
}

