#pragma once
#include "logger.hpp"
#include <map>
#include <sys/epoll.h>
#include <vector>

class ScheduleHelper; // Forward declare

//using callbackFunction = void (*)(int, ScheduleHelper);

class Scheduler
{
	friend class ScheduleHelper;
private:
	Logger & logger;
	int epollFileDescriptor;
//	std::map<int, callbackFunction> fileDescriptorToCallbackMap;
	std::vector<epoll_event> epollEvents;

	void ScheduleFileDescriptor(int fd, int eventFlags, bool isReschedule);

public:
	// Blocks until an event happens
	// Returns a vector filedescriptors that are ready for processing
	std::vector<int> WaitForEvents();

	ScheduleHelper GetScheduleHelper();

	Scheduler(Logger & logger);
	~Scheduler();
	Scheduler(Scheduler & other) = delete;
	Scheduler(Scheduler && other) = delete;
	Scheduler & operator=(Scheduler & other) = delete;
	Scheduler & operator=(Scheduler && other) = delete;
};

class ScheduleHelper
{
private:
	Scheduler & scheduler;

public:
	void ScheduleReadFileDescriptor(int fd, bool oneshot = true, bool isReschedule = false);
	void ScheduleWriteFileDescriptor(int fd, bool oneshot = true, bool isReschedule = false);

	ScheduleHelper(Scheduler & scheduler);
};