/*
PolyGun

Copyright (c) 2023 kacperks https://kacperks.cubesoftware.xyz
              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/component.hpp"

#include "game/gui/constraint.hpp"
#include "game/gui/container.hpp"
#include "common/xml/reader.hpp"

using namespace polygun::gui;

Component::Component(Constraint* x, Constraint* y, Constraint* width, Constraint* height, const std::string& id) :
	m_x(nullptr),
	m_y(nullptr),
	m_width(nullptr),
	m_height(nullptr),
	m_next_component(nullptr),
	m_prev_component(nullptr),
	m_parent(nullptr),
	m_id(id),
	m_inactive(false),
	m_highlighted(false),
	m_top_margin(0),
	m_bottom_margin(0),
	m_left_margin(0),
	m_right_margin(0)
{
	set_x(x);
	set_y(y);
	set_width(width);
	set_height(height);
}

Component::Component(xml::XMLNode* node) :
	m_x(nullptr),
	m_y(nullptr),
	m_width(nullptr),
	m_height(nullptr),
	m_next_component(nullptr),
	m_prev_component(nullptr),
	m_parent(nullptr),
	m_id(),
	m_inactive(false),
	m_highlighted(false),
	m_top_margin(0),
	m_bottom_margin(0),
	m_left_margin(0),
	m_right_margin(0)
{
	xml::XMLReader reader(node);

	std::string val;
	if(reader.get_attribute("x", val))
		set_x(Constraint::from_string(val, "x"));
	if(reader.get_attribute("y", val))
		set_y(Constraint::from_string(val, "y"));
	if(reader.get_attribute("width", val))
		set_width(Constraint::from_string(val, "width"));
	if(reader.get_attribute("height", val))
		set_height(Constraint::from_string(val, "height"));
	reader.get_attribute("id", m_id);
	// Note: for now top_margin and left_margin doesn't make any effect
	reader.get_attribute("top_margin", m_top_margin);
	reader.get_attribute("bottom_margin", m_bottom_margin);
	reader.get_attribute("left_margin", m_left_margin);
	reader.get_attribute("right_margin", m_right_margin);
}

Component::~Component() {
	if(m_x)
		delete m_x;
	if(m_y)
		delete m_y;
	if(m_width)
		delete m_width;
	if(m_height)
		delete m_height;
}

void Component::set_x(Constraint* x) {
	if(m_x)
		delete m_x;
	m_x = x;
	if(m_x) {
		m_x->set_parent(m_parent);
		m_x->set_component(this);
	}
}

void Component::set_y(Constraint* y) {
	if(m_y)
		delete m_y;
	m_y = y;
	if(m_y) {
		m_y->set_parent(m_parent);
		m_y->set_component(this);
	}
}

void Component::set_width(Constraint* width) {
	if(m_width)
		delete m_width;
	m_width = width;
	if(m_width) {
		m_width->set_parent(m_parent);
		m_width->set_component(this);
	}
}

void Component::set_height(Constraint* height) {
	if(m_height)
		delete m_height;
	m_height = height;
	if(m_height) {
		m_height->set_parent(m_parent);
		m_height->set_component(this);
	}
}

void Component::set_parent(Container* parent) {
	m_parent = parent;
	if(m_x)
		m_x->set_parent(m_parent);
	if(m_y)
		m_y->set_parent(m_parent);
	if(m_width)
		m_width->set_parent(m_parent);
	if(m_height)
		m_height->set_parent(m_parent);
}

float Component::get_x() const {
	return POLYGUN_DEREF_CONSTRAINT(m_x)+(m_parent?(m_parent->get_x()+m_parent->get_left_padding()):0);
}

float Component::get_y() const {
	return POLYGUN_DEREF_CONSTRAINT(m_y)+(m_parent?(m_parent->get_y()+m_parent->get_top_padding()):0);
}

float Component::get_width() const {
	return POLYGUN_DEREF_CONSTRAINT(m_width);
}

float Component::get_height() const {
	return POLYGUN_DEREF_CONSTRAINT(m_height);
}

