#include "renderer_api/shader_catalog.h"

#include <array>
#include <string_view>

namespace pp::renderer {

namespace {

constexpr std::array<ShaderCatalogEntry, 25> pano_catalog {
    ShaderCatalogEntry { .name = "texture", .path = "data/shaders/texture.glsl" },
    ShaderCatalogEntry { .name = "texture-alpha", .path = "data/shaders/texture-alpha.glsl" },
    ShaderCatalogEntry { .name = "texture-mask", .path = "data/shaders/texture-mask.glsl" },
    ShaderCatalogEntry { .name = "texture-colorize", .path = "data/shaders/texture-colorize.glsl" },
    ShaderCatalogEntry { .name = "texture-blend", .path = "data/shaders/texture-blend.glsl" },
    ShaderCatalogEntry { .name = "stroke-preview", .path = "data/shaders/stroke-preview.glsl" },
    ShaderCatalogEntry { .name = "comp-erase", .path = "data/shaders/comp-erase.glsl" },
    ShaderCatalogEntry { .name = "comp-draw", .path = "data/shaders/comp-draw.glsl" },
    ShaderCatalogEntry { .name = "color", .path = "data/shaders/color.glsl" },
    ShaderCatalogEntry { .name = "color-quad", .path = "data/shaders/color-quad.glsl" },
    ShaderCatalogEntry { .name = "color-tri", .path = "data/shaders/color-tri.glsl" },
    ShaderCatalogEntry { .name = "color-hue", .path = "data/shaders/color-hue.glsl" },
    ShaderCatalogEntry { .name = "uvs", .path = "data/shaders/uvs.glsl" },
    ShaderCatalogEntry { .name = "font", .path = "data/shaders/font.glsl" },
    ShaderCatalogEntry { .name = "atlas", .path = "data/shaders/atlas.glsl" },
    ShaderCatalogEntry { .name = "stroke", .path = "data/shaders/stroke.glsl" },
    ShaderCatalogEntry { .name = "stroke-pad", .path = "data/shaders/stroke-pad.glsl" },
    ShaderCatalogEntry { .name = "stroke-dilate", .path = "data/shaders/stroke-dilate.glsl" },
    ShaderCatalogEntry { .name = "checkerboard", .path = "data/shaders/checkerboard.glsl" },
    ShaderCatalogEntry { .name = "equirect", .path = "data/shaders/equirect.glsl" },
    ShaderCatalogEntry { .name = "brush-stroke", .path = "data/shaders/stroke-instanced.glsl" },
    ShaderCatalogEntry { .name = "vertex-color", .path = "data/shaders/vertex-color.glsl" },
    ShaderCatalogEntry { .name = "lambert", .path = "data/shaders/lambert.glsl" },
    ShaderCatalogEntry { .name = "lambert-lightmap", .path = "data/shaders/lightmap.glsl" },
    ShaderCatalogEntry { .name = "bakeuv", .path = "data/shaders/bake-uv.glsl" },
};

[[nodiscard]] bool is_empty_c_string(const char* text) noexcept
{
    return text == nullptr || text[0] == '\0';
}

[[nodiscard]] bool has_shader_extension(std::string_view path) noexcept
{
    constexpr std::string_view extension = ".glsl";
    return path.size() >= extension.size()
        && path.substr(path.size() - extension.size()) == extension;
}

}

std::span<const ShaderCatalogEntry> panopainter_shader_catalog() noexcept
{
    return pano_catalog;
}

pp::foundation::Status validate_shader_catalog(std::span<const ShaderCatalogEntry> catalog) noexcept
{
    if (catalog.empty()) {
        return pp::foundation::Status::invalid_argument("shader catalog must not be empty");
    }

    if (catalog.size() > max_shader_catalog_entries) {
        return pp::foundation::Status::out_of_range("shader catalog exceeds the configured limit");
    }

    for (std::size_t i = 0; i < catalog.size(); ++i) {
        const auto& entry = catalog[i];
        if (is_empty_c_string(entry.name)) {
            return pp::foundation::Status::invalid_argument("shader catalog entry name must not be empty");
        }

        if (is_empty_c_string(entry.path)) {
            return pp::foundation::Status::invalid_argument("shader catalog entry path must not be empty");
        }

        if (!has_shader_extension(entry.path)) {
            return pp::foundation::Status::invalid_argument("shader catalog path must end with .glsl");
        }

        for (std::size_t j = i + 1U; j < catalog.size(); ++j) {
            if (std::string_view(entry.name) == std::string_view(catalog[j].name)) {
                return pp::foundation::Status::invalid_argument("shader catalog entry name is duplicated");
            }
        }
    }

    return pp::foundation::Status::success();
}

}
