/*
 * 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/>.
 *
**/
#include "sessionwatcher.h"
#include <QDBusInterface>
#include <QDebug>
#include <QFile>
#include <QDBusReply>
#include "definetypes.h"
#include "utils.h"

#define GSETTINGS_SCHEMA_SCREENSAVER "org.ukui.screensaver"

SessionWatcher::SessionWatcher(GSettingsHelper *helper ,QObject *parent)
    : QObject(parent)
    , m_gsettingsHelper(helper)
{
    sessionPath = qgetenv("XDG_SESSION_PATH");

    // defaultFontSize = getDefaultFontSize();
    defaultFontSize = 10;

    QDBusInterface *interface = new QDBusInterface(
                SM_DBUS_SERVICE,
                SM_DBUS_PATH,
                SM_DBUS_INTERFACE,
                QDBusConnection::sessionBus(), this);

    connect(interface, SIGNAL(StatusChanged(unsigned int)),
            this, SLOT(onStatusChanged(unsigned int)));

    QDBusInterface *displayManagerInterface = new QDBusInterface(
                DM_DBUS_SERVICE,
                DM_DBUS_PATH,
                DM_DBUS_INTERFACE,
                QDBusConnection::systemBus(), this);
    connect(displayManagerInterface, SIGNAL(SessionRemoved(QDBusObjectPath)),
            this, SLOT(onSessionRemoved(QDBusObjectPath)));

    QVariant idleEnableVar = m_gsettingsHelper->GetLockScreenConf(KEY_IDLE_ACTIVATION_ENABLED);
    QVariant idleDelayVar = m_gsettingsHelper->GetLockScreenConf(KEY_IDLE_DELAY);


    QVariant idleLock = m_gsettingsHelper->GetLockScreenConf(KEY_IDLE_LOCK);
    // 重置锁屏时间(不处理超时锁屏220620)
    if (!idleLock.isNull()) {
        m_gsettingsHelper->SetLockScreenConf(KEY_IDLE_LOCK, -1);
    }
    
    idleDelay = idleDelayVar.toInt();
    m_idleLock = m_gsettingsHelper->GetLockScreenConf(KEY_IDLE_LOCK).toInt();
    connect(m_gsettingsHelper, &GSettingsHelper::idleLockConfigChanged, this, [=](int value){
        m_idleLock = value;
    });

    QVariant lockTimeout = m_gsettingsHelper->GetSessionConf(KEY_LOCK_TIMEOUT);
    if (!lockTimeout.isNull()) {
        m_nLockTimeout = lockTimeout.toInt();
    }
}

void SessionWatcher::onStatusChanged(unsigned int status)
{
    if(status == SESSION_IDLE) {
        qDebug()<< idleDelay;
        if(idleDelay != -1){
            if(!m_timer){
                m_timer = new QTimer(this);
                connect(m_timer, &QTimer::timeout, this, [&]{
                    Q_EMIT sessionIdle();
                    m_timer->stop();
                });
            }
            int time = (idleDelay - 1)*60000;
            if(time<0)
                time = 0;

            m_timer->start(time);
         }

        if(m_idleLock != -1){
            if(!m_timer2){
                m_timer2 = new QTimer(this);
                connect(m_timer2, &QTimer::timeout, this, [&]{
                    Q_EMIT sessionLockIdle();
                    m_timer2->stop();
                });
            }
            int lockTime = (m_idleLock - 1)*60000;
            if(lockTime<0)
                lockTime = 0;

            m_timer2->start(lockTime);
        }


    }else if(status == SESSION_AVAILABLE){
        if(m_timer && m_timer->isActive()){
            m_timer->stop();
        }
        if(m_timer2 && m_timer2->isActive()){
            m_timer2->stop();
        }
        Q_EMIT sessionIdleExit();
    }
}

void SessionWatcher::onSessionRemoved(const QDBusObjectPath &objectPath)
{
    //如果session注销了，则结束进程
    if(objectPath.path() == sessionPath)
        exit(0);
}

