#include "input.h"
#include "internal.h"

namespace kek::Defaults {

static int
	keyForward,
	keyBackward,
	keyLeft,
	keyRight,
	keyUp,
	keyDown;

void defaultInput(GLFWwindow *window, void *data) {
	if(Input::getKeyState(keyForward) == GLFW_PRESS) {
		kekData.activeCamera->translate(kekData.activeCamera->direction * KEK_NOCLIP_SPEED * kekData.lastFrameTime);
	}

	if(Input::getKeyState(keyBackward) == GLFW_PRESS) {
		kekData.activeCamera->translate(kekData.activeCamera->direction * -KEK_NOCLIP_SPEED * kekData.lastFrameTime);
	}

	if(Input::getKeyState(keyLeft) == GLFW_PRESS) {
		glm::vec3 camRight = glm::normalize(glm::cross(kekData.activeCamera->direction, glm::vec3(0.0f, 1.0f, 0.0f)));
		kekData.activeCamera->translate(-camRight * KEK_NOCLIP_SPEED * kekData.lastFrameTime);
	}

	if(Input::getKeyState(keyRight) == GLFW_PRESS) {
		glm::vec3 camRight = glm::normalize(glm::cross(kekData.activeCamera->direction, glm::vec3(0.0f, 1.0f, 0.0f)));
		kekData.activeCamera->translate(camRight * KEK_NOCLIP_SPEED * kekData.lastFrameTime);
	}

	if(Input::getKeyState(keyUp) == GLFW_PRESS) {
		kekData.activeCamera->translateY(KEK_NOCLIP_SPEED * kekData.lastFrameTime);
	}

	if(Input::getKeyState(keyDown) == GLFW_PRESS) {
		kekData.activeCamera->translateY(-KEK_NOCLIP_SPEED * kekData.lastFrameTime);
	}
}

void init() {
	keyForward = Input::createKeyMapping("Forward", GLFW_KEY_W);
	keyBackward = Input::createKeyMapping("Backward", GLFW_KEY_S);
	keyLeft = Input::createKeyMapping("Left", GLFW_KEY_A);
	keyRight = Input::createKeyMapping("Right", GLFW_KEY_D);
	keyUp = Input::createKeyMapping("Up", GLFW_KEY_SPACE);
	keyDown = Input::createKeyMapping("Down", GLFW_KEY_LEFT_CONTROL);

	Input::addPeriodicCallback(PeriodicCallback(defaultInput, nullptr));
}

}
