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

#include "game/engine/engine.hpp"
#include "game/gui/style.hpp"
#include "game/renderer/texture.hpp"
#include "common/xml/reader.hpp"
#include "common/locale/util.hpp"

using namespace polygun::gui;

Box::Box(engine::Engine* engine, Constraint* x, Constraint* y, Constraint* width, Constraint* height, const std::string& id, Layout* layout, const BoxProp& prop) :
	Container(x, y, width, height, id, layout),
	StyleChangeListener(),
	m_properties(prop),
	m_title_texture(engine->get_master_renderer()->create_texture())
{
	set_title(prop.m_title);
	m_left_padding = 5;
}

Box::Box(engine::Engine* engine, xml::XMLNode* node) :
	Container(node),
	StyleChangeListener(),
	m_properties(),
	m_title_texture(engine->get_master_renderer()->create_texture())
{
	xml::XMLReader reader(node);
	std::string val;
	if(reader.get_attribute("text", val))
		set_title(val);
	else if(reader.get_attribute("tr_text", val))
		set_title(_(val));
	m_left_padding = 5;
}

Box::~Box() {
	delete m_title_texture;
}

void Box::set_title(const std::string& title) {
	std::unique_ptr<renderer::Surface> surf;
	const float width = get_width();
	const float height = get_height();
	const glm::vec4ub& color = m_inactive?Style::get().m_inactive_text_color:Style::get().m_text_color;
	const unsigned title_size = 14;
	if(width>0 && height>0)
		surf = Style::get().m_font.render_text_in_area(glm::vec2(width, height), title, title_size, color);
	else
		surf = Style::get().m_font.render_text(title, title_size, color);
	m_title_texture->load_from_surface(*surf);
	m_properties.m_title = title;
	m_top_padding = m_title_texture->get_height()+15;
}

void Box::render(renderer::GUIRenderer* renderer) {
	renderer->fill_rect(glm::vec2(get_x()-2, get_y()-2), glm::vec2(get_width()+2, get_height()+2), add_to_color(Style::get().m_background_color, glm::vec4ub(100, 100, 100, 0)));
	renderer->fill_rect(glm::vec2(get_x()+2, get_y()+2), glm::vec2(get_width(), get_height()), subtract_from_color(Style::get().m_background_color, glm::vec4ub(40, 40, 40, 0)));
	renderer->fill_rect(glm::vec2(get_x(), get_y()), glm::vec2(get_width(), get_height()), Style::get().m_background_color);
	renderer->render_texture(glm::vec2(get_x()+5, get_y()+5), m_title_texture);
	Container::render(renderer);
}

void Box::on_style_change() {
	set_title(m_properties.m_title);
}
