#pragma once

#include <cstdint>
#include <span>

#include "log.h"
#include "renderer_gl/opengl_capabilities.h"

namespace pp::legacy::gl_sampler {

inline void gen_opengl_samplers(std::uint32_t count, std::uint32_t* ids) noexcept
{
#if USE_SAMPLER
    glGenSamplers(static_cast<GLsizei>(count), reinterpret_cast<GLuint*>(ids));
#else
    for (std::uint32_t i = 0U; i < count; ++i) {
        ids[i] = 0U;
    }
#endif
}

inline void set_opengl_sampler_parameter_i(
    std::uint32_t sampler,
    std::uint32_t parameter,
    std::int32_t value) noexcept
{
#if USE_SAMPLER
    glSamplerParameteri(
        static_cast<GLuint>(sampler),
        static_cast<GLenum>(parameter),
        static_cast<GLint>(value));
#else
    (void)sampler;
    (void)parameter;
    (void)value;
#endif
}

inline void set_opengl_sampler_parameter_fv(
    std::uint32_t sampler,
    std::uint32_t parameter,
    const float* values) noexcept
{
#if USE_SAMPLER && !defined(__GLES__)
    glSamplerParameterfv(
        static_cast<GLuint>(sampler),
        static_cast<GLenum>(parameter),
        values);
#else
    (void)sampler;
    (void)parameter;
    (void)values;
#endif
}

inline void bind_opengl_sampler(std::uint32_t unit, std::uint32_t sampler) noexcept
{
#if USE_SAMPLER
    glBindSampler(static_cast<GLuint>(unit), static_cast<GLuint>(sampler));
#else
    (void)unit;
    (void)sampler;
#endif
}

inline pp::renderer::gl::OpenGlSamplerCreateDispatch sampler_create_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlSamplerCreateDispatch {
        .gen_samplers = gen_opengl_samplers,
        .sampler_parameter_i = set_opengl_sampler_parameter_i,
    };
}

inline pp::renderer::gl::OpenGlSamplerParameterDispatch sampler_parameter_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlSamplerParameterDispatch {
        .sampler_parameter_i = set_opengl_sampler_parameter_i,
    };
}

inline pp::renderer::gl::OpenGlSamplerBorderDispatch sampler_border_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlSamplerBorderDispatch {
        .sampler_parameter_fv = set_opengl_sampler_parameter_fv,
    };
}

inline pp::renderer::gl::OpenGlSamplerBindDispatch sampler_bind_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlSamplerBindDispatch {
        .bind_sampler = bind_opengl_sampler,
    };
}

inline std::uint32_t create_sampler(
    std::span<const pp::renderer::gl::OpenGlTextureParameter> parameters,
    const char* context)
{
    const auto result = pp::renderer::gl::create_opengl_sampler(parameters, sampler_create_dispatch());
    if (!result.ok()) {
        LOG("%s failed because: %s", context, result.status().message);
        return 0U;
    }
    return result.value();
}

inline bool set_sampler_parameters(
    std::uint32_t sampler_id,
    std::span<const pp::renderer::gl::OpenGlTextureParameter> parameters,
    const char* context)
{
    const auto status = pp::renderer::gl::set_opengl_sampler_parameters(
        sampler_id,
        parameters,
        sampler_parameter_dispatch());
    if (!status.ok()) {
        LOG("%s failed because: %s", context, status.message);
        return false;
    }
    return true;
}

inline bool set_sampler_border_color(
    std::uint32_t sampler_id,
    std::uint32_t parameter,
    const float* rgba,
    const char* context)
{
    const auto status = pp::renderer::gl::set_opengl_sampler_border_color(
        sampler_id,
        parameter,
        rgba,
        sampler_border_dispatch());
    if (!status.ok()) {
        LOG("%s failed because: %s", context, status.message);
        return false;
    }
    return true;
}

inline bool bind_sampler(std::uint32_t unit, std::uint32_t sampler_id, const char* context)
{
    const auto status = pp::renderer::gl::bind_opengl_sampler_object(
        unit,
        sampler_id,
        sampler_bind_dispatch());
    if (!status.ok()) {
        LOG("%s failed because: %s", context, status.message);
        return false;
    }
    return true;
}

} // namespace pp::legacy::gl_sampler
