#include "KMBuilderConfig.h"
#include "KMBuilderUtils.h"
#include "common/KMLogger.h"

KMBuilderConfig::KMBuilderConfig()
    : m_configData(make_shared<ConfigData>())
{
}

KMBuilderConfig::~KMBuilderConfig() { }

const vector<shared_ptr<KMBuilderModule>> KMBuilderConfig::getModules()
{
    return m_modules;
}

void KMBuilderConfig::loadCommit()
{
    string sdk = any_cast<string>(m_configData->attributeMap["sdk"]);
    string runtime = any_cast<string>(m_configData->attributeMap["runtime"]);
    string runtime_version = any_cast<string>(m_configData->attributeMap["runtime-version"]);
    string branch = any_cast<string>(m_configData->attributeMap["branch"]);
    string base_version = any_cast<string>(m_configData->attributeMap["base-version"]);
    string default_branch = any_cast<string>(m_configData->attributeMap["default-branch"]);

    m_configData->attributeMap["sdk-commit"] = KMBuilderUtils::getCommandResoult("kaiming", list<string>({ "info", "--show-commit", sdk, runtime_version.length() > 0 ? runtime_version : DEFAULT_BRANCH }));

    m_configData->attributeMap["runtime-commit"] = KMBuilderUtils::getCommandResoult("kaiming", list<string>({ "info", "--show-commit", runtime, runtime_version.length() > 0 ? runtime_version : DEFAULT_BRANCH }));

    m_configData->attributeMap["base-commit"] = KMBuilderUtils::getCommandResoult("kaiming",
                                                                                  list<string>({ "info",
                                                                                                 "--show-commit",
                                                                                                 any_cast<string>(m_configData->attributeMap["base"]),
                                                                                                 base_version.length() > 0       ? base_version
                                                                                                   : branch.length() > 0         ? branch
                                                                                                   : default_branch.length() > 0 ? default_branch
                                                                                                                                 : DEFAULT_BRANCH }));
}