/*
 * 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 LOCKWIDGET_H
#define LOCKWIDGET_H

#ifndef QT_NO_KEYWORDS
#define QT_NO_KEYWORDS
#endif

#include <QWidget>
#include <QResizeEvent>
#include <QDBusArgument>
#include <QTabWidget>
#include <QPluginLoader>
#include <QFuture>
#include <kylin-nm/kynetworkicon.h>
#include "kylin-nm/kylin-nm-interface.h"
#include "surewindow.h"
#include "lockchecker.h"
#include "xeventmonitor.h"
#include "batterywidget.h"
#include "libinputswitchevent.h"

namespace Ui {
class LockWidget;
}


struct userInfo {
    int userId;
    QString userName;
    QDBusObjectPath userPath;
};

QDBusArgument &operator <<(QDBusArgument &arg, const userInfo &usersInfo);
const QDBusArgument &operator >>(const QDBusArgument &arg, userInfo &usersInfo);

class VirtualKeyboardWidget;
class PowerManager;
class AuthDialog;
class Configuration;
class Users;
class UserItem;
class DisplayManager;
class QMenu;
class QScrollArea;
class KylinNM;

enum TABAT {
    EMPTY = -1,
    LINEEDIT = 0,
    ENTERBTN,
    BIOBTN,
    BOTTMBTN,
    POWERMANAGER,
    MESSAGEBTN,
};

enum HORIZONBTN {
    BATTERYBTN = 0,
    SWITCHBTN,
    NETWORKBTN,
    KEYBOARDBTN,
    POWERBTN,
};

class LockWidget : public QWidget
{
    Q_OBJECT

public:
    explicit LockWidget(QWidget *parent = 0);
    ~LockWidget();
    void closeEvent(QCloseEvent *event);
    void startAuth();
    void stopAuth();
    void setX11Focus();
    bool exitSubWidget();
    void setStartupMode(bool mode);
    void onActiveLineedit();
    QString getBatteryIconName();
    void key_enter_release(int key);

Q_SIGNALS:
    void closed();
    void keyGlobalRelease(int key);
//    void capsLockChanged();

private:
    void initUI();
    void initUserMenu();
    void setVirkeyboardPos();
    void updateNetIcon(int status);
    bool getLoadStatus(const QString &name);
    int getNetStatus();
    void key_OB_release(int key);
    void key_tab_release(int key);
    void key_shiftTab_release();
    void key_LR_release(int key);
    int getLoginedNum();
    void loadNetPlugin();
    void setBottomBtnSheet();
    void setCheckedSheet(int type, bool show);
    void netResetLocation();

    /**
     * @brief IsDesktopStarted 桌面是否已启动
     * @return
     */
    bool IsDesktopStarted();

    /**
     * @brief setRootWindow 设置X背景
     */
    void setRootWindow();

private Q_SLOTS:
    void onUserAdded(const UserItem &user);
    void onUserDeleted(const UserItem &user);
    void onUserMenuTrigged(QAction *action);
    void showVirtualKeyboard();
    void netInPutStatus();
    void showPowerManager(bool keynavigation = false);
    void showUserMenu();
    void showNetManager(bool keynavigation = false);
    void showBattery();
    void switchToGreeter();
    void switchToSureDialog(int type);
    void showWarning(QVector<InhibitInfo::InhibitorInfo> &list, int type);
    void hideSureDialog();
    void confirmClicked();
    void onClickPassword();
    void hideBottomPlugins();
    void resetNavigation();
    void setBatteryIcon(QString str);
    void dealMessage(QDBusMessage);
    void onShowMessageBtn(bool is_show);
    /**
     * @brief onAuthenticateCompete 认证完成
     */
    void onAuthenticateCompete();

protected:
     bool eventFilter(QObject *obj, QEvent *event);
     void resizeEvent(QResizeEvent *event);
     void keyReleaseEvent(QKeyEvent *e);

private:
     Ui::LockWidget      *ui;
     AuthDialog          *authDialog;
     Configuration       *configuration;
     VirtualKeyboardWidget     *vKeyboard = nullptr;
     PowerManager        *powermanager = nullptr;
     SureWindow          *sureWidget = nullptr;
     QTimer              *timer;
     QMenu               *usersMenu = nullptr;
     QList<QWidget *>     list;
     Users               *users;
     DisplayManager      *displayManager;
     int                 timeType;
     QString             dateType;
     QScrollArea         *scrollArea;
     QWidget             *scrollContents;

     QWidget 		 *m_kylinNM = nullptr;
     BatteryWidget       *mBatteryWidget = nullptr;
     QTabWidget          *mkylinNM = nullptr;
     QWidget             *m_NetManagerWidget;
     QStringList         m_loginedUser;
     bool		 isNetFinished = false;
     int                 powermanagerType;
     int                nowAt = -1;
     int                loginedNum = 0;
     QPushButton        *btnNetworkManager = nullptr;
     bool m_isStartupMode = false;
     bool               is_switchBtn = true;
     bool               is_batteryBtn = true;
     bool               is_keynavigation =false;
     int                tabAt = LINEEDIT;
     int                horAT = BATTERYBTN;
     bool                at_power = false;
     bool               at_plugins = false;

     QDBusInterface *batInterface = nullptr;
     QDBusInterface *iface = nullptr;
     QDBusInterface *dface = nullptr;


     //  监听键盘插拔
     LibinputSwitchEvent *libswitch = nullptr;
     bool isTableMode = false;
     bool m_isShowKeyboard = false; // 打开电源管理界面前虚拟键盘是否已打开
    
    double                curFontSize = 0;
    QFuture<void>    m_futureLoadDeskBg;
};

#endif // LOCKWIDGET_H
