#pragma once

#include <array>
#include <cstdint>

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

namespace pp::legacy::ui_gl {

inline void enable_opengl_state(std::uint32_t state) noexcept
{
    glEnable(static_cast<GLenum>(state));
}

inline void disable_opengl_state(std::uint32_t state) noexcept
{
    glDisable(static_cast<GLenum>(state));
}

inline void clear_opengl_buffer(std::uint32_t mask) noexcept
{
    glClear(static_cast<GLbitfield>(mask));
}

inline void set_opengl_clear_color(float r, float g, float b, float a) noexcept
{
    glClearColor(r, g, b, a);
}

inline void set_opengl_viewport(std::int32_t x, std::int32_t y, std::int32_t width, std::int32_t height) noexcept
{
    glViewport(static_cast<GLint>(x), static_cast<GLint>(y), static_cast<GLsizei>(width), static_cast<GLsizei>(height));
}

inline void bind_opengl_texture(std::uint32_t target, std::uint32_t texture) noexcept
{
    glBindTexture(static_cast<GLenum>(target), static_cast<GLuint>(texture));
}

inline void get_opengl_integer(std::uint32_t name, std::int32_t* values) noexcept
{
    GLint raw_values[4] {};
    glGetIntegerv(static_cast<GLenum>(name), raw_values);
    values[0] = static_cast<std::int32_t>(raw_values[0]);
    values[1] = static_cast<std::int32_t>(raw_values[1]);
    values[2] = static_cast<std::int32_t>(raw_values[2]);
    values[3] = static_cast<std::int32_t>(raw_values[3]);
}

inline void get_opengl_float(std::uint32_t name, float* values) noexcept
{
    glGetFloatv(static_cast<GLenum>(name), values);
}

inline std::array<std::int32_t, 4> query_viewport(const char* context)
{
    std::array<std::int32_t, 4> viewport {};
    get_opengl_integer(pp::renderer::gl::viewport_query(), viewport.data());
    (void)context;
    return viewport;
}

inline std::array<float, 4> query_clear_color(const char* context)
{
    std::array<float, 4> color {};
    get_opengl_float(pp::renderer::gl::color_clear_value_query(), color.data());
    (void)context;
    return color;
}

inline void set_capability(std::uint32_t state, bool enabled, const char* context)
{
    const auto status = pp::renderer::gl::apply_opengl_capability(
        state,
        enabled,
        pp::renderer::gl::OpenGlCapabilityDispatch {
            .enable = enable_opengl_state,
            .disable = disable_opengl_state,
        });
    if (!status.ok())
        LOG("%s capability dispatch failed because: %s", context, status.message);
}

inline void set_blend_enabled(bool enabled, const char* context)
{
    set_capability(pp::renderer::gl::blend_state(), enabled, context);
}

inline void apply_viewport(
    std::int32_t x,
    std::int32_t y,
    std::int32_t width,
    std::int32_t height,
    const char* context)
{
    const auto status = pp::renderer::gl::apply_opengl_viewport(
        pp::renderer::gl::OpenGlViewportRect {
            .x = x,
            .y = y,
            .width = width,
            .height = height,
        },
        pp::renderer::gl::OpenGlViewportDispatch {
            .viewport = set_opengl_viewport,
        });
    if (!status.ok())
        LOG("%s viewport dispatch failed because: %s", context, status.message);
}

inline void clear_color_buffer(std::array<float, 4> color, const char* context)
{
    const auto status = pp::renderer::gl::clear_opengl_render_target(
        pp::renderer::gl::OpenGlDefaultClear {
            .color = color,
            .mask = pp::renderer::gl::framebuffer_color_buffer_mask(),
        },
        pp::renderer::gl::OpenGlClearDispatch {
            .clear_color = set_opengl_clear_color,
            .clear = clear_opengl_buffer,
        });
    if (!status.ok())
        LOG("%s clear dispatch failed because: %s", context, status.message);
}

inline void set_clear_color(std::array<float, 4> color, const char* context)
{
    const auto status = pp::renderer::gl::clear_opengl_render_target(
        pp::renderer::gl::OpenGlDefaultClear {
            .color = color,
            .mask = pp::renderer::gl::framebuffer_color_buffer_mask(),
        },
        pp::renderer::gl::OpenGlClearDispatch {
            .clear_color = set_opengl_clear_color,
            .clear = [](std::uint32_t) noexcept {},
        });
    if (!status.ok())
        LOG("%s clear-color dispatch failed because: %s", context, status.message);
}

inline void unbind_texture_2d(const char* context)
{
    const auto status = pp::renderer::gl::bind_opengl_texture_2d(
        0U,
        pp::renderer::gl::OpenGlTexture2DBindDispatch {
            .bind_texture = bind_opengl_texture,
        });
    if (!status.ok())
        LOG("%s texture bind dispatch failed because: %s", context, status.message);
}

} // namespace pp::legacy::ui_gl
