/*
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.
*/

#ifndef POLYGUN_GUI_CONTAINER_HPP
#define POLYGUN_GUI_CONTAINER_HPP

#include "game/gui/component.hpp"

#include <vector>

#include "common/logger.hpp"

namespace polygun::engine {
	class Engine;
}

namespace polygun::gui {
	class Layout;

	class Container : public Component {
	public:
		Container(Constraint* x, Constraint* y, Constraint* width, Constraint* height, const std::string& id, Layout* layout);
		Container(xml::XMLNode* node);
		Container();
		virtual ~Container() override;

		void add_component(Component* component);
		void clear();
		bool load_from_file(const std::string& path, engine::Engine* engine);
		float get_top_padding() const { return m_top_padding; }
		float get_bottom_padding() const { return m_bottom_padding; }
		float get_left_padding() const { return m_left_padding; }
		float get_right_padding() const { return m_right_padding; }
		size_t get_component_count() const { return m_components.size(); }
		template<class T>
		T* get_component_at(size_t index) {
			if(index>=m_components.size())
				LOG_FATAL("Request for out of range component: %zu", index);
			T* const component = dynamic_cast<T*>(m_components[index]);
			if(!component)
				LOG_FATAL("Component at index %zu is not compatible with requested type", index);
			return component;
		}
		template<class T>
		T* get_component_by_id(const std::string& id) {
			for(Component* component : m_components) {
				Container* container = dynamic_cast<Container*>(component);
				if(container) {
					T* comp = container->get_component_by_id<T>(id);
					if(comp)
						return comp;
				}
				if(id==component->get_id()) {
					T* const comp = dynamic_cast<T*>(component);
					if(!comp)
						LOG_FATAL("Component with id '%s' is not compatible with requested type", id.c_str());
					return comp;
				}
			}
			return nullptr;
		}

		virtual void render(renderer::GUIRenderer* renderer) override;
		virtual bool update(const window::Event& event) override;

	protected:
		Layout* m_layout;
		std::vector<Component*> m_components;
		float m_top_padding;
		float m_bottom_padding;
		float m_left_padding;
		float m_right_padding;

	private:
		void load_container(xml::XMLNode* node, engine::Engine* engine);
	};
}

#endif // POLYGUN_GUI_CONTAINER_HPP
