#include <SDL3/SDL.h>
#include "engine/inputmanager.h"
#include "engine/engine.h"

InputManager g_inputManager;

void InputManager::OnKeyboardAction(uint32_t key, bool action)
{
	if (key >= kMaxKeyboardKeys)
		return;

	m_keyboard.m_keys[key] = action;
}

void InputManager::OnMouseAction(uint32_t key, bool action)
{
	assert(key <= kMaxMouseKeys);

	m_mouse.m_keys[key] = action;
}

void InputManager::OnMouseMoveAction(float x, float y)
{
	m_deltaMousePos.x = x - m_mousePos.x;
	m_deltaMousePos.y = y - m_mousePos.y;

	if (m_relativeMouseMode) {
		m_mousePos.x += x;
		m_mousePos.y += y;
	} else {
		m_mousePos.x = x;
		m_mousePos.y = y;
	}
}

void InputManager::SetRelativeMouseMode(bool relativeMode)
{
	if (m_relativeMouseMode != relativeMode) {
		SDL_SetWindowRelativeMouseMode(GetEngine()->GetWindow(), relativeMode);
		m_relativeMouseMode = relativeMode;
	}
}

void InputManager::Frame()
{
	m_deltaMousePos.x = 0;
	m_deltaMousePos.y = 0;
	m_mousePos.x = 0;
	m_mousePos.y = 0;
}

EMouseButtons GetMouseButtonsFromSDL(uint8_t button)
{
	EMouseButtons mousebutton = EMouseButton_None;

	if (button == SDL_BUTTON_LEFT)
		mousebutton = EMouseButton_Left;
	else if (button == SDL_BUTTON_MIDDLE)
		mousebutton = EMouseButton_Middle;
	else if (button == SDL_BUTTON_RIGHT)
		mousebutton = EMouseButton_Right;
	else if (button == SDL_BUTTON_X1)
		mousebutton = EMouseButton_1;
	else if (button == SDL_BUTTON_X2)
		mousebutton = EMouseButton_2;

	return mousebutton;
}