#pragma once

#include <cstdint>
#include <span>

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

namespace pp::legacy::gl_texture {

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

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

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 upload_opengl_texture_2d(
    std::uint32_t target,
    std::int32_t level,
    std::int32_t internal_format,
    std::int32_t width,
    std::int32_t height,
    std::int32_t border,
    std::uint32_t pixel_format,
    std::uint32_t component_type,
    const void* data) noexcept
{
    glTexImage2D(
        static_cast<GLenum>(target),
        static_cast<GLint>(level),
        static_cast<GLint>(internal_format),
        static_cast<GLsizei>(width),
        static_cast<GLsizei>(height),
        static_cast<GLint>(border),
        static_cast<GLenum>(pixel_format),
        static_cast<GLenum>(component_type),
        data);
}

inline void update_opengl_texture_2d_region(
    std::uint32_t target,
    std::int32_t level,
    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,
    const void* data) noexcept
{
    glTexSubImage2D(
        static_cast<GLenum>(target),
        static_cast<GLint>(level),
        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),
        data);
}

inline void set_opengl_texture_parameter_f(std::uint32_t target, std::uint32_t parameter, float value) noexcept
{
    glTexParameterf(static_cast<GLenum>(target), static_cast<GLenum>(parameter), static_cast<GLfloat>(value));
}

inline void generate_opengl_mipmap(std::uint32_t target) noexcept
{
    glGenerateMipmap(static_cast<GLenum>(target));
}

inline pp::renderer::gl::OpenGlTexture2DAllocationDispatch texture_2d_allocation_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlTexture2DAllocationDispatch {
        .gen_textures = gen_opengl_textures,
        .bind_texture = bind_opengl_texture,
        .tex_image_2d = upload_opengl_texture_2d,
    };
}

inline pp::renderer::gl::OpenGlTextureCubeAllocationDispatch texture_cube_allocation_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlTextureCubeAllocationDispatch {
        .gen_textures = gen_opengl_textures,
        .bind_texture = bind_opengl_texture,
        .tex_image_2d = upload_opengl_texture_2d,
    };
}

inline pp::renderer::gl::OpenGlTexture2DDeleteDispatch texture_delete_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlTexture2DDeleteDispatch {
        .delete_textures = delete_opengl_textures,
    };
}

inline pp::renderer::gl::OpenGlTexture2DBindDispatch texture_bind_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlTexture2DBindDispatch {
        .bind_texture = bind_opengl_texture,
    };
}

inline pp::renderer::gl::OpenGlTexture2DUpdateDispatch texture_update_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlTexture2DUpdateDispatch {
        .bind_texture = bind_opengl_texture,
        .tex_sub_image_2d = update_opengl_texture_2d_region,
    };
}

inline pp::renderer::gl::OpenGlTexture2DParameterDispatch texture_parameter_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlTexture2DParameterDispatch {
        .bind_texture = bind_opengl_texture,
        .tex_parameter_f = set_opengl_texture_parameter_f,
    };
}

inline pp::renderer::gl::OpenGlTexture2DMipmapDispatch texture_mipmap_dispatch() noexcept
{
    return pp::renderer::gl::OpenGlTexture2DMipmapDispatch {
        .bind_texture = bind_opengl_texture,
        .generate_mipmap = generate_opengl_mipmap,
    };
}

inline std::uint32_t allocate_texture_2d(
    pp::renderer::gl::OpenGlTexture2DAllocation allocation,
    const char* context)
{
    const auto result = pp::renderer::gl::allocate_opengl_texture_2d(allocation, texture_2d_allocation_dispatch());
    if (!result.ok()) {
        LOG("%s failed because: %s", context, result.status().message);
        return 0U;
    }
    return result.value();
}

inline std::uint32_t allocate_texture_cube(
    pp::renderer::gl::OpenGlTextureCubeAllocation allocation,
    const char* context)
{
    const auto result = pp::renderer::gl::allocate_opengl_texture_cube(
        allocation,
        texture_cube_allocation_dispatch());
    if (!result.ok()) {
        LOG("%s failed because: %s", context, result.status().message);
        return 0U;
    }
    return result.value();
}

inline bool delete_texture_2d(std::uint32_t texture_id, const char* context)
{
    const auto status = pp::renderer::gl::delete_opengl_texture_2d(texture_id, texture_delete_dispatch());
    if (!status.ok()) {
        LOG("%s failed because: %s", context, status.message);
        return false;
    }
    return true;
}

inline bool delete_texture_objects(std::span<const std::uint32_t> texture_ids, const char* context)
{
    const auto status = pp::renderer::gl::delete_opengl_texture_objects(texture_ids, texture_delete_dispatch());
    if (!status.ok()) {
        LOG("%s failed because: %s", context, status.message);
        return false;
    }
    return true;
}

inline bool bind_texture_2d(std::uint32_t texture_id, const char* context)
{
    const auto status = pp::renderer::gl::bind_opengl_texture_2d(texture_id, texture_bind_dispatch());
    if (!status.ok()) {
        LOG("%s failed because: %s", context, status.message);
        return false;
    }
    return true;
}

inline bool bind_texture_cube(std::uint32_t texture_id, const char* context)
{
    const auto status = pp::renderer::gl::bind_opengl_texture_cube(texture_id, texture_bind_dispatch());
    if (!status.ok()) {
        LOG("%s failed because: %s", context, status.message);
        return false;
    }
    return true;
}

inline bool update_texture_2d(pp::renderer::gl::OpenGlTexture2DUpdate update, const char* context)
{
    const auto status = pp::renderer::gl::update_opengl_texture_2d(update, texture_update_dispatch());
    if (!status.ok()) {
        LOG("%s failed because: %s", context, status.message);
        return false;
    }
    return true;
}

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

inline bool generate_texture_2d_mipmaps(std::uint32_t texture_id, const char* context)
{
    const auto status = pp::renderer::gl::generate_opengl_texture_2d_mipmaps(
        texture_id,
        texture_mipmap_dispatch());
    if (!status.ok()) {
        LOG("%s failed because: %s", context, status.message);
        return false;
    }
    return true;
}

} // namespace pp::legacy::gl_texture
