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

#include <QDebug>

PowerManagementDamon::PowerManagementDamon()
{
    eventwatcher = new EventWatcher;
    if (eventwatcher->isNeedConfig()) {
        initConfig();
    }
    eventwatcher->initEventWatcher();
    idlenesswatcher = new IdlenessWatchcer;
    idlenesswatcher->initIdleWatcher();
    lidwatcher = new LidWatcher;
    lidwatcher->initLidWatcher();
    acwatcher = new AcWatcher;
    lowpowerwatcher = new LowPowerWatcher;
    lowpowerwatcher->initLowPowerWatcher();
    gsettingwatcher = new GsettingWatcher;
    gsettingwatcher->initGsettingWatcher();
    powermsgnotificat = new PowerMsgNotificat;
    powermsgnotificat->initPowerMsgNotificat();
}

PowerManagementDamon::~PowerManagementDamon()
{
    delete idlenesswatcher;
    delete lidwatcher;
    delete eventwatcher;
    delete acwatcher;
    delete lowpowerwatcher;
    delete gsettingwatcher;
    delete powermsgnotificat;
}

void PowerManagementDamon::initConfig()
{
    QSettings Settings("/etc/ukui/power/power-manager.conf", QSettings::NativeFormat);
    if (Settings.status() != QSettings::NoError) {
        initGsettings();
        qDebug() << "init power manager config error!";
        return;
    }

    QString proName = getProduct();
    Settings.beginGroup("product");
    QStringList str = Settings.childKeys();
    if (-1 == str.indexOf(proName)) {
        Settings.endGroup();
        int manufacturer = getManufacturer();
        if (Greatwall == manufacturer) {
            eventwatcher->setGsettings(POWER_POLICY_BATTERY, EventWatcher::Balance);
            return;
        } else {
            initGsettings();
            qDebug() << "This product does not require customization";
            return;
        }
    }
    Settings.endGroup();
    Settings.beginGroup(proName);
    eventwatcher->setGsettings(POWER_POLICY_BATTERY, Settings.value(POWER_POLICY_BATTERY).toInt());
    eventwatcher->setGsettings(POWER_POLICY_AC, Settings.value(POWER_POLICY_AC).toInt());
    Settings.endGroup();
}

QString PowerManagementDamon::getProduct()
{
    QString name;
    QFile file("/sys/class/dmi/id/product_name");
    if (!file.open(QIODevice::ReadOnly)) {
        name = "null";
        qDebug() << "product name get error, msg is :" << file.errorString();
        return name;
    }
    QByteArray data = file.readAll();
    file.close();
    name.prepend(data);
    name = name.simplified();
    qDebug() << "product name is :" << name;
    return name;
}

int PowerManagementDamon::getManufacturer()
{
    QString vendor;
    QFile file("/sys/class/dmi/id/board_vendor");
    if (!file.open(QIODevice::ReadOnly)) {
        qDebug() << "Manufacturer get error, msg is :" << file.errorString();
        return Unknown;
    }
    QByteArray data = file.readAll();
    file.close();
    vendor.prepend(data);
    vendor = vendor.simplified();
    qDebug() << "Manufacturer is :" << vendor;
    if (QString("Greatwall") == vendor) {
        qDebug() << "Greatwall";
        return Greatwall;
    } else {
        qDebug() << "unknow";
        return Unknown;
    }
}

void PowerManagementDamon::initGsettings()
{
    eventwatcher->initGsettings();
}
