/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#include "FreeCamCameraController.hpp"
#include "Base/FrameMetadata.hpp"
#include "Scene/Camera.hpp"
#include "Input/InputManager.hpp"
#include "Input/InputKey.hpp"

namespace OpenVulkano
{
	FreeCamCameraController::FreeCamCameraController(Scene::Camera* camera) : CameraController(camera)
	{
		auto input = Input::InputManager::GetInstance();
		m_actionForward = input->GetAction("forward");
		m_actionSide = input->GetAction("side");
		m_actionUp = input->GetAction("up");
		m_actionLookUp = input->GetAction("look up");
		m_actionLookSide = input->GetAction("look side");
		m_actionBoost = input->GetAction("boost");
	}

	void FreeCamCameraController::Tick()
	{
		auto input = Input::InputManager::GetInstance();
		Math::Vector3f_SIMD vec(input->GetAxis(m_actionSide), input->GetAxis(m_actionUp), -input->GetAxis(m_actionForward));
		if(Math::Utils::length2(vec) > 1.0f)
		{
			vec = Math::Utils::normalize(vec);
		}

		float timeScale = 3 * CURRENT_FRAME.frameTime; //TODO
		vec = vec * timeScale * 3.0f; // scale vector
		if (input->GetButton(m_actionBoost))
		{
			vec *= m_boostFactor;
		}

		m_yaw -= input->GetAxis(m_actionLookSide) * timeScale / 2.0f;
		m_pitch -= input->GetAxis(m_actionLookUp) * timeScale / 2.0f;
		m_pitch = std::min(1.4f, std::max(-1.4f, m_pitch));

		const Math::QuaternionF rot(Math::Vector3f(m_pitch, m_yaw, 0));
		m_position += rot * vec;
		Math::Matrix4f camTransformation = Math::Utils::toMat4(rot);
		camTransformation[3] = Math::Vector4f(m_position, 1);
		GetCamera()->SetMatrix(camTransformation);
		CURRENT_FRAME.needsRedraw = true;
	}

	void FreeCamCameraController::SetDefaultKeybindings()
	{
		m_actionForward->BindKey(Input::InputKey::Controller::AXIS_LEFT_Y);
		m_actionForward->BindAxisButtons(Input::InputKey::Keyboard::KEY_W, Input::InputKey::Keyboard::KEY_S);
		m_actionForward->BindKey(Input::InputKey::Touch::AXIS_PAN_TWO_FINGERS_Y, -0.0025f);
		m_actionSide->BindKey(Input::InputKey::Controller::AXIS_LEFT_X);
		m_actionSide->BindAxisButtons(Input::InputKey::Keyboard::KEY_D, Input::InputKey::Keyboard::KEY_A);
		m_actionSide->BindKey(Input::InputKey::Touch::AXIS_PAN_TWO_FINGERS_X, 0.0025f);
		m_actionLookUp->BindKey(Input::InputKey::Controller::AXIS_RIGHT_Y);
		m_actionLookUp->BindAxisButtons(Input::InputKey::Keyboard::KEY_DOWN, Input::InputKey::Keyboard::KEY_UP);
		m_actionLookUp->BindKey(Input::InputKey::Touch::AXIS_PAN_Y, 0.10f);
		m_actionLookSide->BindKey(Input::InputKey::Controller::AXIS_RIGHT_X);
		m_actionLookSide->BindAxisButtons(Input::InputKey::Keyboard::KEY_RIGHT, Input::InputKey::Keyboard::KEY_LEFT);
		m_actionLookSide->BindKey(Input::InputKey::Touch::AXIS_PAN_X, 0.10f);
		m_actionLookUp->BindKey(Input::InputKey::Mouse::AXIS_Y);
		m_actionLookSide->BindKey(Input::InputKey::Mouse::AXIS_X);
		m_actionUp->BindAxisButtons(Input::InputKey::Keyboard::KEY_SPACE, Input::InputKey::Keyboard::KEY_LEFT_CONTROL);
		m_actionBoost->BindKey(Input::InputKey::Keyboard::KEY_LEFT_SHIFT);
	}
}