/*
PolyGun

Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>
              2023 kacperks

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#ifndef POLYGUN_RENDERER_GL_SHADER_HPP
#define POLYGUN_RENDERER_GL_SHADER_HPP

#include "game/renderer/shader.hpp"

#include <glm/glm.hpp>
#include <map>

#include "game/renderer/gl/opengl.hpp"

namespace polygun::renderer {
	class GLShader final : public Shader {
	public:
		GLShader();
		virtual ~GLShader() override;

		void bind();

		void set_uniform(const GLchar* u_name, unsigned int value);
		void set_uniform(const GLchar* u_name, int value);
		void set_uniform(const GLchar* u_name, GLfloat value);
		void set_uniform(const GLchar* u_name, GLfloat x, GLfloat y);
		void set_uniform(const GLchar* u_name, const glm::vec2& vector);
		void set_uniform(const GLchar* u_name, GLfloat x, GLfloat y, GLfloat z);
		void set_uniform(const GLchar* u_name, const glm::vec3& vector);
		void set_uniform(const GLchar* u_name, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
		void set_uniform(const GLchar* u_name, const glm::vec4& vector);
		void set_uniform(const GLchar* u_name, const glm::mat4& mtx);
		void set_uniform(const GLchar* u_name, GLuint tex2d, GLint unit); // sample 2d
		GLuint get_attrib_location(const GLchar* a_name);

		virtual bool load_from_file(const std::string& name) override;

	private:
		GLuint m_program;
		std::map<std::string, GLint> m_uniform_locations;
		std::map<std::string, GLuint> m_attrib_locations;

	private:
		GLint get_uniform_location(const GLchar* u_name);
		bool check_compile_errors(GLuint shader, const std::string& type);
	};
}

#endif // POLYGUN_RENDERER_GL_SHADER_HPP
