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

#include "game/gui/container.hpp"
#include "game/gui/constraint.hpp"

using namespace polygun::gui;

Layout::Layout() :
	m_container(nullptr)
{}

Layout* Layout::from_string(const std::string& str) {
	if(str=="fixed")
		return new FixedLayout;
	else if(str=="vertical")
		return new StackLayout(StackLayout::Direction::DIRECTION_VERTICAL);
	else if(str=="horizontal")
		return new StackLayout(StackLayout::Direction::DIRECTION_HORIZONTAL);
	else
		LOG_ERROR("Unknown layout '%s', using fixed instead", str.c_str());
	return new FixedLayout;
}

void FixedLayout::position_component(Component* component) {}

StackLayout::StackLayout(Direction direction) :
	m_direction(direction),
	m_segments(),
	m_current_x(0),
	m_current_y(0),
	m_repositioning(false)
{
	m_segments.push_back(0);
}

void StackLayout::position_component(Component* component) {
	switch(m_direction) {
	case Direction::DIRECTION_VERTICAL: {
		float* segment = &m_segments.back();
		const float width = component->get_width();
		if(m_current_y+component->get_height()>m_container->get_height()-m_container->get_bottom_padding()) {
			m_current_y = 0;
			m_current_x+=(*segment)+3;
			m_segments.push_back(0);
			segment = &m_segments.back();
		}
		if(width>*segment)
			*segment = width;
		if(!m_repositioning)
			m_components.push_back(component);
		component->set_x(new FixedConstraint(m_current_x));
		component->set_y(new FixedConstraint(m_current_y));
		m_current_y+=component->get_height()+2;
		break;
	}
	case Direction::DIRECTION_HORIZONTAL: {
		float* segment = &m_segments.back();
		const float height = component->get_height();
		if(m_current_x+component->get_width()>m_container->get_width()-m_container->get_right_padding()) {
			m_current_x = 0;
			m_current_y+=(*segment)+2;
			m_segments.push_back(0);
			segment = &m_segments.back();
		}
		if(height>*segment)
			*segment = height;
		if(!m_repositioning)
			m_components.push_back(component);
		component->set_x(new FixedConstraint(m_current_x));
		component->set_y(new FixedConstraint(m_current_y));
		m_current_x+=component->get_width()+3;
		break;
	}
	}
}

void StackLayout::reposition_components() {
	m_repositioning = true;
	m_segments.clear();
	m_segments.push_back(0);
	for(Component* component : m_components)
		position_component(component);
	m_repositioning = false;
}
