#pragma once

#include <cstdint>

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

namespace pp::legacy::gl_framebuffer {

inline void query_opengl_integer(std::uint32_t name, std::int32_t* value) noexcept
{
    glGetIntegerv(static_cast<GLenum>(name), reinterpret_cast<GLint*>(value));
}

inline void gen_opengl_framebuffers(std::uint32_t count, std::uint32_t* ids) noexcept
{
    glGenFramebuffers(static_cast<GLsizei>(count), reinterpret_cast<GLuint*>(ids));
}

inline void delete_opengl_framebuffers(std::uint32_t count, const std::uint32_t* ids) noexcept
{
    glDeleteFramebuffers(static_cast<GLsizei>(count), reinterpret_cast<const GLuint*>(ids));
}

inline void bind_opengl_framebuffer(std::uint32_t target, std::uint32_t framebuffer) noexcept
{
    glBindFramebuffer(static_cast<GLenum>(target), static_cast<GLuint>(framebuffer));
}

inline void attach_opengl_framebuffer_texture_2d(
    std::uint32_t target,
    std::uint32_t attachment,
    std::uint32_t texture_target,
    std::uint32_t texture,
    std::int32_t level) noexcept
{
    glFramebufferTexture2D(
        static_cast<GLenum>(target),
        static_cast<GLenum>(attachment),
        static_cast<GLenum>(texture_target),
        static_cast<GLuint>(texture),
        static_cast<GLint>(level));
}

inline std::uint32_t check_opengl_framebuffer_status(std::uint32_t target) noexcept
{
    return static_cast<std::uint32_t>(glCheckFramebufferStatus(static_cast<GLenum>(target)));
}

inline void blit_opengl_framebuffer(
    std::int32_t source_x0,
    std::int32_t source_y0,
    std::int32_t source_x1,
    std::int32_t source_y1,
    std::int32_t destination_x0,
    std::int32_t destination_y0,
    std::int32_t destination_x1,
    std::int32_t destination_y1,
    std::uint32_t mask,
    std::uint32_t filter) noexcept
{
    glBlitFramebuffer(
        static_cast<GLint>(source_x0),
        static_cast<GLint>(source_y0),
        static_cast<GLint>(source_x1),
        static_cast<GLint>(source_y1),
        static_cast<GLint>(destination_x0),
        static_cast<GLint>(destination_y0),
        static_cast<GLint>(destination_x1),
        static_cast<GLint>(destination_y1),
        static_cast<GLbitfield>(mask),
        static_cast<GLenum>(filter));
}

inline void read_opengl_pixels(
    std::int32_t x,
    std::int32_t y,
    std::int32_t width,
    std::int32_t height,
    std::uint32_t pixel_format,
    std::uint32_t component_type,
    void* pixels) noexcept
{
    glReadPixels(
        static_cast<GLint>(x),
        static_cast<GLint>(y),
        static_cast<GLsizei>(width),
        static_cast<GLsizei>(height),
        static_cast<GLenum>(pixel_format),
        static_cast<GLenum>(component_type),
        pixels);
}

inline void copy_opengl_tex_sub_image_2d(
    std::uint32_t target,
    std::int32_t level,
    std::int32_t destination_x,
    std::int32_t destination_y,
    std::int32_t source_x,
    std::int32_t source_y,
    std::int32_t width,
    std::int32_t height) noexcept
{
    glCopyTexSubImage2D(
        static_cast<GLenum>(target),
        static_cast<GLint>(level),
        static_cast<GLint>(destination_x),
        static_cast<GLint>(destination_y),
        static_cast<GLint>(source_x),
        static_cast<GLint>(source_y),
        static_cast<GLsizei>(width),
        static_cast<GLsizei>(height));
}

inline pp::renderer::gl::OpenGlTexture2DReadbackDispatch texture_2d_readback_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlTexture2DReadbackDispatch {
        .bind_texture = pp::legacy::gl_texture::bind_opengl_texture,
        .gen_framebuffers = gen_opengl_framebuffers,
        .get_integer = query_opengl_integer,
        .bind_framebuffer = bind_opengl_framebuffer,
        .framebuffer_texture_2d = attach_opengl_framebuffer_texture_2d,
        .check_framebuffer_status = check_opengl_framebuffer_status,
        .read_pixels = read_opengl_pixels,
        .delete_framebuffers = delete_opengl_framebuffers,
    };
}

inline pp::renderer::gl::OpenGlFramebufferBlitDispatch framebuffer_blit_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlFramebufferBlitDispatch {
        .get_integer = query_opengl_integer,
        .bind_framebuffer = bind_opengl_framebuffer,
        .blit_framebuffer = blit_opengl_framebuffer,
    };
}

inline pp::renderer::gl::OpenGlFramebufferReadbackDispatch framebuffer_readback_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlFramebufferReadbackDispatch {
        .get_integer = query_opengl_integer,
        .bind_framebuffer = bind_opengl_framebuffer,
        .read_pixels = read_opengl_pixels,
    };
}

inline pp::renderer::gl::OpenGlTexture2DFramebufferCopyDispatch framebuffer_to_texture_copy_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlTexture2DFramebufferCopyDispatch {
        .copy_tex_sub_image_2d = copy_opengl_tex_sub_image_2d,
    };
}

inline pp::renderer::gl::OpenGlFramebufferBindDispatch framebuffer_bind_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlFramebufferBindDispatch {
        .get_integer = query_opengl_integer,
        .bind_framebuffer = bind_opengl_framebuffer,
    };
}

inline pp::renderer::gl::OpenGlFramebufferRestoreDispatch framebuffer_restore_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlFramebufferRestoreDispatch {
        .bind_framebuffer = bind_opengl_framebuffer,
    };
}

inline pp::renderer::gl::OpenGlFramebufferDeleteDispatch framebuffer_delete_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlFramebufferDeleteDispatch {
        .delete_framebuffers = delete_opengl_framebuffers,
    };
}

inline pp::renderer::gl::OpenGlRenderTargetFramebufferAllocationDispatch render_target_allocation_dispatch(
    pp::renderer::gl::OpenGlFramebufferRenderbufferFn framebuffer_renderbuffer) noexcept
{
    return pp::renderer::gl::OpenGlRenderTargetFramebufferAllocationDispatch {
        .gen_framebuffers = gen_opengl_framebuffers,
        .get_integer = query_opengl_integer,
        .bind_framebuffer = bind_opengl_framebuffer,
        .framebuffer_texture_2d = attach_opengl_framebuffer_texture_2d,
        .framebuffer_renderbuffer = framebuffer_renderbuffer,
        .check_framebuffer_status = check_opengl_framebuffer_status,
    };
}

} // namespace pp::legacy::gl_framebuffer
