#include <CPFEventTypes.hpp>
#include <event_queue.hpp>
#include <event_queue_interface.hpp>
#include <event_handler.hpp>

using CPFEventStorage_t = MakeEventStorageType<
	EntryActionEvent,
	GPSPositionReadyEvent,
	GPSTimestampReadyEvent,
	GPSCommunicationErrorEvent
>;
using CPFEventQueue_t = EventQueue<CPFEventStorage_t>;

CallableEventHandler callback_handler;

CPFEventQueue_t global_event_queue_;

void CentralEventQueue::enqueue(const event_t& event)
{
	global_event_queue_.emplace(event);
}

void CentralEventQueue::enqueue(std::function<void(void)> func)
{
	global_event_queue_.emplace(func);
}

const event_t& CentralEventQueue::front()
{
	return global_event_queue_.front();
}

void CentralEventQueue::pop()
{
	global_event_queue_.pop();
}

bool CentralEventQueue::empty()
{
	return global_event_queue_.empty();
}

void CentralEventQueue::processCallbacks()
{
	while(!global_event_queue_.empty())
	{
		auto& item = global_event_queue_.front();
		if(callback_handler.accepts(item.get_message_id()))
		{
			callback_handler.receive(item);
			global_event_queue_.pop();
		}
		else
		{
			break;
		}
	}
}

void Callback::dispatch(std::function<void(void)> func)
{
	global_event_queue_.emplace(func);
}
