/*
 * Copyright 2022 KylinSoft 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 2 of the License, 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 thi1s program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "appmanager.h"
#include <QDebug>
#include <QDBusConnection>
#include "dbusservices/appmanagerservice.h"
#include "dbusservices/applauncher.h"
#include "dbusservices/whitelistmanager.h"
#include "memorymonitor.h"
#include "dbusservices/applauncher.h"
AppManager::AppManager(QObject *parent)
    : QObject(parent)
    , m_service(new AppManagerService(this))
    , m_appLauncher(new AppLauncher(this))
    , m_whiteListManager(new WhiteListManager(this))
    , m_statusManagerInterface(new com::kylin::statusmanager::interface("com.kylin.statusmanager.interface", "/",
                                                                        QDBusConnection::sessionBus(), this))
    #if 0
    , m_memMonitor(new MemoryMonitor(this))
    #endif
{

    QDBusConnection connection = QDBusConnection::sessionBus();
    if (!connection.registerService("com.kylin.AppManager") ||
            !connection.registerObject("/com/kylin/AppManager", this)) {
        qWarning() << "register dbus service failed" << connection.lastError();
    }
    qDebug() << "--&AppLauncher::startSuccess,---";

    int counter = 0;
    while (true) {
        qDebug() << "===counter===" <<counter;

        QDBusInterface iface("com.kylin.statusmanager.interface",
                             "/",
                             "com.kylin.statusmanager.interface",
                              QDBusConnection::sessionBus());

        if (counter >= 10000) {
            qDebug() << "m_statusManagerInterface emit startFailed();";
            Q_EMIT startFailed();
            break;
        }

        if (iface.isValid()) {
            qDebug() << "m_statusManagerInterface emit startSuccess();";
            Q_EMIT startSuccess();
            break;
        }
        counter ++;
        QThread::msleep(1);
        qApp->processEvents();
    }


#if 0
    connect(m_memMonitor, &MemoryMonitor::lowMemoryWaining,
            m_appLauncher, &AppLauncher::killCachedApp);
#endif
}

bool AppManager::LaunchApp(const QString &desktopFile)
{
    return m_appLauncher->LaunchApp(desktopFile);
}

bool AppManager::LaunchAppWithArguments(const QString &desktopFile,const QStringList &args)
{
    return m_appLauncher->LaunchAppWithArguments(desktopFile, args);
}

bool AppManager::LaunchXdgFile(const QString &desktopFile, const QString &fileName)
{
    return m_appLauncher->LaunchXdgFile(desktopFile, fileName);
}

bool AppManager::LaunchDefaultAppWithUrl(const QString &url)
{
    return m_appLauncher->LaunchDefaultAppWithUrl(url);
}

void AppManager::SetPowerSavingModeEnable(bool enable)
{
    m_appLauncher->setPowerSavingModeEnable(enable);
}

bool AppManager::AddToWhiteList(const QString &desktopFile, const QString &option)
{
    return m_whiteListManager->addToWhiteList(desktopFile, option);
}

bool AppManager::RemoveFromWhiteList(const QString &desktopFile, const QString &option)
{
    return m_whiteListManager->removeFromWhiteList(desktopFile, option);
}

bool AppManager::ActiveProcessByWid(const uint &wid)
{
    return m_appLauncher->ActiveProcessByWid(wid);
}

QStringList AppManager::WhiteListsOfApp(const QString &desktopFile)
{
    return m_whiteListManager->whiteListsOfApp(desktopFile);
}

QString AppManager::AppDesktopFileNameByPid(qint64 pid)
{
    return m_appLauncher->AppDesktopFileNameByPid(pid);
}

QString AppManager::AppDesktopFileNameByWid(qint64 wid)
{
    return m_appLauncher->AppDesktopFileNameByWid(wid);
}

#if 0
QList<uint> AppManager::RunningApplications()
{
    return m_appLauncher->RunningApplications();
}
#endif
