/*
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_SERENITYOS_WINDOW_HPP
#define POLYGUN_WINDOW_SERENITYOS_WINDOW_HPP

#include "game/window/window.hpp"

#define AK_DONT_REPLACE_STD 1
#define USING_AK_GLOBALLY 1
#include <LibThreading/Thread.h>
#include <LibThreading/Mutex.h>
#include <LibGUI/Frame.h>
#if defined(RENDERERL_LEGACY_GL)
#include <LibGfx/Bitmap.h>
#endif

#if defined(RENDERER_LEGACY_GL)
#include "game/renderer/gl/serenityos_gl_context.hpp"
#endif

namespace polygun::window {
	class SerenityOSWindow final : public Window {
	friend class SerenityOSWindowFrame;
	public:
		SerenityOSWindow(const std::string& title, unsigned width, unsigned height);
		virtual ~SerenityOSWindow() override;

#if defined(RENDERER_LEGACY_GL)
		renderer::SerenityOSGLContext* create_gl_context(unsigned width, unsigned height);
#endif

		virtual size_t poll_events(Event& event) override;
		virtual void grab_mouse(bool grab) override;
#if defined(RENDERER_LEGACY_GL)
		virtual bool window_creates_gl_context() const override { return true; }
		virtual renderer::GLContext* get_window_gl_context() override { return m_gl_context; }
#endif

	private:
#if defined(RENDERER_LEGACY_GL)
		renderer::SerenityOSGLContext* m_gl_context;
#endif
		AK::RefPtr<Threading::Thread> m_window_thread;
		// Note: Cannot use ThreadSafe util class because SerenityOSWindow doesn't use std::thread
		Threading::Mutex m_window_mutex;
		AK::RefPtr<Gfx::Bitmap> m_framebuffer;
	};

	class SerenityOSWindowFrame final : public GUI::Frame {
	public:
		SerenityOSWindowFrame(SerenityOSWindow* window);

		virtual void paint_event(GUI::PaintEvent& ev) override;
		virtual void resize_event(GUI::ResizeEvent& ev) override;
		virtual void keydown_event(GUI::KeyEvent& ev) override;
		virtual void keyup_event(GUI::KeyEvent& ev) override;
		virtual void mousemove_event(GUI::MouseEvent& ev) override;
		virtual void mousedown_event(GUI::MouseEvent& ev) override;
		virtual void mouseup_event(GUI::MouseEvent& ev) override;
		virtual void mousewheel_event(GUI::MouseEvent& ev) override;

	private:
		SerenityOSWindow* m_window;
		bool m_first_resize_event;

	private:
		void key_event(GUI::KeyEvent& ev);
		void mouse_button_event(GUI::MouseEvent& ev);
	};
}

#endif // POLYGUN_WINDOW_SERENITYOS_WINDOW_HPP
