/* app/KMSubCommand.cpp
 *
 * Copyright (C) 2023 OpenKylin, Inc
 */

#include "KMSubCommand.h"

#include <gio/gio.h>

#include "common/kmtranslation.h"

KMSubCommand::KMSubCommand()
{
}

KMSubCommand::~KMSubCommand()
{
}

void KMSubCommand::enableVfs()
{
    const char *oldEnv = ::getenv("GIO_USE_VFS");
    ::setenv("GIO_USE_VFS", "local", true);

    g_vfs_get_default();

    if (oldEnv)
    {
        ::setenv("GIO_USE_VFS", oldEnv, true);
    }
    else
    {
        ::unsetenv("GIO_USE_VFS");
    }
}

const std::vector<SubCommandEntry>& KMSubCommandFactory::subCommandEntrys()
{
    /**
     * brief: name为空串时，认为是一个子命令分组的开始
     */
    static std::vector<SubCommandEntry> subCommands = {
        { "", _(" Manage installed applications and runtimes") },
        { "install", _("Install an application or runtime") },
        { "uninstall", _("Uninstall an application") },
        { "export", _("Put applications or runtimes onto removable media") },
        { "info", _("Show info for an app or runtime") },

        { "", _("\n Manage running applications") },
        { "run", _("Run an application") },
        { "ps", _("Enumerate running applications") },
        { "kill", _("Stop a running application") },

        { "", _("\n Build applications") },
        { "build-init", _("Initialize a directory for building") },
        { "build", _("Run a build command inside the build dir") },
        { "build-finish", _("Finish a build dir for export") },
        { "build-export", _("Export a build dir to a repository") }
    };

    return subCommands;
}

std::unique_ptr<KMSubCommand> KMSubCommandFactory::createKMSubCommand(const std::string &subCommand)
{
    KMSubCommand *pObjetct = dynamic_cast<KMSubCommand *>(KMDynamicCreator::createObjectById(subCommand));
    return std::unique_ptr<KMSubCommand>(pObjetct);
}
