#include "KMEnv.h"

#include "common/KMError.h"
#include "common/KMLogger.h"
#include "common/KMStorageDir.h"

class KMEnv::Private
{
public:
    std::string m_systemBaseDir;
    std::string m_systemExportsDir;
    std::string m_xdgDataDirs;
};

KMEnv::KMEnv()
    : d(make_shared<Private>())
{
}

KMEnv::~KMEnv() { }

/**
 * @brief 校验环境
 *
 */
void KMEnv::checkEnvironment()
{
    int rows, cols;
    int i;

    /* Don't restarting the session  */
    if (!g_getenv("DBUS_SESSION_BUS_ADDRESS")) // root has't this
    {
        return;
    }

    /* Avoid interfering with kaiming tests */
    if (g_getenv("KAIMING_SYSTEM_DIR") || g_getenv("KAIMING_USER_DIR"))
    {
        return;
    }

    d->m_systemBaseDir = KMStorageDir::getSystemDefaultBaseDirLocation();
    kmlogger.info("m_systemBaseDir: %s", d->m_systemBaseDir.c_str());

    d->m_systemExportsDir = d->m_systemBaseDir + "/share";
    kmlogger.info("m_systemBaseDir: %s", d->m_systemExportsDir.c_str());

    d->m_xdgDataDirs = getXdgDataDirs();
    kmlogger.info("XDG_DATA_DIRS=%s", d->m_xdgDataDirs.c_str());
    if ((std::string::size_type)d->m_xdgDataDirs.find("/opt/kaiming/share") == std::string::npos)
    {
        printf("\n Note that the directory `/opt/kaiming/share` is not in the search path "
               "set by the XDG_DATA_DIRS environment variable, so applications "
               "installed by Kaiming may not appear on your desktop until the "
               "session is restarted.\n");
    }
}

/**
 * @brief 判断当前环境是否以 sudo 或 root 权限运行
 *
 * @return true sudo 或者 root权限
 * @return false 非root 和 sudo 权限
 */
bool KMEnv::isSudoOrRoot()
{
    uid_t euid = geteuid();
    if (euid == 0)
    {
        return true;
    }
    const char *user = std::getenv("USER");
    if (user != nullptr && std::string(user) == "root")
    {
        return true;
    }

    std::cout << _("Insufficient execution permissions, please add 'sudo' and execute again!") << std::endl;

    return false;
}

/**
 * @brief 校验 XDG_DATA_DIRS 合法性
 *
 * @return std::string
 */
std::string KMEnv::getXdgDataDirs()
{
    const char *xdgDataDirs = std::getenv("XDG_DATA_DIRS");
    if (nullptr == xdgDataDirs || *xdgDataDirs == '0')
    {
        return std::string("/usr/share/ukui-wayland:/usr/local/share:/usr/share");
    }

    return std::string(xdgDataDirs);
}

/**
 * @brief 通过环境变量获取自定义远程仓库地址
 *
 *
 * 配置自定义软件源仓库(http://10.41.118.234/repo)实例：
 *      1. 修改 /opt/kaiming/repo/config 的 url=http://10.41.118.234/repo
 *      2. sudo KAIMING_REPO_URL=http://10.41.118.234/repo install app/top.openkylin.Clock/x86_64/stable
 *
 * @return std::string
 */
std::string KMEnv::getRepoUrl()
{
    const char *xdgDataDirs = std::getenv("XDG_DATA_DIRS");
    if (nullptr == xdgDataDirs || *xdgDataDirs == '0')
    {
        return std::string("/usr/share/ukui-wayland:/usr/local/share:/usr/share");
    }

    return std::string(xdgDataDirs);
}

/**
 * @brief 是否通过环境校验
 *
 * @return true 通过环境校验
 * @return false 未通过环境校验
 */
bool KMEnv::passEnvCheck()
{
    // const char *const localeName = "zh_CN.utf-8";
    // std::setlocale(LC_ALL, localeName);
    // std::locale::global(std::locale(localeName));

    // checkEnvironment();

    if (!isSudoOrRoot())
    {
        return false;
    }

    return true;
}
