#include "../http/mime.hpp"
#include "notfound.hpp"
#include <sstream>

namespace Middleware
{
	void NotFound::HandleRequest(State & state, ScheduleHelper & scheduleHelper)
	{
		if (state.response.code != HttpResponse::Code::UNKNOWN || 
			!(state.request.type == HttpRequest::Type::GET || state.request.type == HttpRequest::Type::HEAD))
		{
			return;
		}

		state.response.code = HttpResponse::Code::NOT_FOUND;
		if (state.request.type == HttpRequest::Type::GET)
		{
			state.response.contentType = Http::GetMimeType(Http::FileType::HTML);
			
			std::stringstream ss;
			ss << "<!DOCTYPE html><html><head></head><body>";
			ss << "<h1>404 - File Not Found</h1>";
			ss << "<p>File: " << state.request.url.GetPath() << "<p>";
			ss << "</body></html>";

			auto responseContent = ss.str();
			state.response.content.insert(state.response.content.begin(),
				responseContent.begin(),
				responseContent.end());
		}

		state.finished = true;
	}

	NotFound::NotFound(Logger & _logger)
		: BaseMiddleware(_logger)
	{
	}
}