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

#include <stdexcept>

#include "game/gui/container.hpp"

using namespace polygun::gui;

Constraint* Constraint::from_string(const std::string& str, const std::string& name) {
	if(str.empty())
		return nullptr;
	if(str.back()=='%') {
		if(str.length()==1)
			return nullptr;
		std::string fraction_str = str.substr(0, str.length()-1);
		float fraction;
		try {
			fraction = std::stof(fraction_str);
		}
		catch(const std::invalid_argument& e) {
			LOG_ERROR("Failed to parse number %s: %s", fraction_str.c_str(), e.what());
			return nullptr;
		}
		if(name=="x" || name=="width")
			return new WidthRelConstraint(fraction/100.0f);
		else if(name=="y" || name=="height")
			return new HeightRelConstraint(fraction/100.0f);
		return nullptr;
	}
	if(str=="right")
		return new RightAlignedConstraint(0);
	else if(str=="bottom")
		return new BottomAlignedConstraint(0);
	else if(str=="center") {
		if(name=="x")
			return new XCenteredConstraint;
		else if(name=="y")
			return new YCenteredConstraint;
	}
	float val;
	try {
		val = std::stof(str);
	}
	catch(const std::invalid_argument& e) {
		LOG_ERROR("Failed to parse number %s: %s", str.c_str(), e.what());
		return nullptr;
	}
	return new FixedConstraint(val);
}

FixedConstraint::FixedConstraint(float val) :
	m_val(val)
{}

float FixedConstraint::value() const {
	return m_val;
}

WidthRelConstraint::WidthRelConstraint(float width_fraction) :
	m_width_fraction(width_fraction)
{}

float WidthRelConstraint::value() const {
	return (m_parent->get_width()-m_parent->get_left_padding()-m_parent->get_right_padding())*m_width_fraction;
}

HeightRelConstraint::HeightRelConstraint(float height_fraction) :
	m_height_fraction(height_fraction)
{}

float HeightRelConstraint::value() const {
	return (m_parent->get_height()-m_parent->get_top_padding()-m_parent->get_bottom_padding())*m_height_fraction;
}

float XCenteredConstraint::value() const {
	return (m_parent->get_width()-m_component->get_width())/2;
}

float YCenteredConstraint::value() const {
	return (m_parent->get_height()-m_component->get_height())/2;
}

RightAlignedConstraint::RightAlignedConstraint(float padding) :
	m_padding(padding)
{}

float RightAlignedConstraint::value() const {
	return m_parent->get_width()-m_parent->get_right_padding()-m_parent->get_left_padding()-m_component->get_width()-m_component->get_right_margin()-m_padding;
}

BottomAlignedConstraint::BottomAlignedConstraint(float padding) :
	m_padding(padding)
{}

float BottomAlignedConstraint::value() const {
	return m_parent->get_height()-m_parent->get_bottom_padding()-m_parent->get_top_padding()-m_component->get_height()-m_component->get_bottom_margin()-m_padding;
}
