#pragma once

#include "shader.h"

#include <functional>

namespace pp::panopainter {

struct LegacyStrokePatternUniforms {
    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 LegacyStrokeShaderSetupUniforms {
    glm::vec2 resolution {};
    LegacyStrokePatternUniforms pattern;
    glm::mat4 mvp { 1.0f };
    bool uses_destination_feedback = false;
    bool uses_pattern = false;
    float mix_alpha = 0.0f;
    float wet = 0.0f;
    float noise = 0.0f;
    bool set_opacity = false;
    float opacity = 1.0f;
};

struct LegacyStrokeSampleUniforms {
    glm::vec4 color {};
    float alpha = 0.0f;
    float opacity = 1.0f;
};

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

[[nodiscard]] inline LegacyStrokeShaderExecution legacy_shader_manager_stroke_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_vec4 = [](kShaderUniform uniform, const glm::vec4& value) { ShaderManager::u_vec4(uniform, value); },
        .set_mat4 = [](kShaderUniform uniform, const glm::mat4& value) { ShaderManager::u_mat4(uniform, value); },
    };
}

inline void apply_legacy_stroke_pattern_uniforms(
    const LegacyStrokePatternUniforms& uniforms,
    const LegacyStrokeShaderExecution& 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 use_legacy_stroke_shader(const LegacyStrokeShaderExecution& execution) noexcept
{
    execution.use_shader(kShader::Stroke);
}

inline void use_legacy_stroke_shader()
{
    use_legacy_stroke_shader(legacy_shader_manager_stroke_execution());
}

inline void apply_legacy_stroke_blend_uniforms(
    bool uses_pattern,
    float mix_alpha,
    float wet,
    float noise,
    const LegacyStrokeShaderExecution& execution) noexcept
{
    execution.set_int(kShaderUniform::UsePattern, uses_pattern);
    execution.set_float(kShaderUniform::MixAlpha, mix_alpha);
    execution.set_float(kShaderUniform::Wet, wet);
    execution.set_float(kShaderUniform::Noise, noise);
}

inline void apply_legacy_stroke_blend_uniforms(
    bool uses_pattern,
    float mix_alpha,
    float wet,
    float noise)
{
    apply_legacy_stroke_blend_uniforms(
        uses_pattern,
        mix_alpha,
        wet,
        noise,
        legacy_shader_manager_stroke_execution());
}

inline void setup_legacy_stroke_shader(
    const LegacyStrokeShaderSetupUniforms& uniforms,
    const LegacyStrokeShaderExecution& execution) noexcept
{
    use_legacy_stroke_shader(execution);
    execution.set_int(kShaderUniform::Tex, 0); // brush
    if (uniforms.uses_destination_feedback) {
        execution.set_int(kShaderUniform::TexBG, 1); // bg
    }
    execution.set_int(kShaderUniform::TexPattern, 2); // pattern
    execution.set_int(kShaderUniform::TexMix, 3); // mixer
    // ShaderManager::u_int(kShaderUniform::TexMixA, 4); // mixer
    if (uniforms.set_opacity) {
        execution.set_float(kShaderUniform::Opacity, uniforms.opacity);
    }
    execution.set_vec2(kShaderUniform::Resolution, uniforms.resolution);
    apply_legacy_stroke_pattern_uniforms(uniforms.pattern, execution);
    apply_legacy_stroke_blend_uniforms(
        uniforms.uses_pattern,
        uniforms.mix_alpha,
        uniforms.wet,
        uniforms.noise,
        execution);
    execution.set_mat4(kShaderUniform::MVP, uniforms.mvp);
}

inline void setup_legacy_stroke_shader(const LegacyStrokeShaderSetupUniforms& uniforms)
{
    setup_legacy_stroke_shader(uniforms, legacy_shader_manager_stroke_execution());
}

inline void setup_legacy_stroke_dual_shader(
    bool uses_pattern,
    const LegacyStrokeShaderExecution& execution) noexcept
{
    use_legacy_stroke_shader(execution);
    apply_legacy_stroke_blend_uniforms(uses_pattern, 0.0f, 0.0f, 0.0f, execution);
}

inline void setup_legacy_stroke_dual_shader(bool uses_pattern)
{
    setup_legacy_stroke_dual_shader(uses_pattern, legacy_shader_manager_stroke_execution());
}

inline void apply_legacy_stroke_sample_uniforms(
    const LegacyStrokeSampleUniforms& uniforms,
    const LegacyStrokeShaderExecution& execution) noexcept
{
    execution.set_vec4(kShaderUniform::Col, uniforms.color);
    execution.set_float(kShaderUniform::Alpha, uniforms.alpha);
    execution.set_float(kShaderUniform::Opacity, uniforms.opacity);
}

inline void apply_legacy_stroke_sample_uniforms(const LegacyStrokeSampleUniforms& uniforms)
{
    apply_legacy_stroke_sample_uniforms(uniforms, legacy_shader_manager_stroke_execution());
}

} // namespace pp::panopainter
