#include <grp.h>
#include <luna/Units.h>
#include <os/ArgumentParser.h>
#include <os/Directory.h>
#include <os/File.h>
#include <os/FileSystem.h>
#include <pwd.h>

struct UsernameCache
{
    uid_t uid;
    String name;
};

struct GroupCache
{
    gid_t gid;
    String name;
};

Vector<UsernameCache> g_user_cache;
Vector<GroupCache> g_group_cache;

Result<void> find_user_and_group(struct stat& st, StringView& owner, StringView& group)
{
    for (const auto& user : g_user_cache)
    {
        if (user.uid == st.st_uid) owner = user.name.view();
    }
    if (owner.is_empty())
    {
        auto* pw = getpwuid(st.st_uid);
        if (!pw) owner = "???";
        else
            owner = pw->pw_name;

        auto name = TRY(String::from_string_view(owner));
        TRY(g_user_cache.try_append({ st.st_uid, move(name) }));
    }

    for (const auto& grp : g_group_cache)
    {
        if (grp.gid == st.st_gid) group = grp.name.view();
    }
    if (group.is_empty())
    {
        auto* grp = getgrgid(st.st_gid);
        if (!grp) group = "???";
        else
            group = grp->gr_name;

        auto name = TRY(String::from_string_view(group));
        TRY(g_group_cache.try_append({ st.st_gid, move(name) }));
    }

    return {};
}

Result<int> luna_main(int argc, char** argv)
{
    StringView pathname;
    bool show_all { false };
    bool show_almost_all { false };
    bool long_list { false };
    bool human_readable { false };
    bool si { false };
    bool follow_symlink_args { false };

    os::ArgumentParser parser;
    parser.add_description("List files contained in a directory (defaults to '.', the current directory)"_sv);
    parser.add_system_program_info("ls"_sv);
    parser.add_positional_argument(pathname, "directory"_sv, "."_sv);
    parser.add_switch_argument(show_all, 'a', "all"_sv, "also list hidden files (whose filename begins with a dot)"_sv);
    parser.add_switch_argument(show_almost_all, 'A', "almost-all"_sv, "list all files except '.' and '..'"_sv);
    parser.add_switch_argument(long_list, 'l', ""_sv, "use a long listing format"_sv);
    parser.add_switch_argument(human_readable, 'h', "human-readable"_sv,
                               "with -l, show human-readable sizes e.g. 2KiB, 6GiB"_sv);
    parser.add_switch_argument(si, ' ', "si"_sv, "same as -h, but show sizes in powers of 10"_sv);
    parser.add_switch_argument(follow_symlink_args, 'H', "dereference-args"_sv,
                               "follow symbolic links listed as arguments"_sv);
    parser.parse(argc, argv);

    Vector<String> files;
    int dirfd = AT_FDCWD;
    SharedPtr<os::Directory> dir;

    if (os::FileSystem::is_directory(pathname, follow_symlink_args))
    {
        dir = TRY(os::Directory::open(pathname));
        dirfd = dir->fd();

        auto filter = os::Directory::Filter::Hidden;
        if (show_almost_all) filter = os::Directory::Filter::ParentAndBase;
        else if (show_all)
            filter = os::Directory::Filter::None;

        files = TRY(dir->list(filter));
    }
    else if (os::FileSystem::exists(pathname, follow_symlink_args))
    {
        auto str = TRY(String::from_string_view(pathname));
        TRY(files.try_append(move(str)));
    }
    else
        return err(ENOENT);

    if (!long_list)
    {
        auto list = TRY(String::join(files, " "_sv));
        if (!list.is_empty()) os::println("%s", list.chars());
    }
    else
    {
        for (const auto& file : files)
        {
            struct stat st;
            TRY(os::FileSystem::stat({ dirfd, file.view() }, st, false));

            StringView owner;
            StringView group;

            TRY(find_user_and_group(st, owner, group));

            if (!human_readable && !si)
            {
                os::println("%6o %u %4s %4s %10lu %s", st.st_mode, st.st_nlink, owner.chars(), group.chars(),
                            st.st_size, file.chars());
            }
            else
            {
                auto size = TRY(to_dynamic_unit(st.st_size, 10, false, si ? Unit::SI : Unit::Binary, false));
                os::println("%6o %u %4s %4s %6s %s", st.st_mode, st.st_nlink, owner.chars(), group.chars(),
                            size.chars(), file.chars());
            }
        }
    }

    return 0;
}