#include "jes/Runtime.h"

namespace jes
{
    static const char * const runtime_directories[Runtime::COUNT] = {
        "fonts",
        "shaders",
    };

    PathRef Runtime::locate(int type, const std::string & name)
    {
        PathRef path = Core::instance.get_bin_path()->dirname()->dirname()->join("runtime")->join(runtime_directories[type]);
        PathRef rv = NULL;
        switch (type)
        {
        case Runtime::SHADER:
            rv = path->join(name);
            break;
        case Runtime::FONT:
            {
                static const char * extensions[] = {
                    "ttf",
                };
                std::vector<std::string> dirents = path->dir_entries();
                for (auto de : dirents)
                {
                    PathRef test_path = path->join(de)->join(name);
                    if (test_path->exists())
                        return test_path;
                    for (auto ext : extensions)
                    {
                        test_path = test_path->ext(ext);
                        if (test_path->exists())
                            return test_path;
                    }
                }
            }
            break;
        }

        if ((rv != NULL) && (rv->exists()))
            return rv;

        return NULL;
    }
}
