/*
 * 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/>.
 */

#ifndef POWERWINDOW_H
#define POWERWINDOW_H

#include <QWidget>
#include <QStyleOption>
#include <QPainter>
#include <QDebug>
#include <QGSettings>
#include <QGuiApplication>
#include <QScreen>
#include <QLabel>
#include <QVBoxLayout>
#include <QListWidgetItem>
#include <QSlider>
#include <QMouseEvent>
#include <QProcess>
#include <QFont>
#include <QPushButton>
#include <QToolButton>

#include "kylable.h"
#include "kslider.h"
#include "divider.h"
#include "kwindowsystem.h"
#include "customstyle.h"
#include "UKUIGSettings/ukui-qgsettings.h"
#include "upm-gsettings/upm_gsettings.h"
#include "upower-dbus/upm_upowerdbus.h"

#define PT_PANEL_GSETTINGS              "org.ukui.panel.settings"
#define PT_PANEL_TYPE                   "paneltype"
#define PT_PANEL_DATA_POSITION          "dataislandposition"
#define PT_PANEL_SETTINGS_POSITION      "settingsislandposition"
#define PT_PANEL_TOP_BAR_SIZE           "topbarsize"
#define PT_PANEL_SIZE                   "panelsize"
#define PT_PANEL_LENGTH                 "panellength"
#define PT_PANEL_LAYOUT_DIRECTION       "layoutDirection"
#define PT_PANEL_POSITION               "panelposition"


using namespace kdk;

class powerwindow : public QWidget
{
    Q_OBJECT

public:
    explicit powerwindow(QWidget *parent = nullptr);
    ~powerwindow();

    void setIconName(QString iconName);
    void setAcOnlineState(bool acOnlineState);
    void setBatteryState(int iBatteryState);
    void setBatteryPercentage(int iBatteryPercentage);
    void setShowPowerLeftTime(int iShowPowerLeftTime);
    void setBatteryLeftTime(qlonglong timeToFull, qlonglong timeToEmpty);
    void setPowerStateLabelForAcOnlineChanged();
    void setPowerStateLabelForBatteryInfoChanged();
    void setPercentageLabelFontsize();
    void showPowerWindow();
    void slideWindowByPanelPosition(int panelPosition);

private :
    bool         m_isWayland = false;

    int          m_batteryState = battery_state_last;
    bool         m_batteryStateChanged = false;
    int          m_batteryPercentage;
    bool         m_acOnlineState = true;
    bool         m_acOnlineChange;
    int          m_showBatteryLeftTime;
    qint64       m_timeToFull;
    qint64       m_timeToEmpty;
    QString      m_iconName;
    int          m_powerPolicyAc;
    int          m_powerPolicyBattery;

    QRect        m_windowPositionRect;
    double       m_sysFontSize = 10;
    double       m_transparency = 0.75;

    QVBoxLayout  *m_pmainlayout    = nullptr;
    QHBoxLayout  *m_toplayout      = nullptr;
    QHBoxLayout  *m_firstlayout    = nullptr;
    QHBoxLayout  *m_lastlayout     = nullptr;
    QHBoxLayout  *m_statelayout    = nullptr;

    QWidget      *m_firstwidget;
    QWidget      *m_lastWidget;
    QWidget      *m_topwidget;
    QWidget      *m_statewidget;

    QPushButton  *m_iconButton;
    QLabel       *m_percentageLabel;
    QLabel       *m_powerStateLabel;
    QLabel       *m_toplabel;
    QToolButton  *m_enduranceIconButtun;
    QToolButton  *m_performanceIconButtun;

    Divider      *m_segmentationLine_1;
    Divider      *m_segmentationLine_2;

    KSlider      *m_StateSlider;
    KyLable      *m_settingLabel;

    UKUIGSettings *m_pPanelGsetings = nullptr;
    QMap<QString, uint> m_panelSettings;
    bool m_specialLanguage;

    enum powerPolicy
    {
        Performance = 0,
        Balance,
        EnergySaving,
    };
    enum PanelPosition
    {
        panel_bottom = 0,
        panel_top,
        panel_left,
        panel_right
    };
    const uint MARGIN = 8;
private:
    void initUI();
    void initPlatformType();
    void resetWindowPosition();
    void setWindowProperty();
    void setSliderValue();
    QRect getCurrentOutputRect();
    bool eventFilter(QObject *watched, QEvent *event);
    void keyPressEvent(QKeyEvent *event);

    bool specialLanguage();
    void initPanelSettings(QString key = "");
    QString getCurrentQScreenName();

private Q_SLOTS:
    void sliderValueChanged(int);
    void onPanelGsettingsChanged(QString key);

protected:
    void paintEvent(QPaintEvent *event);
};

#endif // POWERWINDOW_H
