#pragma once
#include "util.h"

bool check_uniform_uniqueness();

enum class kShaderUniform : uint16_t
{
    MVP = const_hash("mvp"),
    Tex = const_hash("tex"),
    TexFG = const_hash("tex_fg"),
    TexBG = const_hash("tex_bg"),
    TexMix = const_hash("tex_mix"),
    TexMixA = const_hash("tex_mix_alpha"),
    TexMask = const_hash("tex_mask"),
    TexDual = const_hash("tex_dual"),
    TexStroke = const_hash("tex_stroke"),
    TexPattern = const_hash("tex_pattern"),
    PatternOffset = const_hash("pattern_offset"),
    PatternAlpha = const_hash("pattern_alpha"),
    MixAlpha = const_hash("mix_alpha"),
    Opacity = const_hash("opacity"),
    Wet = const_hash("wet"),
    Lock = const_hash("lock"),
    Col = const_hash("col"),
    Tof = const_hash("tof"),
    Tsz = const_hash("tsz"),
    Alpha = const_hash("alpha"),
    Mask  = const_hash("mask"),
    Resolution = const_hash("resolution"),
    Highlight = const_hash("highlight"),
    BlendMode = const_hash("blend_mode"),
    DualBlendMode = const_hash("dual_blend_mode"),
    Noise = const_hash("noise"),
    Direction = const_hash("dir"),
    UseDual = const_hash("use_dual"),
    UsePattern = const_hash("use_pattern"),
    LightDir = const_hash("light_dir"),
    Mode = const_hash("mode"),
    Ambient = const_hash("ambient"),
    PatternInvert = const_hash("pattern_invert"),
    PatternScale = const_hash("pattern_scale"),
    PatternBright = const_hash("pattern_bright"),
    PatternContrast = const_hash("pattern_contr"),
    PatternDepth = const_hash("pattern_depth"),
    PatternBlendMode = const_hash("patt_blend_mode"),
    Colorize = const_hash("colorize"),
    DualAlpha = const_hash("dual_alpha"),
    UseFragcoord = const_hash("use_fragcoord"),
    DrawOutline = const_hash("draw_outline"),
};

enum class kShader : uint16_t
{
    Color       = const_hash("color"),
    ColorQuad   = const_hash("color-quad"),
    ColorTri    = const_hash("color-tri"),
    ColorHue    = const_hash("color-hue"),
    Texture     = const_hash("texture"),
    TextureMask = const_hash("texture-mask"),
    TextureColorize = const_hash("texture-colorize"),
    TextureAlpha= const_hash("texture-alpha"),
    TextureBlend= const_hash("texture-blend"),
    CompErase   = const_hash("comp-erase"),
    CompDraw    = const_hash("comp-draw"),
    UVs         = const_hash("uvs"),
    UVs_2       = const_hash("uvs2"),
    Font        = const_hash("font"),
    Atlas       = const_hash("atlas"),
    Stroke      = const_hash("stroke"),
    StrokePad   = const_hash("stroke-pad"),
    StrokeDilate= const_hash("stroke-dilate"),
    StrokePreview = const_hash("stroke-preview"),
    Checkerboard= const_hash("checkerboard"),
    Equirect    = const_hash("equirect"),
    BrushStroke = const_hash("brush-stroke"),
    VertexColor = const_hash("vertex-color"),
    Lambert     = const_hash("lambert"),
    LambertLightmap = const_hash("lambert-lightmap"),
    BakeUV      = const_hash("bakeuv"),
};

class Shader
{
    std::map<std::string, struct stat> m_deps;
    std::string m_path;
    std::map<kShaderUniform, GLuint> m_umap;
    GLuint prog;
    std::string read(const std::string& path);
public:
    kShader name;
    void parse_error(const std::string& msg, const std::string& code);
    bool load(const std::string& path);
    bool reload();
    bool create(const std::string& vertex, const std::string& fragment);
    void destroy();
    void use();
    void u_vec4(kShaderUniform id, const glm::vec4& v);
    void u_vec3(kShaderUniform id, const glm::vec3& v);
    void u_vec2(kShaderUniform id, const glm::vec2& v);
    void u_mat4(kShaderUniform id, const glm::mat4& m);
    void u_int(kShaderUniform id, int i);
    void u_int(const char* uniform_name, int i);
    void u_float(kShaderUniform id, float f);
    GLint GetAttribLocation(const char* attribute_name);
};

class ShaderManager
{
    static std::map<kShader, Shader> m_shaders;
    static Shader* m_current;
public:
    static bool ext_framebuffer_fetch;
    static bool ext_float32;
    static bool ext_float32_linear;
    static bool ext_float16;
    static bool ext_map_aligned;
    static bool load(kShader id, const std::string& path);
    static bool reload();
    static bool create(kShader id, const std::string& vertex, const std::string& fragment);
    static void use(kShader id);
    static void use(const char* name);
    static Shader* get(kShader id);
    static void u_vec4(kShaderUniform id, const glm::vec4& v);
    static void u_vec3(kShaderUniform id, const glm::vec3& v);
    static void u_vec2(kShaderUniform id, const glm::vec2& v);
    static void u_mat4(kShaderUniform id, const glm::mat4& m);
    static void u_int(kShaderUniform id, int i);
    static void u_int(const char* name, int i);
    static void u_float(kShaderUniform id, float f);
    static void invalidate();
};
