// =====================================================================================================================
//  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/containers/dynarray.h>
#include <fennec/containers/set.h>
#include <fennec/core/event.h>
#include <fennec/threading/lock_guard.h>
#include <fennec/threading/mpscq.h>
#include <fennec/threading/mutex.h>

#define FENNEC_EVENT_QUEUE_SIZE 8192

namespace fennec
{

static mutex                          lock;
static dynarray<set<event_listener*>> listeners;
static mpscq<event>                   queue(FENNEC_EVENT_QUEUE_SIZE);

event_listener::~event_listener() {
	event::remove_listener(this);
}

void event::handle_events() {
	lock_guard guard(lock);

	// if we don't copy into a temporary buffer, we could deadlock ourselves
	// if someone unwittingly calls dispatch() in an event listener.
	dynarray<unique_ptr<event>> dispatch(FENNEC_EVENT_QUEUE_SIZE);

	// we query the size instead of empty just in case someone decides to run their own thread
	// and send events from that thread.
	const size_t n = queue.size();
	for (size_t i = 0; i < n; ++i) {
		dispatch[i] = queue.pop();
	}

	for (size_t i = 0; i < n; ++i) {
		_handle_event(dispatch[i]);
	}
}

void event::_add_listener(event_listener* listener, uint64_t type) {
	lock_guard guard(lock);

	if (listeners.size() <= type) {
		listeners.resize(type + 1);
	}
	listeners[type].insert(listener);
}

void event::remove_listener(event_listener* listener) {
	lock_guard guard(lock);

	for (auto& it : listeners) {
		it.erase(listener);
	}
}

void event::_handle_event(unique_ptr<event>& event) {
	auto type = event->get_type().id();
	for (auto& listener : listeners[type]) {
		listener->handle_event(event.get());
	}
}

void event::_dispatch(unique_ptr<event>&& event) {
	// queue is taking ownership of the resource
	queue.emplace(event.release());
}

void event::_dispatch_immediate(unique_ptr<event>&& event) {
	lock_guard guard(lock);

	_handle_event(event);
}

}
