/*
 * Copyright (C) 2018 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/>.
 *
**/
#ifndef FULLBACKGROUNDWIDGET_H
#define FULLBACKGROUNDWIDGET_H

#ifndef QT_NO_KEYWORDS
#define QT_NO_KEYWORDS
#endif
#include <QGSettings>
#include <QtConcurrent/QtConcurrentRun>
#include <QWidget>
#include "types.h"
#include <QAbstractNativeEventFilter>
#include <QDBusUnixFileDescriptor>
#include <QTimer>
#include "logind.h"
#include "config.h"

void x11_get_screen_size(int *width,int *height);

class LockWidget;
class XEventMonitor;
class MonitorWatcher;
class Configuration;
class QDBusInterface;
class TabletLockWidget;
class QProcess;
class FullBackgroundWidget : public QWidget , public QAbstractNativeEventFilter
{
    Q_OBJECT
public:
    explicit FullBackgroundWidget(QWidget *parent = nullptr);
    void paintEvent(QPaintEvent *event);
    void closeEvent(QCloseEvent *event);
    void showEvent(QShowEvent *event);
    virtual bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) override;
    void mouseMoveEvent(QMouseEvent *e);
    void mousePressEvent(QMouseEvent *e);
    void onScreensaver();    
    void onBlankScreensaver(int nDelay = 0, bool hasLock = true);
    void closeScreensaver();
    void setIsStartup(bool val);

    int RegisteSubWnd(quint64 uWndId);
    int UnRegisteSubWnd(quint64 uWndId);
    QList<quint64> GetSubWndIds();
    bool IsStartupMode();

public Q_SLOTS:
    void onCursorMoved(const QPoint &pos);
    void lock();
    void showLockWidget();
    void showLock();
    void showScreensaver(bool isPreview = false);
    void onSecondRunParam(QString strParam);
    int onSessionStatusChanged(uint status);
    void inhibit();
    void uninhibit();
#ifdef USE_INTEL
    void propertiesChangedSlot(QString, QMap<QString, QVariant>, QStringList);
    void onShowBlackBackGround();
#endif

Q_SIGNALS:
    void StartupModeChanged(bool isStartup);

private:
    void init();
    void clearScreensavers();
    bool eventFilter(QObject *obj, QEvent *event);

    QPixmap getPaddingPixmap(QPixmap pixmap, int width, int height);
//    void checkNumLock();
//    int numberMatch(const QString &key);
    void raiseOtherWnd();
    bool isOtherWnd(int wndId);
    void delayLockScreen();
    void stopDelayLockScreen();
    bool initMultiviewDbus();
    bool isMultiviewHidden();
    void HideMultiview();
    void ShowMultiview();

private Q_SLOTS:
    void onScreenCountChanged(int);
    void onDesktopResized();
    void onGlobalKeyPress(const quint8 &key);
    void onGlobalKeyRelease(int key);
    void onGlobalButtonDrag(int xPos, int yPos);
    void onGlobalButtonPressed(int xPos, int yPos);
    void onPrepareForSleep(bool sleep);
    void switchToLinux();
    void laterActivate();
    void laterOtherActivate();
    void setLockState();
    void killWindow();
    void laterInhibit(bool val);
    void laterStartAuth();
    void onLockScreenTimeout();

private:
    QDBusInterface      *smInterface;
#ifdef USE_INTEL
    TabletLockWidget    *lockWidget;
#else
    LockWidget          *lockWidget;
#endif
    XEventMonitor       *xEventMonitor;
    MonitorWatcher      *monitorWatcher;
    Configuration       *configuration;
    QList<QWidget*>     widgetXScreensaverList;
    QList<QWidget*>     widgetBlackList;
    QList<pid_t>        xscreensaverPidList;
    bool                isLocked;
    bool                lockState;
    ScreenStatus        screenStatus;
    QPixmap             background;
    QDBusUnixFileDescriptor m_inhibitFileDescriptor;
    bool                isPassed;
    bool                m_delay;
    int			isBlank;
    QProcess *process = nullptr;
    bool lockEnabled;
    bool		isStartup = false;
    QFuture<void>    future;

    // 上层窗口管理
    QList<quint64> m_listWndIds;
    QTimer *m_timerLock = nullptr;
    bool m_isAlreadyShowSaver = false;

    // 多任务视图接口
    QDBusInterface *m_dbusIfMultiview = nullptr;
};

#endif // FULLBACKGROUNDWIDGET_H
