/*
 * Copyright 2021 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 <http://www.gnu.org/licenses/>.
 */

#include "sysdbusregister.h"

#include <QDebug>

SysdbusRegister::SysdbusRegister()
{
    m_powerConfig = new PowerConfig(this);

    if (true == m_powerConfig->isFirstRun()) {
        m_powerConfig->updatePowerConfig(GSETTINGS_KEY_BRIGHTNESS_AC,
                                         QString::number(DEFAULE_BRIGHTNESS_PERCNET));
    }
    int brightnessPercent = m_powerConfig->getPowerConfig(GSETTINGS_KEY_BRIGHTNESS_AC).toInt();
    m_brightnessNode = new BrightnessNode(brightnessPercent, this);

    m_upmCpuControl = new UpmCpuControl(this);

    m_devPowerControl = new DevPowerControl(this);

    m_hardwarePowerControl = new HardwarePowerControl(this);

    m_isPanguM900 = UpmCustomHardwareConfig::self()->isPanguM900();
}

SysdbusRegister::~SysdbusRegister() {}

void SysdbusRegister::blockLidAction()
{
    QDBusInterface interface(
        QStringLiteral("org.freedesktop.login1"),
        QStringLiteral("/org/freedesktop/login1"),
        QStringLiteral("org.freedesktop.login1.Manager"),
        QDBusConnection::systemBus());
    QDBusReply<QDBusUnixFileDescriptor> reply = interface.call(
        QStringLiteral("Inhibit"),
        QStringLiteral("handle-lid-switch"),
        QStringLiteral("powermanagment"),
        QStringLiteral("ukui-powermanagement blocked LidSwitch!"),
        QStringLiteral("block"));
    if (reply.isValid()) {
        m_login1Lock = reply.value();
        m_blockLidAction = true;
        qDebug() << "Inhibit got:" << m_login1Lock.fileDescriptor();
    } else {
        qDebug() << "Error from inhibit:" << reply.error();
    }
}

void SysdbusRegister::releaseLidAction()
{
    m_blockLidAction = false;
}

void SysdbusRegister::UserLogin(const QString userName)
{
    if (false == m_userMap.contains(userName)) {
        m_userMap.insert(userName, true);
    }
    if (false == m_blockLidAction) {
        blockLidAction();
    }
    UserActive(userName);
}

void SysdbusRegister::UserLogout(const QString userName)
{
    if (true == m_userMap.contains(userName)) {
        m_userMap.remove(userName);
        if (0 == m_userMap.size()) {
            if (true == m_blockLidAction) {
                releaseLidAction();
            }
        }
    }
}

void SysdbusRegister::UserActive(const QString userName)
{
    QList<QString> keys = m_userMap.keys();

    for (int i = 0; i < keys.size(); ++i) {
        QString key = keys.at(i);
        if (key == userName) {
            m_userMap.insert(key, true);
        } else {
            m_userMap.insert(key, false);
        }
    }
    emit ActiveUserChanged(userName);
}

bool SysdbusRegister::CanSetBrightness()
{
    return m_brightnessNode->canSetBrightness();
}

void SysdbusRegister::SetBrightnessPercent(const int percent)
{
    m_brightnessNode->setBrightnessPercent(percent);
}

int SysdbusRegister::GetBrightnessPercent()
{
    return m_brightnessNode->getBrightnessPercent();
}

int SysdbusRegister::GetMaxBrightness()
{
    return m_brightnessNode->getMaxBrightness();
}

void SysdbusRegister::SetReduceBacklightSwitch(const bool value)
{
    m_brightnessNode->setReduceMaxBrightness(value);
}

int SysdbusRegister::GetCpuPolicy()
{
    return m_cpuPolicy;
}

void SysdbusRegister::SetPcPolicy(const int policy)
{
    m_cpuPolicy = policy;
    if (false == m_isPanguM900) {
        m_upmCpuControl->setAllCorePolicy(policy);
    }

    m_hardwarePowerControl->setGpuFrequencyMode(policy);
    m_hardwarePowerControl->setAudioMode(policy);
    m_hardwarePowerControl->setPcieAspmMode(policy);

    if (system_policy_performance == policy) {
        m_devPowerControl->setAllDevicePerformance();
    } else if (system_policy_powersave == policy) {
        m_devPowerControl->setAllDeviceSave();
    } else {
        m_devPowerControl->setAllDeviceBalance();
    }
}

void SysdbusRegister::SetCpuPolicy(const int policy)
{
    m_cpuPolicy = policy;
    if (false == m_isPanguM900) {
        m_upmCpuControl->setAllCorePolicy(policy);
    }
}

void SysdbusRegister::SetGpuPolicy(const int policy)
{
    m_hardwarePowerControl->setGpuFrequencyMode(policy);
}

void SysdbusRegister::SetAudioPolicy(const int policy)
{
    m_hardwarePowerControl->setAudioMode(policy);
}

void SysdbusRegister::SetAspmPolicy(const int policy)
{
    m_hardwarePowerControl->setPcieAspmMode(policy);
}

void SysdbusRegister::SetSystemConfig(const QString userName, const QString key, const QString value)
{
    m_powerConfig->updatePowerConfig(key, value);

    emit SystemConfigChanged(userName, key, value);
}

QString SysdbusRegister::GetSystemConfig(const QString key)
{
    return m_powerConfig->getPowerConfig(key);
}

