/*
PolyGun

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

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.
*/

#include "game/renderer/legacy_gl/legacy_gl_gui_renderer.hpp"

#include "game/renderer/master_renderer.hpp"
#include "game/renderer/mesh_renderer.hpp"
#include "game/renderer/texture.hpp"
#include "game/renderer/gl/opengl.hpp"
#include "game/renderer/legacy_gl/legacy_gl_mesh_renderer.hpp"

using namespace polygun::renderer;

LegacyGLGUIRenderer::LegacyGLGUIRenderer(MasterRenderer* master_renderer) :
	GUIRenderer(master_renderer)
{}

void LegacyGLGUIRenderer::on_viewport_resize(int width, int height) {
	GUIRenderer::on_viewport_resize(width, height);
	glViewport(0, 0, width, height);
}

void LegacyGLGUIRenderer::render_texture(const glm::vec2& pos, Texture* texture, const glm::vec2& scale, float angle, const glm::vec4& color) {
	MeshRenderer* mesh_renderer = m_master_renderer->get_mesh_renderer();
	mesh_renderer->set_camera(&m_camera);
	mesh_renderer->set_shader(m_textured_shader);
	mesh_renderer->set_alpha_blending_mode(true);
	mesh_renderer->set_3d_rendering_mode(false);

	LegacyGLMeshRenderer* gl_mesh_renderer = static_cast<LegacyGLMeshRenderer*>(mesh_renderer);
	glEnableClientState(GL_COLOR_ARRAY);
	gl_mesh_renderer->set_color_mod(color, m_rect_mesh);

	mesh_renderer->translate(pos);
	mesh_renderer->scale(scale * glm::vec2(texture->get_width(), texture->get_height()));
	mesh_renderer->rotate(glm::vec2(1, 0), angle);
	mesh_renderer->render_textured(m_rect_mesh, texture);
	glDisableClientState(GL_COLOR_ARRAY);
	mesh_renderer->set_alpha_blending_mode(false);
}

void LegacyGLGUIRenderer::render_cropped_texture(const glm::vec2& pos, Texture* texture, const glm::vec2& scale, const math::Rect& crop_rect, float angle, const glm::vec4& color) {
	MeshRenderer* mesh_renderer = m_master_renderer->get_mesh_renderer();
	mesh_renderer->set_camera(&m_camera);
	mesh_renderer->set_shader(m_textured_shader);
	mesh_renderer->set_alpha_blending_mode(true);
	mesh_renderer->set_3d_rendering_mode(false);

	LegacyGLMeshRenderer* gl_mesh_renderer = static_cast<LegacyGLMeshRenderer*>(mesh_renderer);
	glEnableClientState(GL_COLOR_ARRAY);
	gl_mesh_renderer->set_color_mod(color, m_rect_mesh);

	const glm::vec2 texture_size(glm::vec2(texture->get_width(), texture->get_height()));
	mesh_renderer->translate(pos);
	mesh_renderer->scale(scale * crop_rect.constrained(texture_size, texture_size).get_size());
	mesh_renderer->rotate(glm::vec2(1, 0), angle);
	crop_uvs(crop_rect, texture_size);
	mesh_renderer->render_textured(m_rect_mesh, texture);
	glDisableClientState(GL_COLOR_ARRAY);
	restore_uvs();
	mesh_renderer->set_alpha_blending_mode(false);
}
