#include <QDebug>

#include "login1_dbus.h"

static const QString LOGIND_DEST = "org.freedesktop.login1";
static const QString LOGIND_PATH = "/org/freedesktop/login1";
static const QString LOGIND_IFACE = "org.freedesktop.login1.Manager";
static const QString login1SessionInterface = QStringLiteral("org.freedesktop.login1.Session");

static const QString SCREENSAVER_DEST = "org.ukui.ScreenSaver";
static const QString SCREENSAVER_IFACE = "org.ukui.ScreenSaver";

static const QString INHIBIT_WHO = "Kylin Camera";
static const QString INHIBIT_WHY = "Stop Recording when system is shutdown or sleep";
static const QString INHIBIT_MODE = "block";

login1dbusManager::login1dbusManager() :
    m_inhibitShutdownFD(QDBusUnixFileDescriptor()),
    m_inhibitSleepFD(QDBusUnixFileDescriptor())
{
    auto sysBus = QDBusConnection::systemBus();
    sysBus.connect(LOGIND_DEST, LOGIND_PATH,LOGIND_IFACE,
                    QString("PrepareForShutdown"), this, SLOT(login1PrepareForShutdown(bool)));
    sysBus.connect(LOGIND_DEST, LOGIND_PATH,LOGIND_IFACE,
                    QString("PrepareForSleep"), this, SLOT(login1PrepareForSleep(bool)));

#if 0
    //切换用户信号操作
    QDBusInterface loginInterface(LOGIND_DEST,LOGIND_PATH,LOGIND_IFACE,QDBusConnection::systemBus());
    QDBusReply<QDBusObjectPath> sessionPath = loginInterface.call("GetSessionByPID",(quint32)QCoreApplication::applicationPid());
    QString session = sessionPath.value().path();
    QDBusConnection::systemBus().connect(LOGIND_DEST,session,login1SessionInterface,QStringLiteral("Lock"),this,SLOT(changeUserLock()));
    QDBusConnection::systemBus().connect(LOGIND_DEST,session,login1SessionInterface,QStringLiteral("Unlock"),this,SLOT(changeUserUnLock()));
#endif
    //锁屏操作
    auto sesBus = QDBusConnection::sessionBus();
    sesBus.connect(SCREENSAVER_DEST,QString("/"),QString(SCREENSAVER_IFACE),
                   QString("lock"),this,SLOT(screenSaverLocked()));
    sesBus.connect(SCREENSAVER_DEST,QString("/"),QString(SCREENSAVER_IFACE),
                   QString("unlock"),this,SLOT(screenSaverUnlocked()));

}

login1dbusManager::~login1dbusManager()
{

}

//Call this method when start record ...
bool login1dbusManager::inhibit(INHIBIT_TYPE type)
{
    QString what;
    if(type == INHIBIT_SLEEP){
        what = "sleep";
    }else if(type == INHIBIT_SHUTDOWN){
        what = "shutdown";
    }else{
        return false;
    }
    QDBusMessage message = QDBusMessage::createMethodCall("org.freedesktop.login1",
                                                          "/org/freedesktop/login1",
                                                          "org.freedesktop.login1.Manager",
                                                          QStringLiteral("Inhibit"));
    message.setArguments(QVariantList({what,
                                       "Kylin Camera",
                                       INHIBIT_WHY,
                                       QStringLiteral("delay")}));
    QDBusPendingReply<QDBusUnixFileDescriptor> reply = QDBusConnection::systemBus().call(message);
    qDebug() << "xx" << reply.value().isValid() ;
    if(type == INHIBIT_SLEEP){
        reply.value().swap(m_inhibitSleepFD);
    }else if(type == INHIBIT_SHUTDOWN){
        reply.value().swap(m_inhibitShutdownFD);
    }else{
        return false;
    }

    return reply.value().isValid();
}

//Call this method when stop recording ...
void login1dbusManager::unInhibit()
{
    if(m_inhibitSleepFD.isValid() == true)
    {
        m_inhibitSleepFD = QDBusUnixFileDescriptor();
    }
    if(m_inhibitShutdownFD.isValid() == true)
    {
        m_inhibitShutdownFD = QDBusUnixFileDescriptor();
    }
}


void login1dbusManager::login1PrepareForSleep(bool in)
{
    qDebug()<<__FILE__<<":"<<__LINE__<<","<<__FUNCTION__<<
          " "<< in;
    if(in == true){
        emit goingToSleep(in);
    } else {
        qDebug() << "System awake...";
    }
}
void login1dbusManager::login1PrepareForShutdown(bool in)
{
    qDebug()<<__FILE__<<":"<<__LINE__<<","<<__FUNCTION__<<
          " "<< in;
    emit shuttingDown();
}
void login1dbusManager::screenSaverLocked()
{
    qDebug()<<__FILE__<<":"<<__LINE__<<","<<__FUNCTION__;
    emit locking(true);
}
void login1dbusManager::screenSaverUnlocked()
{
    qDebug()<<__FILE__<<":"<<__LINE__<<","<<__FUNCTION__;
    emit locking(false);
}

void login1dbusManager::changeUserLock()
{
    qDebug()<<__FILE__<<":"<<__LINE__<<","<<__FUNCTION__;
    emit changeUserLocking(true);
}

void login1dbusManager::changeUserUnLock()
{
    qDebug()<<__FILE__<<":"<<__LINE__<<","<<__FUNCTION__;
    emit changeUserLocking(false);
}
