/*
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_WINDOW_WINDOW_HPP
#define POLYGUN_WINDOW_WINDOW_HPP

#include "common/thread_safe.hpp"

#include <config.hpp>
#include <vector>
#include <string>
#include <map>

#include "game/window/event.hpp"
#include "game/renderer/master_renderer.hpp"

#if defined(RENDERER_GL) || defined(RENDERER_LEGACY_GL)
namespace polygun::renderer {
	class GLContext;
}
#endif

namespace polygun::window {
	enum WindowType {
		WINDOW_TYPE_WIN32,
		WINDOW_TYPE_X11,
		WINDOW_TYPE_WAYLAND,
		WINDOW_TYPE_SERENITYOS
	};

	class Window : public utils::ThreadSafe{
	public:
		Window();
		virtual ~Window() {}

		void push_event(const Event& event);
		WindowType get_window_type() const { return m_type; }
		bool is_keyboard_key_pressed(KeyCode key) { return m_pressed_keyboard_keys[key]; }
		bool is_mouse_button_pressed(MouseButton button) { return m_pressed_mouse_buttons[button]; }
		int get_mouse_x() const { return m_mouse_x; }
		int get_mouse_y() const { return m_mouse_y; }
		bool is_mouse_inside_area(const glm::vec2& pos, const glm::vec2& size);
		bool is_mouse_grabbed() const { return m_mouse_grabbed; }

		// TODO: Gamepad support

		virtual size_t poll_events(Event& event);
		virtual void finish_frame() {}
		virtual void grab_mouse(bool grab) = 0;
#if defined(RENDERER_GL) || defined(RENDERER_LEGACY_GL)
		virtual bool window_creates_gl_context() const { return false; }
		virtual renderer::GLContext* get_window_gl_context() { return nullptr; }
#endif

		static Window* create_window(const std::string& title, unsigned width, unsigned height, renderer::RendererType renderer_type);

	protected:
		WindowType m_type;
		std::vector<Event> m_event_queue;
		std::map<KeyCode, bool> m_pressed_keyboard_keys;
		std::map<MouseButton, bool> m_pressed_mouse_buttons;
		int m_mouse_x;
		int m_mouse_y;
		bool m_mouse_grabbed;
	};
}

#endif // POLYGUN_WINDOW_WINDOW_HPP
