// =====================================================================================================================
//  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/>.
// =====================================================================================================================

#include <fennec/core/logger.h>

#include <fennec/renderers/interface/gfxcontext.h>

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

#include <fennec/platform/linux/wayland/lib/wayland.h>
#include <fennec/platform/linux/wayland/lib/loader.h>
#include <fennec/platform/linux/wayland/lib/headers/xdg-shell-client-protocols.h>

#if FENNEC_HAS_LIBDECOR
#include <fennec/platform/linux/wayland/libdecor/loader.h>
#endif

namespace fennec
{

wayland_server::wayland_server(fennec::platform* p)
	: display_server_base(p)
	, display(nullptr), registry(nullptr), compositor(nullptr), seat(nullptr), fifo(false), has_libdecor(false) {

	// load shared lib
	assertf(libwayland::load_symbols(platform), "Failed to load libwayland.");

#if FENNEC_HAS_LIBDECOR
	has_libdecor = libdecor::load_symbols(platform);
#endif
}

wayland_server::~wayland_server() {

	// ensure we did disconnect
	wayland_server::disconnect();

	// unload shared lib
	libwayland::unload_symbols(platform);
}

void wayland_server::connect() {

	static constexpr wl_registry_listener listener = {
		_wl_registry_listen_global, _wl_registry_on_global_remove
	};

	if (display != nullptr) {
		return;
	}

	// connect to wayland
	display = wl_display_connect(nullptr);
	if (display == nullptr) {
		return;
	}

	// get the registry
	registry = wl_display_get_registry(display);
	if (registry == nullptr) {
		assert(registry, "Failed to acquire registry from Wayland");
		disconnect();
		return;
	}

	// Aquire interfaces
	wl_registry_add_listener(registry, &listener, this);
	wl_display_roundtrip(display);

	// Check for fifo v1, wayland's builtin fifo only allows one frame to be in flight at a time
	if (not fifo) {
		assert(fifo, "Compositor does not support fifo-v1 protocol, falling back to other display protocols.");
		disconnect();
		return;
	}

#if FENNEC_HAS_LIBDECOR
	static libdecor_interface libdecor_listener = {
		.error = _libdecor_on_error,

		.reserved0 = nullptr,
		.reserved1 = nullptr,
		.reserved2 = nullptr,
		.reserved3 = nullptr,
		.reserved4 = nullptr,
		.reserved5 = nullptr,
		.reserved6 = nullptr,
		.reserved7 = nullptr,
		.reserved8 = nullptr,
		.reserved9 = nullptr,
	};

	if (has_libdecor) {
		libdecor = libdecor_new(display, &libdecor_listener);
	}
#endif

	auto contexts = ctx_registry::get_type_list();
	while (not contexts.is_empty()) {
		const auto& ctx = contexts.front();
		gfx_context = unique_ptr(ctx.ctor(this));

		if (gfx_context->is_valid()) {
			break;
		} else {
			gfx_context.reset();
			gfx_context = nullptr;
			contexts.pop();
		}
	}

	assertf(not gfx_context.is_empty(), "Failed to create graphics context.");
}

void wayland_server::disconnect() {

	// early escape
	if (display == nullptr) {
		return;
	}

	// delete the gfx context
	gfx_context.reset();


	// check compositor
	if (compositor) {
		wl_compositor_destroy(compositor);
	}

	// check registry
	if (registry) {
		wl_registry_destroy(registry);
	}

	// disconnect from wayland
	wl_display_flush(display);
	wl_display_disconnect(display);

	// clear vars
	gfx_context    = nullptr;
	display    = nullptr;
	registry   = nullptr;
	compositor = nullptr;
	seat       = nullptr;
	fifo       = false;
}

bool wayland_server::connected() const {
	return display != nullptr;
}

void wayland_server::dispatch() {
#if FENNEC_HAS_LIBDECOR
	if (libdecor) {
		libdecor_dispatch(libdecor, 0);
	} else
#endif
	{
		wl_display_dispatch_pending(display);
	}
}

window* wayland_server::create_window(const window::config& conf, window* parent) {
	return new wayland_window(this, conf, parent);
}

void wayland_server::_wl_registry_listen_global(void* data, wl_registry* reg, uint32_t id, const char* name, uint32_t version) {
	static constexpr wl_seat_listener seat_listener = {
		_wl_seat_listen_capabilities, _wl_seat_listen_name
	};

	static constexpr xdg_wm_base_listener xdg_listener = {
		_xdg_listen_ping
	};

	wayland_server* server = static_cast<wayland_server*>(data);
	const cstring interface = cstring(name, strlen(name));

	if (interface == "wl_compositor") {
		server->compositor = static_cast<wl_compositor*>(wl_registry_bind(reg, id, &wl_compositor_interface, version));
		return;
	}

	if (interface == "xdg_wm_base") {
		server->xdg = static_cast<xdg_wm_base*>(wl_registry_bind(reg, id, &xdg_wm_base_interface, version));
		xdg_wm_base_add_listener(server->xdg, &xdg_listener, server);
		return;
	}

	if (interface == "wl_seat") {
		server->seat = static_cast<wl_seat*>(wl_registry_bind(reg, id, &wl_seat_interface, version));
		wl_seat_add_listener(server->seat, &seat_listener, server);
		return;
	}

	if (interface == "wp_fifo_manager_v1") {
		server->fifo = true;
		return;
	}
}

void wayland_server::_wl_registry_on_global_remove(void*, wl_registry*, uint32_t) {

}


void wayland_server::_wl_seat_listen_capabilities(void*, wl_seat*, uint32_t) {

}

void wayland_server::_wl_seat_listen_name(void*, wl_seat*, const char*) {}


void wayland_server::_xdg_listen_ping(void*, xdg_wm_base* xdg, uint32_t serial) {
	xdg_wm_base_pong(xdg, serial);
}

#if FENNEC_HAS_LIBDECOR
void wayland_server::_libdecor_on_error(struct libdecor*, libdecor_error error, const char* message) {
	fennec::logger::log(logger::error,
		fennec::format("libdecor error ({}): {}", static_cast<int>(error), fennec::cstring(message, strlen(message)))
	);
}
#endif

}
