#include "powershow.h"
#include <QStyleOption>

Powershow::Powershow(QWidget *parent) : QWidget(parent)
{
     initDbusaddrest();
     initMemberVariables();
     initDbusValue();
     ConnetChangeDbus();
}

void Powershow::ConnetChangeDbus()
{
    QDBusConnection::systemBus().connect(QString("org.freedesktop.UPower"), powerpath, "org.freedesktop.DBus.Properties", "PropertiesChanged", this, SLOT(changesignal()));

    QDBusConnection::systemBus().connect(QString("org.freedesktop.login1"),
                                                  "/org/freedesktop/login1",
                                                  "org.freedesktop.login1.Manager",
                                                  "PrepareForSleep",
                                                  this,
                                                  SLOT(sleepsignal()));

}
void Powershow::changesignal()
{
    initDbusValue();
}
void Powershow::sleepsignal()
{
    initDbusValue();
}


Powershow::~Powershow()
{

}

void Powershow::initMemberVariables()
{

    m_IconBatterList << KYLIN_BATTERY0_PATH  << KYLIN_BATTERY10_PATH
                     << KYLIN_BATTERY20_PATH << KYLIN_BATTERY30_PATH
                     << KYLIN_BATTERY40_PATH << KYLIN_BATTERY50_PATH
                     << KYLIN_BATTERY60_PATH << KYLIN_BATTERY70_PATH
                     << KYLIN_BATTERY80_PATH << KYLIN_BATTERY90_PATH
                     << KYLIN_BATTERY100_PATH;

    m_IconBatterChangeList   << KYLIN_BATTERY_CHARGING0_PATH  << KYLIN_BATTERY_CHARGING10_PATH
                             << KYLIN_BATTERY_CHARGING20_PATH << KYLIN_BATTERY_CHARGING30_PATH
                             << KYLIN_BATTERY_CHARGING40_PATH << KYLIN_BATTERY_CHARGING50_PATH
                             << KYLIN_BATTERY_CHARGING60_PATH << KYLIN_BATTERY_CHARGING70_PATH
                             << KYLIN_BATTERY_CHARGING80_PATH << KYLIN_BATTERY_CHARGING90_PATH
                             << KYLIN_BATTERY_CHARGING100_PATH;

    Elect_surplus = QObject::tr("Electricity surplus") ;
    Charging = QObject::tr("Charging");

    QPalette pe1;
    pe1.setColor(QPalette::WindowText,QColor(0,0,0,255));

    m_pPowerLabel = new QLabel();
    m_pPowerLabel->setStyleSheet("QLabel {font:12pt;}");

    QPalette palette = m_pPowerLabel->palette();
    QStyleOption opt;
    opt.init(m_pPowerLabel);
    QColor color = opt.palette.color(QPalette::Text);
    color.setAlphaF(0.45);
    palette.setBrush(QPalette::WindowText, color);
    m_pPowerLabel->setPalette(palette);

    m_pPowericon=new QLabel();
    m_pPowericon->setScaledContents(true);
    m_pPowericon->setFixedSize(16, 16);

    m_pHboxLayout = new QHBoxLayout();
    m_pHboxLayout->setContentsMargins(60, 0, 0, 0);
    m_pHboxLayout->setSpacing(10);

    m_pHboxLayout->addWidget(m_pPowericon, 0, Qt::AlignCenter);
    m_pHboxLayout->addWidget(m_pPowerLabel, 1, Qt::AlignCenter);
    m_pHboxLayout->addItem(new QSpacerItem(50, 10, QSizePolicy::Expanding));
    
    m_pPictureToWhite = new PictureToWhite();

    this->setLayout(m_pHboxLayout);
    this->update();

}
void Powershow::initDbusaddrest()
{

    QStringList users;
    QDBusInterface m_interface1( "org.freedesktop.UPower",
                                "/org/freedesktop/UPower",
                                "org.freedesktop.UPower",
                                QDBusConnection::systemBus() );
    if (!m_interface1.isValid()) {
        qDebug() << "dbus接口初始化失败";
        return;
    }

    QDBusReply<QList<QDBusObjectPath>> obj_reply = m_interface1.call("EnumerateDevices");


     if (obj_reply.isValid()) {
         for (QDBusObjectPath op : obj_reply.value())
              users << op.path();
         if(users.size()==1 || users.isEmpty()){
             return ;
         }
         powerpath=users.at(1);

     }
     const QByteArray id_1("org.ukui.style");
     if (QGSettings::isSchemaInstalled(id_1)){
         m_psGsetting = new QGSettings(id_1);
         if (m_psGsetting != nullptr) {
             connect(m_psGsetting, &QGSettings::changed, this, &Powershow::getStyleChageSlots);
         }
     }

}

void Powershow::initDbusValue()
{

    QDBusInterface m_interface( "org.freedesktop.UPower",
                                powerpath,
                                "org.freedesktop.DBus.Properties",
                                QDBusConnection::systemBus() );

    if (!m_interface.isValid()) {
        qDebug() << "dbus接口初始化失败";
        return;
    }
    QDBusReply<QVariant> obj_reply = m_interface.call("Get","org.freedesktop.UPower.Device","Percentage");

    QDBusReply<QVariant> obj_state = m_interface.call("Get","org.freedesktop.UPower.Device","State");
    
    if (obj_reply.isValid() && obj_state.isValid()) {

        QPixmap pixmap;
        if(obj_state.value().toUInt() == 1 || obj_state.value().toUInt() == 4)
        {

            QString Ele_surplus=obj_reply.value().toString();
            int Ele_surplus_int = obj_reply.value().toInt();
            if (Ele_surplus_int>=0 && Ele_surplus_int<10)
            {
                pixmap = m_IconBatterChangeList.at(0);
            } else if (Ele_surplus_int>=10 && Ele_surplus_int<20){
                pixmap = m_IconBatterChangeList.at(1);
            } else if (Ele_surplus_int>=20 && Ele_surplus_int<30){
                pixmap = m_IconBatterChangeList.at(2);
            }else if (Ele_surplus_int>=30 && Ele_surplus_int<40){
                pixmap = m_IconBatterChangeList.at(3);
            }else if (Ele_surplus_int>=40 && Ele_surplus_int<50){
                pixmap = m_IconBatterChangeList.at(4);
            }else if (Ele_surplus_int>=50 && Ele_surplus_int<60){
                pixmap = m_IconBatterChangeList.at(5);
            }else if (Ele_surplus_int>=60 && Ele_surplus_int<70){
                pixmap = m_IconBatterChangeList.at(6);
            }else if (Ele_surplus_int>=70 && Ele_surplus_int<80){
                pixmap = m_IconBatterChangeList.at(7);
            }else if (Ele_surplus_int>=80 && Ele_surplus_int<90){
                pixmap = m_IconBatterChangeList.at(8);
            }else if (Ele_surplus_int>=90 && Ele_surplus_int<100){
                pixmap = m_IconBatterChangeList.at(9);
            }else if (Ele_surplus_int == 100){
                pixmap = m_IconBatterChangeList.at(10);
            }
            pixmap = m_pPictureToWhite->drawSymbolicColoredPixmap(pixmap);
            m_pPowericon->setPixmap(drawBackground(pixmap));

            QString filename = QString("%1%2%").arg(Charging).arg(Ele_surplus);
            QStyleOption opt;
            opt.init(m_pPowerLabel);
            QColor color = opt.palette.color(QPalette::Text);
            QPalette palette = m_pPowerLabel->palette();
            color.setAlphaF(0.45);
            palette.setBrush(QPalette::WindowText, color);
            m_pPowerLabel->setPalette(palette);
            m_pPowerLabel->setText(filename);

        } else {

            int Ele_surplus_int = obj_reply.value().toInt();
            if (Ele_surplus_int>=0 && Ele_surplus_int<10)
            {
                pixmap = m_IconBatterList.at(0);
            } else if (Ele_surplus_int>=10 && Ele_surplus_int<20){
                pixmap = m_IconBatterList.at(1);
            } else if (Ele_surplus_int>=20 && Ele_surplus_int<30){
                pixmap = m_IconBatterList.at(2);
            }else if (Ele_surplus_int>=30 && Ele_surplus_int<40){
                pixmap = m_IconBatterList.at(3);
            }else if (Ele_surplus_int>=40 && Ele_surplus_int<50){
                pixmap = m_IconBatterList.at(4);
            }else if (Ele_surplus_int>=50 && Ele_surplus_int<60){
                pixmap = m_IconBatterList.at(5);
            }else if (Ele_surplus_int>=60 && Ele_surplus_int<70){
                pixmap = m_IconBatterList.at(6);
            }else if (Ele_surplus_int>=70 && Ele_surplus_int<80){
                pixmap = m_IconBatterList.at(7);
            }else if (Ele_surplus_int>=80 && Ele_surplus_int<90){
                pixmap = m_IconBatterList.at(8);
            }else if (Ele_surplus_int>=90 && Ele_surplus_int<100){
                pixmap = m_IconBatterList.at(9);
            }else if (Ele_surplus_int == 100){
                pixmap = m_IconBatterList.at(10);
            }
            pixmap = m_pPictureToWhite->drawSymbolicColoredPixmap(pixmap);
            m_pPowericon->setPixmap(drawBackground(pixmap) );
            QString Ele_surplus=obj_reply.value().toString();

            QString filename = QString("%1%2%").arg(Elect_surplus).arg(Ele_surplus);
            QStyleOption opt;
            opt.init(m_pPowerLabel);
            QColor color = opt.palette.color(QPalette::Text);
            color.setAlphaF(0.45);
            QPalette palette = m_pPowerLabel->palette();
            palette.setBrush(QPalette::WindowText, color);
            m_pPowerLabel->setPalette(palette);
            m_pPowerLabel->setText(filename);
        }
    }
}

QPixmap Powershow::drawBackground(const QPixmap& pixmap)
{
    QPixmap temp(pixmap.size());
    temp.fill(Qt::transparent);

    QPainter p1(&temp);
    p1.setCompositionMode(QPainter::CompositionMode_Source);
    p1.drawPixmap(0, 0, pixmap);
    p1.setCompositionMode(QPainter::CompositionMode_DestinationIn);
    p1.fillRect(temp.rect(), QColor(0, 0, 0, 125));
    p1.end();
    return temp;
}

void Powershow::getStyleChageSlots(QString key)
{
    if (key == "styleName") {
        QTime dieTime = QTime::currentTime().addMSecs(500);
        while( QTime::currentTime() < dieTime )
        QCoreApplication::processEvents(QEventLoop::AllEvents, 100);

        initDbusValue();
    }
    return;
}


