/*
 * 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/.
 */

#pragma once

#include "CameraController.hpp"
#include "Math/Math.hpp"

namespace OpenVulkano
{
	namespace Input
	{
		class InputAction;
	}

	class ArcballCameraController final : public CameraController
	{
		Math::Vector4f m_pivotPoint{ 0, 0, 0, 1 };
		float m_yaw = 0, m_pitch = 0, m_distance = 3, m_frameDistance;

		Input::InputAction* m_actionForward = nullptr;
		Input::InputAction* m_actionSide = nullptr;
		Input::InputAction* m_actionUp = nullptr;
		Input::InputAction* m_actionLookUp = nullptr;
		Input::InputAction* m_actionLookSide = nullptr;
		Input::InputAction* m_actionForwardOrtho = nullptr;
		Input::InputAction* m_actionSideOrtho = nullptr;
		Input::InputAction* m_actionUpOrtho = nullptr;
		Input::InputAction* m_actionZoomOrtho = nullptr;

		Math::Vector4f m_framePosition;
		bool m_frameUpdated = false, m_distUpated = true;
		bool m_lockYaw = false, m_lockPitch = false;

		float m_movementSpeedModifier = 3;

		void SetupInputActions();

		void HandleRotation();

		void HandleMovement();

		void HandleMovementOrtho();

	public:
		ArcballCameraController() { SetupInputActions(); }

		ArcballCameraController(Scene::Camera* camera) : CameraController(camera) { SetupInputActions(); SetActive(); }

		void Tick() override;

		[[nodiscard]] const Math::Vector4f& GetPivotPoint() const { return m_pivotPoint; }

		void SetPivotPoint(const Math::Vector4f& pivotPoint, bool updateDistance = false);

		void SetActive() override;

		void SetDefaultKeybindings();

		void Init(Scene::Camera *camera) override { CameraController::SetCamera(camera); SetActive(); }

		void LockYaw(float value = INFINITY, bool locked = true) { m_lockYaw = locked; if (value != INFINITY) m_yaw = value; }
		void LockPitch(float value = INFINITY, bool locked = true) { m_lockPitch = locked; if (value != INFINITY) m_pitch = value; }

		[[nodiscard]] float GetYaw() const { return m_yaw; }
		[[nodiscard]] float GetPitch() const { return m_pitch; }
		[[nodiscard]] float GetDistance() const { return m_distance; }
		[[nodiscard]] float GetMovementSpeedModifier() const { return m_movementSpeedModifier; }

		void SetYaw(float y) { m_yaw = y; }
		void SetPitch(float p) { m_pitch = p; }
		void SetDistance(float dist) { m_distance = dist; m_distUpated = true; }
		void SetMovementSpeedModifier(float speed = 3) { m_movementSpeedModifier = speed; }
	};
}
