#pragma once
#include "../http/request.hpp"
#include "../http/response.hpp"
#include "../logger.hpp"
#include "../middleware/middleware.hpp"
#include "../scheduler.hpp"
#include "eventtype.hpp"
#include <map>
#include <netinet/in.h>

namespace Server
{
	class HttpServer
	{
	private:
		Logger & logger;
		Scheduler scheduler;
		ScheduleHelper scheduleHelper;

		sockaddr_in socketAddress;
		int listeningSocketFileDescriptor;

		std::map<int, EventType> fileDescriptorEventTypeMap;	
		std::map<int, Http::Request> socketRequestMap;
		std::map<int, Http::Response> socketResponseMap;

		Middleware::Middleware middleware;

		bool isOpen;

		void RegisterNewConnection(int const listeningSocketFd);

		void ReadRequest(int const socketFd);

		void HandleMiddlewareEvent(int const eventFd);

		void HandleMiddlewareResponse(Middleware::MiddlewareResult const & middlewareResult);

		void WriteResponse(int const socketFd);

	public:
		void Execute();
		
		HttpServer(Logger & logger, Configuration const & serverConfiguration);
		~HttpServer();
	};
}