// =====================================================================================================================
//  fennec, a free and open source game engine
//  Copyright © 2025 - 2026  Medusa Slockbower
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <https://www.gnu.org/licenses/>.
// =====================================================================================================================

///
/// \file fennec/platform/linux/wayland/window.h
/// \brief
///
///
/// \details
/// \author Medusa Slockbower
///
/// \copyright Copyright © 2025 - 2026 Medusa Slockbower ([GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html))
///
///

#ifndef FENNEC_PLATFORM_LINUX_WAYLAND_WINDOW_H
#define FENNEC_PLATFORM_LINUX_WAYLAND_WINDOW_H

#include <fennec/platform/linux/wayland/forward.h>
#include <fennec/platform/interface/window.h>

#ifndef FENNEC_DOXYGEN
struct wl_array;
struct wl_callback;
struct wl_output;
struct wl_surface;

struct xdg_surface;
struct xdg_toplevel;

#if FENNEC_HAS_LIBDECOR
struct libdecor_frame;
struct libdecor_configuration;
#endif
#endif

namespace fennec
{

///
/// \brief Class for handling a Wayland window
class wayland_window : public window_base<wayland_server> {

// Constructors & Destructor ===========================================================================================
public:

	/// \name Constructors & Destructor
	/// @{

	///
	/// \brief constructor
	/// \param server the display server
	/// \param cfg the configuration
	/// \param parent the parent window
	wayland_window(display_server* server, const config& cfg, window* parent);

	///
	/// \brief destructor
	~wayland_window();

	/// @}


	void set_size(const ivec2& size) override;

// Behaviour Flags =====================================================================================================
public:

	/// \name Behaviour Flags
	/// @{

	///
	/// \brief sets a specific flag
	/// \param flag the flag from `window::flag_`
	/// \param val the value to set the flag to
	/// \returns \emph{true} on success, \emph{false} otherwise
	bool set_flag(uint8_t flag, bool val) override;

	/// @}


// Initialization & Update =============================================================================================
public:

	/// \name Initialization & Update
	/// @{

	void initialize() override; //!< initialize the window
	void shutdown() override;   //!< shutdown the window

	/// \returns the native wl_surface handle
	void* get_native_handle() override;

	/// @}


// Private Member Variables ============================================================================================
private:
	wl_surface*   surface;
	xdg_surface*  xdgsurface;
	xdg_toplevel* xdgtoplevel;
	wl_callback*  frame_callback;

#if FENNEC_HAS_LIBDECOR
	libdecor_frame*         libdecorframe;
	libdecor_configuration* libdecorcfg;
#endif


// Private Helpers =====================================================================================================
private:
	void _update_size(const ivec2& size);


// Listeners ===========================================================================================================
private:

	static void _wl_surface_listen_enter(void*, wl_surface*, wl_output*);
	static void _wl_surface_listen_leave(void*, wl_surface*, wl_output*);
	static void _wl_surface_listen_preferred_buffer_scale(void*, wl_surface*, int32_t);
	static void _wl_surface_listen_preferred_buffer_transform(void*, wl_surface*, uint32_t);

	static void _wl_frame_listen_done(void*, wl_callback*, uint32_t);

	static void _xdg_surface_listen_configure(void*, xdg_surface*, uint32_t);

	static void _xdg_toplevel_listen_configure(void*, xdg_toplevel*, int32_t, int32_t, wl_array*);
	static void _xdg_toplevel_listen_configure_bounds(void*, xdg_toplevel*, int32_t, int32_t);
	static void _xdg_toplevel_listen_close(void*, xdg_toplevel*);
	static void _xdg_toplevel_listen_wm_capabilities(void*, xdg_toplevel*, wl_array*);

#if FENNEC_HAS_LIBDECOR
	static void _libdecor_frame_listen_configure(libdecor_frame*, libdecor_configuration*, void*);
	static void _libdecor_frame_listen_close(libdecor_frame*, void*);
	static void _libdecor_frame_listen_commit(libdecor_frame*, void*);
	static void _libdecor_frame_listen_dismiss_popup(libdecor_frame*, const char*, void*);
#endif
};

}

#endif // FENNEC_PLATFORM_LINUX_WAYLAND_WINDOW_H
