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

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

using namespace polygun::gui;

Label::Label(engine::Engine* engine, Constraint* x, Constraint* y, Constraint* width, Constraint* height, const std::string& id, const LabelProp& prop) :
	Component(x, y, width, height, id),
	StyleChangeListener(),
	m_properties(prop),
	m_text_texture(engine->get_master_renderer()->create_texture())
{
	set_text(prop.m_text);
}

Label::Label(engine::Engine* engine, Container* parent, xml::XMLNode* node) :
	Component(node),
	StyleChangeListener(),
	m_properties(),
	m_text_texture(engine->get_master_renderer()->create_texture())
{
	set_parent(parent);
	xml::XMLReader reader(node);
	std::string val;
	if(!reader.get_attribute("text_size", m_properties.m_text_size))
		m_properties.m_text_size = 15;
	if(reader.get_attribute("text", val))
		set_text(val);
	else if(reader.get_attribute("tr_text", val))
		set_text(_(val));
}

Label::~Label() {
	delete m_text_texture;
}

void Label::set_text(const std::string& text) {
	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;
	if(width>0 && height>0)
		surf = Style::get().m_font.render_text_in_area(glm::vec2(width, height), text, m_properties.m_text_size, color);
	else {
		surf = Style::get().m_font.render_text(text, m_properties.m_text_size, color);
		set_width(new FixedConstraint(surf->get_width()));
		set_height(new FixedConstraint(surf->get_height()));
	}
	m_text_texture->load_from_surface(*surf);
	m_properties.m_text = text;
}

void Label::render(renderer::GUIRenderer* renderer) {
	renderer->render_texture(glm::vec2(get_x(), get_y()), m_text_texture);
}

bool Label::update(const window::Event& event) { return false; }

void Label::on_style_change() {
	set_text(m_properties.m_text);
}
