#include "client/render/shader/ShaderProgram.h"

#include "blitz/misc/Format.h"
#include "blitz/misc/Log.h"
#include "client/render/OpenGL.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>

namespace blitz {
namespace shader {

ShaderProgram::ShaderProgram() : m_ProgramID(0), m_VertexShaderID(0), m_FragmentShaderID(0) {}

ShaderProgram::~ShaderProgram() {
	CleanUp();
}

void ShaderProgram::Start() const {
	glUseProgram(m_ProgramID);
}

void ShaderProgram::Stop() const {
	glUseProgram(0);
}

int ShaderProgram::GetUniformLocation(const std::string& uniformName) const {
	const int location = glGetUniformLocation(m_ProgramID, uniformName.c_str());
	if (location == -1) {
		utils::LOGD(utils::Format("[ShaderProgram] Warning ! Uniform variable %s not found !", uniformName.c_str()));
	}
	return location;
}

void ShaderProgram::LoadFloat(unsigned int location, float value) const {
	glUniform1f(static_cast<GLint>(location), value);
}

void ShaderProgram::LoadInt(unsigned int location, int value) const {
	glUniform1i(static_cast<GLint>(location), value);
}

void ShaderProgram::LoadVector(unsigned int location, const Vec2f& vector) const {
	glUniform2f(static_cast<GLint>(location), vector.x, vector.y);
}

void ShaderProgram::LoadVector(unsigned int location, const Vec3f& vector) const {
	glUniform3f(static_cast<GLint>(location), vector.x, vector.y, vector.z);
}

void ShaderProgram::LoadBoolean(unsigned int location, bool value) const {
	glUniform1i(static_cast<GLint>(location), value);
}

void ShaderProgram::LoadMat4(unsigned int location, const Mat4f& mat) const {
	glUniformMatrix4fv(static_cast<GLint>(location), 1, false, reinterpret_cast<const float*>(&mat));
}

void ShaderProgram::CleanUp() const {
	Stop();
	glDetachShader(m_ProgramID, m_VertexShaderID);
	glDetachShader(m_ProgramID, m_FragmentShaderID);
	glDeleteShader(m_VertexShaderID);
	glDeleteShader(m_FragmentShaderID);
	glDeleteProgram(m_ProgramID);
}

bool ShaderProgram::LoadProgram(const std::string& vertexSource, const std::string& fragmentSource) {
	int shaderID;

	if ((shaderID = LoadShader(vertexSource, GL_VERTEX_SHADER)) < 0)
		return false;
	m_VertexShaderID = static_cast<unsigned int>(shaderID);

	if ((shaderID = LoadShader(fragmentSource, GL_FRAGMENT_SHADER)) < 0)
		return false;
	m_FragmentShaderID = static_cast<unsigned int>(shaderID);

	m_ProgramID = glCreateProgram();
	glAttachShader(m_ProgramID, m_VertexShaderID);
	glAttachShader(m_ProgramID, m_FragmentShaderID);
	glLinkProgram(m_ProgramID);
	glValidateProgram(m_ProgramID);
	GetAllUniformLocation();

	return true;
}

int ShaderProgram::LoadShader(const std::string& source, GLenum type) {
	unsigned int shaderID = glCreateShader(type);

	const char* c_str = source.c_str();
	int* null = 0;
	glShaderSource(shaderID, 1, &c_str, null);
	glCompileShader(shaderID);
	GLint compilesuccessful;
	glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compilesuccessful);
	if (!compilesuccessful) {
		GLsizei size;
		glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &size);
		std::vector<char> shaderError(static_cast<std::size_t>(size));
		glGetShaderInfoLog(shaderID, size, &size, shaderError.data());

		utils::LOGE("[ShaderProgram] Could not compile shader !");

		utils::LOGE(shaderError.data());

		utils::LOGD(utils::Format("\nShader source : \n"
								  "---------------------------------------------------------------------------------------------------"
								  "---------------------------------\n"
								  "%s\n"
								  "---------------------------------------------------------------------------------------------------"
								  "---------------------------------\n",
			source.c_str()));

		return -1;
	}
	return shaderID;
}

std::string ShaderProgram::GetShaderHeader() {
#ifdef __ANDROID__
	return "#version 300 es\nprecision mediump float;\n";
#else
	return "#version 330\n";
#endif
}

}  // namespace shader
}  // namespace blitz