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

#include "game/window/event.hpp"

using namespace polygun::gui;

GUIMaster::GUIMaster() :
	Container(),
	m_highlighted_component(nullptr),
	m_highlight_locked(false),
	m_shift(false)
{}

void GUIMaster::request_highlight(Component* component) {
	if(m_highlighted_component)
		m_highlighted_component->set_highlighted(false);
	if(component)
		component->set_highlighted(true);
	m_highlighted_component = component;
}

bool GUIMaster::update(const window::Event& event) {
	switch(event.m_type) {
	case window::EventType::EVENT_TYPE_KEY_DOWN:
		if(event.m_data.m_key_event.m_key==window::KeyCode::KEY_LEFT_SHIFT || event.m_data.m_key_event.m_key==window::KeyCode::KEY_RIGHT_SHIFT)
			m_shift = true;
		else if(event.m_data.m_key_event.m_key==window::KeyCode::KEY_TAB ||
				((event.m_data.m_key_event.m_key==window::KeyCode::KEY_RIGHT || event.m_data.m_key_event.m_key==window::KeyCode::KEY_LEFT) && !m_highlight_locked)) {
			const bool shift = event.m_data.m_key_event.m_key==window::KeyCode::KEY_TAB?m_shift:event.m_data.m_key_event.m_key==window::KeyCode::KEY_RIGHT;
			if(m_highlighted_component) {
				Component* component = shift?m_highlighted_component->get_prev_component():m_highlighted_component->get_next_component();
				if(component) {
					request_highlight(component);
					return true;
				}
			}
			else {
				m_highlighted_component = shift?find_last_component(this):find_first_component(this);
				if(m_highlighted_component) {
					m_highlighted_component->set_highlighted(true);
					return true;
				}
			}
		}
		break;
	case window::EventType::EVENT_TYPE_KEY_UP:
		if(event.m_data.m_key_event.m_key==window::KeyCode::KEY_LEFT_SHIFT || event.m_data.m_key_event.m_key==window::KeyCode::KEY_RIGHT_SHIFT)
			m_shift = false;
	default:
		break;
	}
	bool result = Container::update(event);
	if(!result && event.m_type==window::EventType::EVENT_TYPE_MOUSE_BUTTON_DOWN)
		request_highlight(nullptr);
	return result;
}

Component* GUIMaster::find_first_component(Container* container) {
	for(size_t i = 0; i<container->get_component_count(); i++) {
		Component* const component = container->get_component_at<Component>(i);
		Container* const c = dynamic_cast<Container*>(component);
		if(c)
			find_first_component(c);
		else
			return component;
	}
	return nullptr;
}

Component* GUIMaster::find_last_component(Container* container) {
	for(size_t i = container->get_component_count()-1; i>0; i--) {
		Component* const component = container->get_component_at<Component>(i);
		Container* const c = dynamic_cast<Container*>(component);
		if(c)
			find_last_component(c);
		else
			return component;
	}
	return nullptr;
}
