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

#include "upm_upowerdbus.h"

Q_GLOBAL_STATIC(UpmUpowerDBus, s_upowerDBus)

UpmUpowerDBus::UpmUpowerDBus(QObject *parent) : QObject(parent)
{
    m_upowerDBus = new UPowerDBus(this);
    connect(m_upowerDBus, &UPowerDBus::lidStateChanged, this, &UpmUpowerDBus::dealLidStateChanged);
    connect(m_upowerDBus, &UPowerDBus::batteryOnlineStateChanged, this, &UpmUpowerDBus::dealBatteryOnlineStateChanged);

    int index = 0;
    QStringList batteryPathList = m_upowerDBus->getBatteryPathList();
    for (index = 0; index < batteryPathList.size(); ++index) {
        UpowerBatteryDBus *upowerBatteryDBus = new UpowerBatteryDBus(index, batteryPathList.at(index), this);
        connect(upowerBatteryDBus, &UpowerBatteryDBus::batteryInfoChanged, this, &UpmUpowerDBus::dealBatteryInfoChanged);
        m_batteryDBusVector << upowerBatteryDBus;
    }

    QStringList linePowerPathList = m_upowerDBus->getLinePowerPathList();
    for (index = 0; index < linePowerPathList.size(); ++index) {
        UpowerLinePowerDBus *upowerLinePowerDBus = new UpowerLinePowerDBus(index, linePowerPathList.at(index), this);
        connect(upowerLinePowerDBus, &UpowerLinePowerDBus::acOnlineStateChanged, this, &UpmUpowerDBus::dealAcOnlineStateChanged);
        m_linePowerDBusVector << upowerLinePowerDBus;
    }

    qDebug() << "Battery Num:" << m_batteryDBusVector.size();
    qDebug() << "line power Num:" << m_linePowerDBusVector.size();
}

UpmUpowerDBus::~UpmUpowerDBus() {}

UpmUpowerDBus *UpmUpowerDBus::self()
{
    return s_upowerDBus;
}

void UpmUpowerDBus::dealLidStateChanged(bool value)
{
    qDebug() << "lid state changed:" << value;
    emit lidStateChanged(value);
}

void UpmUpowerDBus::dealBatteryOnlineStateChanged(bool value)
{
    emit batteryOnlineStateChanged(value);
}

void UpmUpowerDBus::dealBatteryInfoChanged(int index, QStringList batteryInfoChangedList)
{
    qDebug() << "battery info changed";
    emit batteryInfoChanged(index, batteryInfoChangedList);
}

void UpmUpowerDBus::dealAcOnlineStateChanged(int index, bool value)
{
    qDebug() << "ac online changed:" << value;
    emit acOnlineStateChanged(index, value);
}

bool UpmUpowerDBus::getLidClosedState()
{
    return m_upowerDBus->getLidClosedState();
}

bool UpmUpowerDBus::getBatteryOnlineState()
{
    return m_upowerDBus->getBatteryOnlineState();
}

QString UpmUpowerDBus::getUpowerDaemonVersion()
{
    return m_upowerDBus->getUpowerDaemonVersion();
}

int UpmUpowerDBus::getBatteryNum()
{
    return m_batteryDBusVector.size();
}

int UpmUpowerDBus::getLinePowerNum()
{
    return m_linePowerDBusVector.size();
}

int UpmUpowerDBus::getBatteryState(int index)
{
    if (0 == m_batteryDBusVector.size()) {
        return 0;
    }
    return m_batteryDBusVector.at(index)->getBatteryState();
}

double UpmUpowerDBus::getOneBatteryPercentage(int index)
{
    if (0 == m_batteryDBusVector.size()) {
        return 0;
    }
    return m_batteryDBusVector.at(index)->getBatteryPercentage();
}

double UpmUpowerDBus::getBatteryPercentage()
{
    if (0 == m_batteryDBusVector.size()) {
        return 0;
    }

    double percentage = 0.0;
    for (int index = 0; index < m_batteryDBusVector.size(); ++index) {
        percentage += m_batteryDBusVector.at(index)->getBatteryPercentage();
    }

    return percentage / m_batteryDBusVector.size();
}

double UpmUpowerDBus::calcBatteryPercentage()
{
    if (0 == m_batteryDBusVector.size()) {
        return 0;
    }

    double energyNow = 0.0;
    double energyFull = 0.0;
    for (int index = 0; index < m_batteryDBusVector.size(); ++index) {
        energyNow += m_batteryDBusVector.at(index)->getEnergyNow();
        energyFull += m_batteryDBusVector.at(index)->getEnergyFull();
    }

    if (0 == energyFull) {
        return 0;
    }

    return energyNow / energyFull * 100;
}

double UpmUpowerDBus::getEnergyNow(int index)
{
    if (0 == m_batteryDBusVector.size()) {
        return 0;
    }
    return m_batteryDBusVector.at(index)->getEnergyNow();
}

double UpmUpowerDBus::getEnergyFull(int index)
{
    if (0 == m_batteryDBusVector.size()) {
        return 0;
    }
    return m_batteryDBusVector.at(index)->getEnergyFull();
}

double UpmUpowerDBus::getEnergyFullDesign(int index)
{
    if (0 == m_batteryDBusVector.size()) {
        return 0;
    }
    return m_batteryDBusVector.at(index)->getEnergyFullDesign();
}

double UpmUpowerDBus::getEnergyRate(int index)
{
    if (0 == m_batteryDBusVector.size()) {
        return 0;
    }
    return m_batteryDBusVector.at(index)->getEnergyRate();
}

qlonglong UpmUpowerDBus::getTimeToFull(int index)
{
    if (0 == m_batteryDBusVector.size()) {
        return 0;
    }
    return m_batteryDBusVector.at(index)->getTimeToFull();
}

qlonglong UpmUpowerDBus::getTimeToEmpty(int index)
{
    if (0 == m_batteryDBusVector.size()) {
        return 0;
    }
    return m_batteryDBusVector.at(index)->getTimeToEmpty();
}

double UpmUpowerDBus::getBatteryTemperature(int index)
{
    if (0 == m_batteryDBusVector.size()) {
        return 0;
    }
    return m_batteryDBusVector.at(index)->getBatteryTemperature();
}

int UpmUpowerDBus::getWarninglevel(int index)
{
    if (0 == m_batteryDBusVector.size()) {
        return 0;
    }
    return m_batteryDBusVector.at(index)->getWarninglevel();
}

QString UpmUpowerDBus::getBatteryVendor(int index)
{
    if (0 == m_batteryDBusVector.size()) {
        return "unknown";
    }
    return m_batteryDBusVector.at(index)->getBatteryVendor();
}

QString UpmUpowerDBus::getBatteryModel(int index)
{
    if (0 == m_batteryDBusVector.size()) {
        return "unknown";
    }
    return m_batteryDBusVector.at(index)->getBatteryModel();
}

QString UpmUpowerDBus::getOneBatteryIconName(int index)
{
    if (0 == m_batteryDBusVector.size() || 0 == m_linePowerDBusVector.size()) {
        return "battery-level-0-charging-symbolic";
    }
    double percentage = m_batteryDBusVector.at(index)->getBatteryPercentage();

    if (true == m_linePowerDBusVector.at(index)->getAcOnlineState()) {
        m_batteryIconName = QString("battery-level-%1-charging-symbolic").arg((int)percentage / 10 * 10);
    } else {
        m_batteryIconName = QString("battery-level-%1-symbolic").arg((int)percentage / 10 * 10);
    }

    return m_batteryIconName;
}

QString UpmUpowerDBus::getBatteryIconName()
{
    if (0 == m_batteryDBusVector.size() || 0 == m_linePowerDBusVector.size()) {
        return "battery-level-0-charging-symbolic";
    }

    double percentage = getBatteryPercentage();
    if (true == getAcOnlineState()) {
        m_batteryIconName = QString("battery-level-%1-charging-symbolic").arg((int)percentage / 10 * 10);
    } else {
        m_batteryIconName = QString("battery-level-%1-symbolic").arg((int)percentage / 10 * 10);
    }

    return m_batteryIconName;
}

bool UpmUpowerDBus::getAcOnlineState()
{
    if (0 == m_batteryDBusVector.size() || 0 == m_linePowerDBusVector.size()) {
        return true;
    }
    for (int index = 0; index < m_linePowerDBusVector.size(); ++index) {
        if (true == m_linePowerDBusVector.at(index)->getAcOnlineState()) {
            return true;
        }
    }
    return false;
}
