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

#include <config.hpp>
#if defined(WINDOW_WIN32)
#include "game/window/win32_window.hpp"
#endif
#if defined(WINDOW_X11)
#include "game/window/x11_window.hpp"
#endif
#if defined(WINDOW_WAYLAND)
#include "game/window/wayland_window.hpp"
#endif
#if defined(WINDOW_SERENITYOS)
#include "game/window/serenityos_window.hpp"
#endif
#include "common/logger.hpp"

using namespace polygun;

window::Window::Window() :
	m_type(),
	m_event_queue(),
	m_pressed_keyboard_keys(),
	m_pressed_mouse_buttons(),
	m_mouse_x(0),
	m_mouse_y(0),
	m_mouse_grabbed(false)
{}

void window::Window::push_event(const Event& event) {
	acquire();
	m_event_queue.push_back(event);
	release();
}

bool window::Window::is_mouse_inside_area(const glm::vec2& pos, const glm::vec2& size) {
	return m_mouse_x>=pos.x && m_mouse_x<=pos.x+size.x && m_mouse_y>=pos.y && m_mouse_y<=pos.y+size.y;
}

size_t window::Window::poll_events(Event& event) {
	acquire();
	size_t result = m_event_queue.size();
	if(!m_event_queue.empty()) {
		event = m_event_queue.front();
		m_event_queue.erase(m_event_queue.begin());
	}
	release();
	return result;
}

window::Window* window::Window::create_window(const std::string& title, unsigned width, unsigned height, renderer::RendererType renderer_type) {
#if defined(_WIN32)
#if defined(WINDOW_WIN32)
	return new window::Win32Window(title, width, height);
#else
	LOG_FATAL("Win32Window creation is not compiled in");
	return nullptr;
#endif
#elif defined(__apple__)
	// TODO
#elif defined(__serenityos__)
	return new window::SerenityOSWindow(title, width, height);
#elif defined(__unix__)
	char* c_session_type = getenv("XDG_SESSION_TYPE");
	if(!c_session_type) {
#if defined(WINDOW_X11)
		try {
			return new window::X11Window(title, width, height, renderer_type);
		}
		catch(const std::exception& e) {
#if defined(WINDOW_WAYLAND)
			return new window::WaylandWindow(title, width, height, renderer_type);
#else
			LOG_FATAL("Failed to create window");
#endif
		}
#elif defined(WINDOW_WAYLAND)
		try {
			return new window::WaylandWindow(title, width, height, renderer_type);
		}
		catch(const std::exception& e) {
			LOG_FATAL("Failed to create window");
		}
#endif
	}
	std::string session_type(c_session_type);
	if(session_type=="wayland") {
#if defined(WINDOW_WAYLAND)
		return new window::WaylandWindow(title, width, height, renderer_type);
#else
		LOG_FATAL("Failed to create Wayland window: not compiled in");
#endif
	}
	else {
#if defined(WINDOW_X11)
		return new window::X11Window(title, width, height, renderer_type);
#else
		LOG_FATAL("Failed to create X11 window: not compiled in");
#endif
	}
#endif
	LOG_FATAL("Failed to determine which window type should be created. You may have to set it manually");
	return nullptr;
}
