/*
 * Copyright (C) 2025, 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 3 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 this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * Authors: liuliang <liuliang@kylinos.cn>
 *
 */

#include <QCoreApplication>

#include <QCommandLineOption>
#include <QCommandLineParser>

#include <QDBusConnection>
#include <QDBusError>
#include <QDebug>

#include "systemservicelauncher.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QCommandLineParser parser;
    auto helpOption = parser.addHelpOption();

    QCommandLineOption pluginPathOption(QStringList() << "p" << "plugin-path");
    pluginPathOption.setValueName(QObject::tr("plugin path"));
    pluginPathOption.setDescription(QObject::tr("Specific the service plugin path where should be loaded."));
    QCommandLineOption spaceNameOption(QStringList() << "n" << "service-space");
    spaceNameOption.setValueName(QObject::tr("space name"));
    spaceNameOption.setDescription(QObject::tr("Specific the process with a name which service should be running with."));
    QCommandLineOption residentOption(QStringList() << "r" << "resident");
    residentOption.setValueName(QObject::tr("resident"));
    residentOption.setDescription(QObject::tr("Specific wether the services should be runned residently, default is yes."));
    residentOption.setDefaultValue("yes");
    QCommandLineOption servicesOption(QStringList() << "s" << "services");
    servicesOption.setValueName(QObject::tr("services"));
    servicesOption.setDescription(QObject::tr("Specific the services to be registered, splited with ';'."));
    QCommandLineOption argsOption(QStringList() << "a" << "args");
    argsOption.setValueName(QObject::tr("service args"));
    argsOption.setDescription(QObject::tr("Specific the service arguments to be processed, splited with whitespace, this option must be set after other options."));

    parser.addOption(pluginPathOption);
    parser.addOption(spaceNameOption);
    parser.addOption(residentOption);
    parser.addOption(servicesOption);
    parser.addOption(argsOption);

    if (!parser.parse(a.arguments())) {
        qWarning() << parser.errorText();
        return -1;
    }

    if (parser.isSet(helpOption)) {
        parser.showHelp();
        return 0;
    }

    QString spaceName = "Default";
    if (parser.isSet(spaceNameOption)) {
        QString tmp = parser.value(spaceNameOption);
        if (!tmp.isEmpty()) {
            // fixme：需要进行space名称检测
            spaceName = tmp;
        }
    }

    QStringList args = QStringList();
    if (parser.isSet(argsOption)) {
        args = parser.values(argsOption);
    }

    SystemServiceLauncher launcher;
    QString servicePath = "/org/ukui/ServiceSpace/" + spaceName;
    QString serviceInterface = "org.ukui.ServiceSpace." + spaceName;
    QDBusConnection::systemBus().registerObject(servicePath, serviceInterface, &launcher, QDBusConnection::ExportAllSlots);

    bool resident = true;
    if (parser.isSet(residentOption)) {
        QString result = parser.value(residentOption).toLower();
        resident = (result == "yes" || result == "y" || result == "true");
    }

    QString pluginPath;
    if (parser.isSet(pluginPathOption)) {
        pluginPath = parser.value(pluginPathOption);
    }

    QStringList serviceNames;
    if (parser.isSet(servicesOption)) {
        serviceNames = parser.values(servicesOption);
    }

    // 注册空间服务
    QString spaceServiceName = QString("org.ukui.Service.%1").arg(spaceName);
    if (!QDBusConnection::systemBus().registerService(spaceServiceName)) {
        qWarning() << QDBusConnection::systemBus().lastError();
        return -1;
    }

    if (pluginPath.isEmpty() || serviceNames.isEmpty()) {
        qDebug() << "启动时未加载服务，参数为：" << a.arguments();
    } else {
        launcher.launchServices(pluginPath, serviceNames, resident, args);
    }

    return a.exec();
}
