#include "ShaderManager.h"

ShaderManager* ShaderManager::m_pInstance = 0;

ShaderManager::ShaderManager()
{

}

ShaderManager *ShaderManager::getInstance() 
{
	if (m_pInstance == 0) 
	{
		m_pInstance = new ShaderManager();
	}
	return m_pInstance;
}

void ShaderManager::compileProgram(GLSLProgram *program, string sShaderName)
{
	std::string sLog;
	if (!program->vertex_->isCompiled())
	{
		program->vertex_->getShaderLog(sLog);
		return;
	}
	if (!program->fragment_->isCompiled())
	{	
		program->fragment_->getShaderLog(sLog);
		return;
	}
}

void ShaderManager::initialize()
{
	loadShader("HorizontalBlur", "Data/Shaders/v_blur.glsl","Data/Shaders/f_HBlur.glsl");
	loadShader("VerticalBlur", "Data/Shaders/v_blur.glsl","Data/Shaders/f_VBlur.glsl");
	loadShader("GBuffer", "Data/Shaders/v_GBuffer.glsl","Data/Shaders/f_GBuffer.glsl");
	loadShader("Edge", "Data/Shaders/v_edge.glsl","Data/Shaders/f_edge.glsl");
	loadShader("Light", "Data/Shaders/v_edgeLight.glsl","Data/Shaders/f_edgeLight.glsl");
	loadShader("Final", "Data/Shaders/v_final.glsl","Data/Shaders/f_final.glsl");
}

void ShaderManager::loadShader(string sName, string sVertexShader, string sFragmentShader)
{
	GLSLProgram *glslProgram = new GLSLProgram(sVertexShader,sFragmentShader);
	compileProgram(glslProgram, sName);
	m_shaders[sName] = glslProgram;
}

void ShaderManager::useShader(string sName)
{
	m_shaders[sName]->use();
}

void ShaderManager::disableShader(string sName)
{
	m_shaders[sName]->disable();
}

void ShaderManager::deleteShader(string sName)
{
	delete m_shaders[sName];
}

void ShaderManager::deleteAllShaders()
{
	m_shaders.clear();
}

GLSLProgram *ShaderManager::getShader(string sName) 
{ 
	return m_shaders[sName]; 
}

map<string,GLSLProgram *> *ShaderManager::getShaders() 
{
	return &m_shaders;
}