#include <luna/CPath.h>
#include <luna/PathParser.h>
#include <luna/ScopeGuard.h>
#include <luna/StringBuilder.h>

Result<PathParser> PathParser::create(const char* path)
{
    char* copy = strdup(path);
    if (!copy) return err(ENOMEM);

    return PathParser { path, copy };
}

PathParser::PathParser(const char* original, char* copy) : m_original(original), m_copy(copy)
{
}

PathParser::PathParser(PathParser&& other) : m_original(other.m_original), m_copy(other.m_copy)
{
    other.m_copy = nullptr;
}

PathParser::~PathParser()
{
    if (m_copy) free_impl(m_copy);
}

Option<const char*> PathParser::next()
{
    char* result = strtok_r(m_already_called_next ? nullptr : m_copy, "/", &m_strtok_saved_state);
    m_already_called_next = true;

    if (!result) return {};

    return result;
}

Result<String> PathParser::basename()
{
    char* copy = strdup(m_original);
    if (!copy) return err(ENOMEM);

    auto guard = make_scope_guard([copy] { free_impl(copy); });

    char* result = ::basename(copy);

    // We must copy this as we cannot rely on the original string.
    return String::from_cstring(result);
}

Result<String> PathParser::dirname()
{
    char* copy = strdup(m_original);
    if (!copy) return err(ENOMEM);

    auto guard = make_scope_guard([copy] { free_impl(copy); });

    char* result = ::dirname(copy);

    // We must copy this as we cannot rely on the original string.
    return String::from_cstring(result);
}

Result<String> PathParser::join(StringView path1, StringView path2)
{
    StringBuilder sb;
    TRY(sb.add(path1));

    if (path1[path1.length() - 1] != '/') TRY(sb.add('/'));

    TRY(sb.add(path2));

    String result = TRY(sb.string());

    return realpath(result.view());
}

Result<String> PathParser::realpath(StringView path)
{
    if (!is_absolute(path)) return String {};

    Vector<StringView> parts;
    PathParser parser = TRY(PathParser::create(path.chars()));

    const char* part;
    while (parser.next().try_set_value(part))
    {
        StringView view = part;
        if (view == ".") continue;
        if (view == "..")
        {
            parts.try_pop();
            continue;
        }

        TRY(parts.try_append(view));
    }

    StringBuilder sb;
    for (const auto& section : parts)
    {
        TRY(sb.add('/'));
        TRY(sb.add(section));
    }

    return sb.string();
}