#include "input.h"

/* Initializes input. */
void
input_init(Input* self)
{
	memset(self, '\0', sizeof(Input));
}

/* Updates input struct. */
void
input_update(Input* self)
{
	memcpy(&self->previous, &self->current, sizeof(bool) * INPUT_COUNT);
	memset(&self->current, '\0', sizeof(bool) * INPUT_COUNT);

	keyboard_update(&self->keyboard);
	mouse_update(&self->mouse);
	event_update(&self->event);

	for (s32 i = 0; i < INPUT_COUNT; i++)
	{
		KeyboardInputEntry keyboardEntry;
		MouseInputEntry mouseEntry;
		
		keyboardEntry = INPUT_KEYBOARD_ENTRIES[i];
		mouseEntry = INPUT_MOUSE_ENTRIES[i];

		for (s32 j = 0; j < (s32)keyboardEntry.count; j++)
		{
			KeyboardKeyType keyboardKey;

			keyboardKey = keyboardEntry.types[j];

			if (self->keyboard.current[keyboardKey])
				self->current[i] = true;
		}

		for (s32 j = 0; j < (s32)mouseEntry.count; j++)
		{
			MouseButtonType mouseButton;

			mouseButton = mouseEntry.types[j];

			if (self->mouse.current[mouseButton])
				self->current[i] = true;
		}
	}
}

/* Is the given key pressed? */
bool
input_press(Input* self, InputType type)
{
	return (self->current[type] && !self->previous[type]);
}

/* Is the given key held? */
bool
input_held(Input* self, InputType type)
{
	return (self->current[type] && self->previous[type]);
}

/* Is the given key released? */
bool
input_release(Input* self, InputType type)
{
	return (!self->current[type] && self->previous[type]);
}
