#pragma once

#include "shader.h"
#include "util.h"

#include <functional>

namespace pp::panopainter {

struct LegacyStrokeCompositePatternUniforms {
    glm::vec2 scale {};
    float invert = 0.0f;
    float brightness = 0.0f;
    float contrast = 0.0f;
    float depth = 0.0f;
    int blend_mode = 0;
    glm::vec2 offset {};
};

struct LegacyStrokeCompositeUniforms {
    glm::vec2 resolution {};
    LegacyStrokeCompositePatternUniforms pattern;
    glm::mat4 mvp { 1.0f };
    int texture_slot = 0;
    int stroke_texture_slot = 1;
    int mask_texture_slot = 2;
    int dual_texture_slot = 3;
    int pattern_texture_slot = 4;
    float layer_alpha = 1.0f;
    bool alpha_lock = false;
    bool mask_enabled = false;
    bool use_fragcoord = false;
    int blend_mode = 0;
    bool use_dual = false;
    int dual_blend_mode = 0;
    float dual_alpha = 0.0f;
    bool use_pattern = false;
};

struct LegacyStrokeCompositeShaderExecution {
    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::vec2&)> set_vec2;
    std::function<void(kShaderUniform, const glm::mat4&)> set_mat4;
};

[[nodiscard]] inline LegacyStrokeCompositeShaderExecution legacy_shader_manager_stroke_composite_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_vec2 = [](kShaderUniform uniform, const glm::vec2& value) { ShaderManager::u_vec2(uniform, value); },
        .set_mat4 = [](kShaderUniform uniform, const glm::mat4& value) { ShaderManager::u_mat4(uniform, value); },
    };
}

inline void apply_legacy_stroke_composite_pattern_uniforms(
    const LegacyStrokeCompositePatternUniforms& uniforms,
    const LegacyStrokeCompositeShaderExecution& execution) noexcept
{
    execution.set_vec2(kShaderUniform::PatternScale, uniforms.scale);
    execution.set_float(kShaderUniform::PatternInvert, uniforms.invert);
    execution.set_float(kShaderUniform::PatternBright, uniforms.brightness);
    execution.set_float(kShaderUniform::PatternContrast, uniforms.contrast);
    execution.set_float(kShaderUniform::PatternDepth, uniforms.depth);
    execution.set_int(kShaderUniform::PatternBlendMode, uniforms.blend_mode);
    execution.set_vec2(kShaderUniform::PatternOffset, uniforms.offset);
}

inline void setup_legacy_stroke_composite_shader(
    const LegacyStrokeCompositeUniforms& uniforms,
    const LegacyStrokeCompositeShaderExecution& execution) noexcept
{
    execution.use_shader(kShader::CompDraw);
    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::TexDual, uniforms.dual_texture_slot);
    execution.set_int(kShaderUniform::TexPattern, uniforms.pattern_texture_slot);
    execution.set_vec2(kShaderUniform::Resolution, uniforms.resolution);
    execution.set_float(kShaderUniform::Alpha, uniforms.layer_alpha);
    execution.set_int(kShaderUniform::Lock, uniforms.alpha_lock);
    execution.set_int(kShaderUniform::Mask, uniforms.mask_enabled);
    execution.set_int(kShaderUniform::UseFragcoord, uniforms.use_fragcoord);
    execution.set_int(kShaderUniform::BlendMode, uniforms.blend_mode);
    execution.set_mat4(kShaderUniform::MVP, uniforms.mvp);
    execution.set_int(kShaderUniform::UseDual, uniforms.use_dual);
    execution.set_int(kShaderUniform::UsePattern, uniforms.use_pattern);
    execution.set_int(kShaderUniform::DualBlendMode, uniforms.dual_blend_mode);
    apply_legacy_stroke_composite_pattern_uniforms(uniforms.pattern, execution);
    execution.set_float(kShaderUniform::DualAlpha, uniforms.dual_alpha);
}

inline void setup_legacy_stroke_composite_shader(const LegacyStrokeCompositeUniforms& uniforms)
{
    setup_legacy_stroke_composite_shader(uniforms, legacy_shader_manager_stroke_composite_execution());
}

} // namespace pp::panopainter
