#include "GL/gl3w.h"
#include "GL/gl.h"
#include "Shader.hpp"
#include "Debug.hpp"

TSE::uint TSE::GLFW::Shader::activeProgramID = 0;

void TSE::GLFW::Shader::Bind() const
{
    Enable(true);
}

void TSE::GLFW::Shader::Unbind() const
{
    Disable(true);
}

void TSE::GLFW::Shader::Enable(bool notify) const
{
    activeProgramID = programID;
    glUseProgram(programID);
    if(notify) OnEnable();
}

void TSE::GLFW::Shader::Disable(bool notify) const
{
    activeProgramID = 0;
    glUseProgram(0);
    if(notify) OnDisable();
}

void TSE::GLFW::Shader::Flush()
{
    OnFlush();
}

void TSE::GLFW::Shader::DrawCall(int indexCount)
{
    OnDrawCall(indexCount);
}

void TSE::GLFW::Shader::Submit(const Transformable &t, float *&target, TransformationStack &stack, void (*restartDrawcall)(IRenderer &), IRenderer &rnd)
{
    OnSubmit(t, target, stack, restartDrawcall, rnd);
}

bool TSE::GLFW::Shader::IsEnabled() const
{
    return programID == activeProgramID;
}

int TSE::GLFW::Shader::packageSize()
{
    return PackageSize;
}

TSE::GLFW::Shader::Shader(const std::vector<std::unique_ptr<ShaderPart>> &parts)
{
    programID = glCreateProgram();

    for (const auto& part : parts) {
        glAttachShader(programID, part->shaderPartID);
    }

    glLinkProgram(programID);

    int success;

    glGetProgramiv(programID, GL_LINK_STATUS, &success);

    if(!success)
    {
        char log[512];
        glGetProgramInfoLog(programID, 512, nullptr, log);
        TSE_ERROR(log);
    }

    for (const auto& part : parts) {
        glDetachShader(programID, part->shaderPartID);
    }
}

TSE::GLFW::Shader::~Shader()
{
    glDeleteProgram(programID);
}

int TSE::GLFW::Shader::GetUniformLocation(const char *name)
{
    auto it = uniformLocations.find(name);
    if (it != uniformLocations.end()) return it->second;

    int loc = glGetUniformLocation(programID, name);
    uniformLocations[name] = loc;
    return loc;
}

void TSE::GLFW::Shader::SetUniform(const char *name, int value)
{
    glUniform1i(GetUniformLocation(name), value);
}

void TSE::GLFW::Shader::SetUniform(const char *name, const int *value, const int count)
{
    glUniform1iv(GetUniformLocation(name), count, value);
}

void TSE::GLFW::Shader::SetUniform(const char *name, const Matrix4x4 *value)
{
    float colmbMajor[16];
    value->ToArrayColumnMajor(colmbMajor);
    glUniformMatrix4fv(GetUniformLocation(name),1, false, colmbMajor);
}

void TSE::GLFW::Shader::SetUniform(const char *name, float value)
{
    glUniform1f(GetUniformLocation(name), value);
}

void TSE::GLFW::Shader::SetUniform(const char *name, const float *value, const int count)
{
    glUniform1fv(GetUniformLocation(name), count, value);
}

void TSE::GLFW::Shader::SetUniform(const char *name, const Vector2 *value)
{
    glUniform2f(GetUniformLocation(name), value->x, value->y);
}

void TSE::GLFW::Shader::SetUniform(const char *name, const Vector3 *value)
{
    glUniform3f(GetUniformLocation(name), value->x, value->y, value->z);
}

void TSE::GLFW::Shader::SetUniform(const char *name, const Vector4 *value)
{
    glUniform4f(GetUniformLocation(name), value->x, value->y, value->z, value->w);
}
