/*
* Copyright (C) 2019 Tianjin KYLIN Information Technology 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, 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/&gt;.
*
*/


#ifndef WIDGET_H
#define WIDGET_H

#include <QtWidgets>
#include <QPropertyAnimation>
#include <QScreen>
#include <QGuiApplication>
#include <QPalette>
#include <QSettings>
#include "notification_interface.h"
#include "mostgrandwidget.h"
#include "shortCutPanelInterface.h"
#include "sidebarDbusService.h"
#include "dbusmanager.h"
#include "gsettingmanager.h"
#include "platforminfo.h"
#include "dataprocess.h"

#define  TRAY_ICON           ":/data/images/kylin-tool-box.svg"
#define  TRAY_NULL_ICON      ":/data/images/kylin-tool-box-null.svg"
#define  SETTING_ICON        ":/data/images/application-menu.svg"

#define SIDEBAR_WIDTH                    400
#define SIDEBAR_AND_PANEL_DISTANCE       16
#define ANIMATIONSPEED                   200

#define TRAN_COEFFICIENT 0.7

#define SYSTEM_TRAY_ICON      "System Tray Icon"
#define HIDE_SIDEBAR  "hide Sidebar"
#define SHOW_SIDEBAR "Show Sidebar"


#define SYSTEM_TRAY_ICON_MENU "System Tray Icon Menu"
#define PANEL "Panel"
#define SHELL_COMMAND "shell command"
#define LOSE_FOCUS  "Lose focus"

class Widget : public QWidget
{
    Q_OBJECT
    Q_CLASSINFO("D-Bus Interface", "org.ukui.ukuisidebar")
public:
    explicit Widget(QWidget *parent = nullptr);

    ~Widget();
    enum PanelStatePosition
    {
        PanelDown = 0,
        PanelUp,
        PanelLeft,
        PanelRight
    };

    //主界面
    void registerDbusService();                                                 // 初始化Dbus服务
    void startBackgroundFunction();                                             // 初始化功能
    void initLayout();                                                          // 初始化布局
    void clickTrayFunction(QSystemTrayIcon::ActivationReason reason);           // 托盘点击处理事件
    void updateSidebarPosition(int height,int width);                           // 更新位置信息数据
    void initTranslation();                                                     // 初始化翻译
    void initAimation();                                                        // 初始化动画
    void showAnimation();                                                       // show动作
    void hideAnimation();                                                       // hide动作
    void initConnect();                                                         // 初始化信号连接
    bool loadQuickOperationPlugin();                                            // 加载快捷操作面板
    int  getPanelSize();                                                        // 获取任务栏高度
    int  getPanelPosition();                                                    // 获取任务栏位置

    //系统托盘
    void createAction();                                                        // 连接信号和槽函数，设置其动作;
    void createSystray();                                                       // 设置menu界面、添加动作 和 创建sysytray实例
    void setIcon(QIcon icon);                                                   // 设置图标和提示信息;
    void iconActivated(QSystemTrayIcon::ActivationReason reason);               // 获取点击事件
    bool loadNotificationPlugin();                                              // 加载通知中心插件
    void GetsAvailableAreaScreen();                                             // 获取屏幕可用区域高度
    void MostGrandWidgetCoordinates();                                          // 根据任务栏位置调整侧边栏位置
    void InitializeHomeScreenGeometry();                                        // 初始化主屏的X坐标

    void setoneShotBool(bool state);

protected:
    bool eventFilter(QObject *obj, QEvent *event);                              // 设置过滤事件
    void paintEvent(QPaintEvent *event);                                        // 设置主题窗口背景颜色

private:
    //主界面
    QVBoxLayout*                m_pMainQVBoxLayout;                             // 主界面垂直布局器
    QDBusInterface*             m_pServiceInterface;                            // 获取任务栏的高度

    bool                        m_bShowFlag = false ;                            // 侧边栏界面是否展示的标志位，false为隐藏
    bool                        m_oneShotBool = false ;
    int                         m_nScreenWidth;                                 // 屏幕分辨率的宽
    int                         m_nScreenHeight;                                // 屏幕分辨率的高
    int                         m_nScreen_x;                                    // 主屏的起始坐标X
    int                         m_nScreen_y;                                    // 主屏的起始坐标Y

    QObject*                    m_pNotificationPluginObject;                    // 通知中心插件对象

    QTranslator*                m_pTranslator;
    QPropertyAnimation*         m_pAnimationShow;
    QPropertyAnimation*         m_pAnimationHide;
    QDesktopWidget*             m_pDeskWgt;                                    // 桌面分辨率监听等


    //系统托盘
    QSystemTrayIcon*            m_trayIcon;
    QMenu*                      m_trayIconMenu;
    QAction*                    m_open;
    QAction*                    m_openSetUp;

    void hideSidebar();
    void showSidebar();

    SidebarDbusService*         m_dbusService;                                  // 侧边栏dbus
    shortCutPanelInterface*     m_pShortCutPanelInterface;                      // 快捷操作面板插件接口对象

public slots:
    void sidebaractive();

private slots :
    void onResolutionChanged(const QRect argc);                                 // 当改变屏幕分辨率时重新获取屏幕分辨率
    void onVirtualGeometryChanged(const QRect argc);                            // 当屏幕位置变化时，不隐藏，只调整位置
    void onNewNotification();                                                   // 当没展开时，来了新通知才提示
    void hideAnimationFinish();                                                 // 隐藏动画完成
    void showAnimationAction(const QVariant &value);                            // 展开动画开始
    void primaryScreenChangedSLot();                                            // 主屏发生变化
    void screenCountChangedSlots(int count);                                    // 屏幕数量改变时对应槽函数
    void OpenSidebarSlots();                                                    // 打开侧边栏
    void OpenControlCenterSettings();                                           // 打开控制中心的通知中心
    void ClickPanelHideSidebarSlots();                                          // 左键点击任务栏时隐藏侧边栏
    void bootOptionsFilter(QString opt);                                        // 过滤当前的终端命令
    void showAnimationFinish();

signals:
    void startRun(QSystemTrayIcon::ActivationReason reason);

};

#endif // WIDGET_H
