#ifndef TRAYWIDGET_H
#define TRAYWIDGET_H

#include <glib.h>
#include <gio/gio.h>
#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLayoutItem>
#include <QLabel>
#include <QPalette>
#include <QStackedWidget>
#include <QPushButton>
#include <QToolButton>
#include <QScrollArea>
#include <QDesktopWidget>
#include <QGSettings>
#include <QScrollBar>
#include <KWindowSystem>
#include <QKeyEvent>

#include "component/bluetoothsettinglabel.h"
#include "component/switchbutton.h"
#include "component/kyhline.h"
#include "component/qdevitem.h"
#include "config/config.h"
#include "config/xatom-helper.h"
#include "pin/pincodewidget.h"
#include "daemon/bluetoothdbus.h"
#include "mainwidget/trayicon.h"

#include "kswitchbutton.h"
using namespace kdk;

#define TRANSPARENCY_SETTINGS       "org.ukui.control-center.personalise"
#define TRANSPARENCY_KEY            "transparency"

class TrayWidget : public QWidget
{
    Q_OBJECT
public:
    static TrayWidget * self(BluetoothDbus *dbus, bool intel = false);
    ~TrayWidget();

    void InitMemberVariables();
    void connectUISignalAndSlot();
    void setBluetoothOpen(bool v);
    void updatePairedDev();
    void layoutAddOnePairedDev(QString name, bool paired);
    void setWidgetPosition();

public slots:
    void GSettingsChanges(const QString &key);
    void showUI();

private slots:
    void openBluetoothSlot(bool);
    void removePairedDevUIByAddress(QString address);
    void layoutAddAllPairedDev();

private:
    //键盘交互
    void kbUDEvent(bool opt);
    void kbTabEvent();
    void kbEnterEvent();
    //界面
    void InitNomalWidget();
    void InitErrorWidget();
    void clearPairedWidget();

    void getTransparency();
    void SendMessage(QString name, bool connected);

    TrayWidget(bool intel, BluetoothDbus *dBus, QWidget *parent = nullptr);
    static TrayWidget* _MSelf;
    TrayIcon *trayicon;

    BluetoothDbus *_SessionDBus;
    Config _config;

    KSwitchButton             *_MBtn = nullptr;
    QStackedWidget *_MStackedWidget = nullptr;
    QLabel                  *_MLoad = nullptr;
    QLabel                 *_MLabel = nullptr;
    QVBoxLayout       *_MMainLayout = nullptr;
    BluetoothSettingLabel       *_MSettingBtn = nullptr;
    QGSettings       *StyleSettings = nullptr;
    QGSettings *transparency_gsettings = nullptr;

    QWidget           *_NomalWidget = nullptr;
    QWidget           *_ErrorWidget = nullptr;
    QWidget          *_PairedWidget = nullptr;

    QVBoxLayout      *_PairedLayout = nullptr;
    QScrollArea        *_ScrollArea = nullptr;
    QTimer               *_MIconTimer = nullptr;
    KyHLine               *lineFrame = nullptr;
    KyHLine               *lineFrame2 = nullptr;
    QMap<QString, QStringList> pairedDevices;
    QList<QString> pairedList;
    int iconFlag = 0;
    double tran =1;
    bool            _Intel;
    bool            MBtnFocused = false;
    int             selfHeight = 0;
    int pressCnt = 0;
    int scrollCnt = 0;
    int listCnt = 0;

signals:
    void trayIconStatusChanged(bool);
    void setManagerUnblocked(bool);

protected:
    void keyPressEvent(QKeyEvent *event);
    void paintEvent(QPaintEvent *event);
    bool eventFilter(QObject *o, QEvent *e);
    void hideEvent(QHideEvent *event);
    void showEvent(QShowEvent *event);
};

#endif // TRAYWIDGET_H
