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

#include "blitz/misc/Format.h"
#include "blitz/misc/Log.h"
#include <GL/glew.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("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);
}

void ShaderProgram::LoadProgramFile(const std::string& vertexFile, const std::string& fragmentFile) {
	m_VertexShaderID = static_cast<unsigned int>(LoadShaderFromFile(vertexFile, GL_VERTEX_SHADER));
	m_FragmentShaderID = static_cast<unsigned int>(LoadShaderFromFile(fragmentFile, GL_FRAGMENT_SHADER));
	m_ProgramID = glCreateProgram();
	glAttachShader(m_ProgramID, m_VertexShaderID);
	glAttachShader(m_ProgramID, m_FragmentShaderID);
	glLinkProgram(m_ProgramID);
	glValidateProgram(m_ProgramID);
	GetAllUniformLocation();
}

void ShaderProgram::LoadProgram(const std::string& vertexSource, const std::string& fragmentSource) {
	m_VertexShaderID = static_cast<unsigned int>(LoadShader(vertexSource, GL_VERTEX_SHADER));
	m_FragmentShaderID = static_cast<unsigned int>(LoadShader(fragmentSource, GL_FRAGMENT_SHADER));
	m_ProgramID = glCreateProgram();
	glAttachShader(m_ProgramID, m_VertexShaderID);
	glAttachShader(m_ProgramID, m_FragmentShaderID);
	glLinkProgram(m_ProgramID);
	glValidateProgram(m_ProgramID);
	GetAllUniformLocation();
}

unsigned 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);	// @suppress("Function cannot be resolved")
	glCompileShader(shaderID);
	GLint compilesuccessful;
	glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compilesuccessful);
	if (compilesuccessful == false) {
		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("Could not compile shader !");

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

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

unsigned int ShaderProgram::LoadShaderFromFile(const std::string& file, GLenum type) {
	std::stringstream stream;
	std::ifstream fileStream(file);

	if (fileStream) {
		stream << fileStream.rdbuf();
	} else {
		return 0;
	}

	return LoadShader(stream.str(), type);
}

}  // namespace shader
}  // namespace blitz