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

#include <QObject>
#include <QDBusContext>
#include <QProcess>
#include <QTimer>
#include <QDBusUnixFileDescriptor>
#include "types.h"
#include "logind.h"
#include "sessionwatcher.h"

class QGSettings;
class Interface : public QObject, protected QDBusContext
{
    Q_OBJECT

    Q_CLASSINFO("D-Bus Interface", SS_DBUS_SERVICE)

public:
    enum {
        SESSION_STATUS_SCREENSAVER = 1, // 屏保
        SESSION_STATUS_SCREENCLOSE = 2, // 关屏
        SESSION_STATUS_SLEEPED = 4, // 休眠/睡眠
    };
    explicit Interface(QObject *parent = nullptr);
	 LogindIntegration *m_logind;

public Q_SLOTS:
    /**
     * Lock the screen
     */
    void Lock();
    void UnLock();
    void ShowScreensaver();
    bool GetLockState();
    bool GetSlpState();
    void SetLockState();
    void onSessionIdleReceived();
    void onShowScreensaver();
    void onShowBlankScreensaver();
    void onNameLost(const QString&);
    void onPrepareForSleep(bool sleep);
    bool checkScreenDialogRunning();
    void onSessionIdleExit();
    void onScreenClosed(bool state);
    void onLidStateChaned(bool isClosed);
    bool GetBlankState();
    bool LockByBlank(int nType);

private:
    bool checkExistChild();
    void inhibit();
    void uninhibit();
    void emitLockState(bool);
    bool checkStatus(int nStatus);
    /**
     * @brief delayLockScreen 延迟锁屏
     */
    void delayLockScreen();
    /**
     * @brief stopDelayLockScreen 停止延迟锁屏
     */
    void stopDelayLockScreen();
    /**
     * @brief onLockScreenTimeout 延迟锁屏处理过程
     */
    void onLockScreenTimeout();

private:
    bool lockState;
    bool slpState;
    int m_timerCount;
    SessionWatcher	*m_sessionWatcher = nullptr;
    QTimer *m_timer;
    QDBusUnixFileDescriptor m_inhibitFileDescriptor;
    int m_nStatus = 0; // 当前状态
    bool blankState = false;
    bool m_isNewBlankInterface = false;
    QTimer *m_timerLock = nullptr;
private:
    QProcess process;
};

#endif // INTERFACE_H
