#include "input.h"
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_log.h>
#include <SDL2/SDL_scancode.h>

namespace rogue {
InputHandler::InputHandler() {
	Input::add_handler(this);
}

InputHandler::~InputHandler() {
	Input::remove_handler(this);
}

KeyEventHandler::KeyEventHandler(std::set<SDL_Scancode> code, Listener listener)
: scancode{code}
, listener{listener} {}

void KeyEventHandler::process_event(SDL_Event event) {
	if((event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) && this->scancode.contains(event.key.keysym.scancode)) {
		this->listener(event.type == SDL_KEYDOWN);
	}
}

void Input::add_handler(InputHandler *handler) {
	Input::handlers.insert(handler);
}

void Input::remove_handler(InputHandler *handler) {
	Input::handlers.erase(handler);
}

void Input::process_event(SDL_Event event) {
	for(InputHandler *handler : Input::handlers) {
		handler->process_event(event);
	}
}

std::set<InputHandler*> Input::handlers{};
}
