#include "gameplay/input.h"
#include "utility/command.h"
#include "gameplay/gameactor.h"

#include <SDL_timer.h>

void InputHandler::handleInput()
{
	if (!actor) return;
	Uint32 currentTime = SDL_GetTicks();
	// check for bound keys that were pressed,
	// next check if the hasn't been executed within the amount specified in delay
	// if not execute the command and set lastExecution to currentTime
	for (auto& [key, command] : keyCommands)
	{
		if (keys[key] == true)
		{
			if (currentTime - command.lastExecution >= command.delay)
			{
				command.cmd->execute(*actor);
				command.lastExecution = currentTime;
			}
		}
	}
	// Same with the mouse, for this context we'll be checking for motion events and for click events
	for (auto& [button, command] : mouseCommands)
	{
		if (mouseButtons[button] == true)
		{
			if (currentTime - command.lastExecution >= command.delay)
			{
				command.cmd->execute(*actor, mouse_state);
				command.lastExecution = currentTime;
			}
		}
	}
	if (!mouseMotionCommand) return;
	mouseMotionCommand->execute(*actor, mouse_state);
}
