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

#include "game/gui/layout.hpp"
#include "game/gui/box.hpp"
#include "game/gui/button.hpp"
#include "game/gui/label.hpp"
#include "game/gui/text_edit.hpp"
#include "common/xml/parser.hpp"
#include "common/xml/xml_node.hpp"
#include "common/xml/reader.hpp"

using namespace polygun::gui;

Container::Container(Constraint* x, Constraint* y, Constraint* width, Constraint* height, const std::string& id, Layout* layout) :
	Component(x, y, width, height, id),
	m_layout(layout),
	m_components(),
	m_top_padding(0),
	m_bottom_padding(0),
	m_left_padding(0),
	m_right_padding(0)
{
	m_layout->m_container = this;
}

Container::Container(xml::XMLNode* node) :
	Component(node),
	m_layout(nullptr),
	m_components(),
	m_top_padding(0),
	m_bottom_padding(0),
	m_left_padding(0),
	m_right_padding(0)
{
	xml::XMLReader reader(node);
	std::string val;
	if(reader.get_attribute("layout", val)) {
		m_layout = Layout::from_string(val);
		m_layout->m_container = this;
	}
}

Container::Container() :
	Component(nullptr, nullptr, nullptr, nullptr, "root"),
	m_layout(new FixedLayout),
	m_components(),
	m_top_padding(0),
	m_bottom_padding(0),
	m_left_padding(0),
	m_right_padding(0)
{}

Container::~Container() {
	clear();
	delete m_layout;
}

void Container::add_component(Component* component) {
	m_layout->position_component(component);
	component->set_parent(this);
	m_components.push_back(component);
}

void Container::clear() {
	for(Component* component : m_components)
		delete component;
	m_components.clear();
}

bool Container::load_from_file(const std::string& path, engine::Engine* engine) {
	xml::XMLNode* node;
	if(!xml::parse_file(path, node))
		return false;
	if(node->get_name()!="gui") {
		LOG_ERROR("GUI file '%s' is invalid", path.c_str());
		delete node;
		return false;
	}

	load_container(node, engine);

	delete node;
	return true;
}

void Container::render(renderer::GUIRenderer* renderer) {
	for(Component* component : m_components) {
		// don't draw components that don't fit in container
		if(component->get_right()>get_right() || component->get_bottom()>get_bottom())
			continue;
		component->render(renderer);
	}
}

bool Container::update(const window::Event& event) {
	for(Component* component: m_components) {
		if(component->get_right()>get_right() || component->get_bottom()>get_bottom())
			continue;
		if(component->update(event))
			return true;
	}
	return false;
}

void Container::load_container(xml::XMLNode* node, engine::Engine* engine) {
	Component* prev_component = nullptr;
	for(xml::XMLNode* n = node->next_node(); n; n = node->next_node()) {
		Component* c = nullptr;
		bool parent_set = false;
		if(n->get_name()=="box") {
			c = new Box(engine, n);
			c->set_parent(this);
			static_cast<Box*>(c)->load_container(n, engine);
			parent_set = true;
		}
		else if(n->get_name()=="button") {
			c = new Button(engine, this, n);
			parent_set = true;
		}
		else if(n->get_name()=="container") {
			c = new Container(n);
			c->set_parent(this);
			static_cast<Box*>(c)->load_container(n, engine);
			parent_set = true;
		}
		else if(n->get_name()=="label") {
			c = new Label(engine, this, n);
			parent_set = true;
		}
		else if(n->get_name()=="text_edit")
			c = new TextEdit(engine, n);

		if(c) {
			if(!parent_set)
				c->set_parent(this);
			if(prev_component) {
				c->set_prev_component(prev_component);
				prev_component->set_next_component(c);
			}
			m_layout->position_component(c);
			m_components.push_back(c);
			prev_component = c;
		}
	}
}
