#include "CommandLineArgs.h"

#include "UnicodeUtils.h"

#include "Win32BaseIncludes.h"

CommandLineArgs::CommandLineArgs()
    : mArugments(),
      mLaunchPath()
{

}

void CommandLineArgs::initialize(CommandLineArgs* args)
{
#ifdef _WIN32
    int nArgs{ 0 };
    auto szArglist = ::CommandLineToArgvW(::GetCommandLineW(), &nArgs);
    if (szArglist == nullptr)
    {
        return;
    }

    std::vector<std::string> windowsArgs(nArgs);
    for (int idx = 0; idx < nArgs; idx++)
    {
        windowsArgs[idx] = UnicodeUtils::utf16ToUtf8String(szArglist[idx]);
    }
    ::LocalFree(szArglist);

    args->process(windowsArgs);
#else
    (void)args;
#endif
}

std::unique_ptr<CommandLineArgs> CommandLineArgs::Create()
{
    return std::make_unique<CommandLineArgs>();
}

std::filesystem::path CommandLineArgs::getLaunchPath()
{
    return mLaunchPath;
}

void CommandLineArgs::recordLaunchPath()
{
    mLaunchPath = std::filesystem::current_path();
}

void CommandLineArgs::process(int argc, char *argv[])
{
    for(int idx=0; idx<argc; idx++)
    {
        mArugments.push_back(std::string(argv[idx]));
    }
}

void CommandLineArgs::process(const std::vector<std::string>& args)
{
    mArugments = args;
}

std::vector<std::string> CommandLineArgs::getUserArgs() const
{
    std::vector<std::string> user_args;
    for(unsigned idx=1; idx<mArugments.size(); idx++)
    {
        user_args.push_back(mArugments[idx]);
    }
    return user_args;
}

const std::vector<std::string> CommandLineArgs::getArgs() const
{
    return mArugments;
}
