/*
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/gui_renderer.hpp"

#include "game/renderer/master_renderer.hpp"
#include "game/renderer/mesh.hpp"
#include "game/renderer/shader.hpp"
#include "game/engine/engine.hpp"

using namespace polygun::renderer;

static std::vector<float> rect_vertices = {
	0.0f, 1.0f, 0.0f,
	0.0f, 0.0f, 0.0f,
	1.0f, 0.0f, 0.0f,
	1.0f, 1.0f, 0.0f
};
static std::vector<unsigned> rect_indices = {
	0, 1, 2, 2, 0, 3
};
static std::vector<float> rect_uvs = {
	0.0f, 1.0f,
	0.0f, 0.0f,
	1.0f, 0.0f,
	1.0f, 1.0f
};

GUIRenderer::GUIRenderer(MasterRenderer* master_renderer) :
	m_master_renderer(master_renderer),
	m_textured_shader(master_renderer->create_shader()),
	m_solid_shader(master_renderer->create_shader()),
	m_rect_mesh(master_renderer->create_mesh()),
	m_camera(engine::Engine::get_screen_width(), engine::Engine::get_screen_height())
{
	m_textured_shader->load_from_file("gui_textured");
	m_solid_shader->load_from_file("gui_solid");
	m_rect_mesh->load_from_memory(rect_vertices, rect_indices, rect_uvs);
}

GUIRenderer::~GUIRenderer() {
	delete m_textured_shader;
	delete m_solid_shader;
	delete m_rect_mesh;
}

void GUIRenderer::on_viewport_resize(int width, int height) {
	m_camera.update_view(width, height);
}

void GUIRenderer::fill_rect(const glm::vec2& pos, const glm::vec2& size, const glm::vec4& color, float angle) {
	MeshRenderer* mesh_renderer = m_master_renderer->get_mesh_renderer();
	mesh_renderer->set_camera(&m_camera);
	mesh_renderer->set_shader(m_solid_shader);
	mesh_renderer->set_alpha_blending_mode(true);
	mesh_renderer->set_3d_rendering_mode(false);

	mesh_renderer->translate(pos);
	mesh_renderer->scale(size);
	mesh_renderer->rotate(glm::vec2(1, 0), angle);
	mesh_renderer->render(m_rect_mesh, color);
	mesh_renderer->set_alpha_blending_mode(false);
}

void GUIRenderer::fill_rect(const glm::vec2& pos, const glm::vec2& size, const glm::vec4ub& color, float angle) {
	const glm::vec4 converted_color(color.r/255.0f, color.g/255.0f, color.b/255.0f, color.a/255.0f);
	fill_rect(pos, size, converted_color, angle);
}

void GUIRenderer::crop_uvs(const math::Rect& crop_rect, const glm::vec2& texture_size) {
	math::Rect constrained_rect = crop_rect.constrained(texture_size, texture_size);
	const float texture_left = constrained_rect.get_x()/texture_size.x;
	const float texture_right = constrained_rect.get_right()/texture_size.x;
	const float texture_top = 1.0f-constrained_rect.get_y()/texture_size.y;
	const float texture_bottom = 1.0f-constrained_rect.get_bottom()/texture_size.y;
	const std::vector<float> uvs = {
		texture_left, texture_top,
		texture_left, texture_bottom,
		texture_right, texture_bottom,
		texture_right, texture_top
	};
	m_rect_mesh->load_from_memory(rect_vertices, rect_indices, uvs);
}

void GUIRenderer::restore_uvs() {
	m_rect_mesh->load_from_memory(rect_vertices, rect_indices, rect_uvs);
}
