#pragma once

#include "shader.h"

#include <functional>

namespace pp::panopainter {

struct LegacyStrokeEraseUniforms {
    glm::mat4 mvp { 1.0f };
    int texture_slot = 0;
    int stroke_texture_slot = 1;
    int mask_texture_slot = 2;
    float alpha = 1.0f;
    bool mask_enabled = false;
};

struct LegacyStrokeEraseShaderExecution {
    std::function<void(kShader)> use_shader;
    std::function<void(kShaderUniform, int)> set_int;
    std::function<void(kShaderUniform, float)> set_float;
    std::function<void(kShaderUniform, const glm::mat4&)> set_mat4;
};

[[nodiscard]] inline LegacyStrokeEraseShaderExecution legacy_shader_manager_stroke_erase_execution() noexcept
{
    return {
        .use_shader = [](kShader shader) { ShaderManager::use(shader); },
        .set_int = [](kShaderUniform uniform, int value) { ShaderManager::u_int(uniform, value); },
        .set_float = [](kShaderUniform uniform, float value) { ShaderManager::u_float(uniform, value); },
        .set_mat4 = [](kShaderUniform uniform, const glm::mat4& value) { ShaderManager::u_mat4(uniform, value); },
    };
}

inline void setup_legacy_stroke_erase_shader(
    const LegacyStrokeEraseUniforms& uniforms,
    const LegacyStrokeEraseShaderExecution& execution) noexcept
{
    execution.use_shader(kShader::CompErase);
    execution.set_int(kShaderUniform::Tex, uniforms.texture_slot);
    execution.set_int(kShaderUniform::TexStroke, uniforms.stroke_texture_slot);
    execution.set_int(kShaderUniform::TexMask, uniforms.mask_texture_slot);
    execution.set_int(kShaderUniform::Mask, uniforms.mask_enabled);
    execution.set_float(kShaderUniform::Alpha, uniforms.alpha);
    execution.set_mat4(kShaderUniform::MVP, uniforms.mvp);
}

inline void setup_legacy_stroke_erase_shader(const LegacyStrokeEraseUniforms& uniforms)
{
    setup_legacy_stroke_erase_shader(uniforms, legacy_shader_manager_stroke_erase_execution());
}

} // namespace pp::panopainter
