/*
 * 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 "MovingCubeApp.hpp"
#include "Math/Math.hpp"
#include "Scene/Scene.hpp"
#include "Scene/Shader/Shader.hpp"
#include "Scene/Geometry.hpp"
#include "Scene/GeometryFactory.hpp"
#include "Scene/Material.hpp"
#include "Scene/Vertex.hpp"
#include "Scene/SimpleDrawable.hpp"
#include "Scene/Camera.hpp"
#include "Scene/SimpleAnimationController.hpp"
#include "Scene/SequenceAnimationController.hpp"
#include "Scene/MorphableCameraController.hpp"
#include "Scene/PlaneCameraController.hpp"
#include "Scene/UI/PerformanceInfo.hpp"
#include "Input/InputManager.hpp"
#include "Host/GraphicsAppManager.hpp"
#include "Base/EngineConfiguration.hpp"
#include "Base/Logger.hpp"
#include "Controller/FreeCamCameraController.hpp"

#define USE_PLANE_CAM_CONTROL 0

namespace OpenVulkano
{
	namespace
	{
		struct SceneElement
		{
			Scene::Geometry m_geometry;
			Scene::SimpleDrawable m_drawable;
			Scene::Node m_node;
		};
		const Math::Vector3f PLANE_NORMAL(1, 0, 1);
	}
	class MovingCubeAppImpl final : public MovingCubeApp
	{
		Scene::Scene m_scene;
		Scene::MorphableCamera m_camera;
		Scene::MorphableCameraController m_morphableCameraControl;
#if USE_PLANE_CAM_CONTROL
		Scene::PlaneCameraController m_cameraControl;
#else
		FreeCamCameraController m_cameraControl;
#endif
		Scene::Material m_material;
		Scene::Shader m_shader;

		Scene::SimpleAnimationController m_simpleAnimationController;
		Scene::SequenceAnimationController m_sequenceAnimationController;

		Scene::UI::SimpleUi m_ui;
		std::shared_ptr<Scene::UI::PerformanceInfo> m_perfInfo;

		SceneElement m_whiteBox;
		SceneElement m_redBox;

		SceneElement m_plane;
		SceneElement m_sphere;
		SceneElement m_hemisphere;
		SceneElement m_triangle;
		SceneElement m_cylinder;
		SceneElement m_pyramid;

		void CompleteSceneElement(SceneElement *dest)
		{
			dest->m_drawable.Init(&m_shader, &dest->m_geometry, &m_material);
			dest->m_node.Init();
			m_scene.GetRoot()->AddChild(&dest->m_node);
			dest->m_node.SetUpdateFrequency(Scene::UpdateFrequency::Always);
			dest->m_node.AddDrawable(&dest->m_drawable);
			dest->m_node.SetMatrix(Math::Matrix4f(1));
		}

		void CreateSceneElement(SceneElement *dest, const Math::Vector4f &color, float scale)
		{
			dest->m_geometry = Scene::GeometryFactory::MakeCube(scale, scale, scale, color);
			CompleteSceneElement(dest);
		}

		void CreatePlane(SceneElement *dest, const Math::Vector4f &color)
		{
			dest->m_geometry = Scene::GeometryFactory::MakePlane(1, 1, color);
			CompleteSceneElement(dest);
		}

		void CreateSphere(SceneElement *dest, const Math::Vector4f &color)
		{
			dest->m_geometry = Scene::GeometryFactory::MakeSphere(1, 32, 16, color);
			CompleteSceneElement(dest);
		}

		void CreateHemisphere(SceneElement *dest, const Math::Vector4f &color)
		{
			dest->m_geometry = Scene::GeometryFactory::MakeHemisphere(1, 16, 16, color);
			CompleteSceneElement(dest);
		}

		void CreateTriangle(SceneElement *dest, const Math::Vector4f &color)
		{
			dest->m_geometry = Scene::GeometryFactory::MakeTriangle(
			Math::Vector3f(0.5, 0., 0.), Math::Vector3f(0., 0.5, 0.), Math::Vector3f(-0.5, 0., 0.), color);
			CompleteSceneElement(dest);
		}

		void CreateCylinder(SceneElement *dest, const Math::Vector4f &color)
		{
			dest->m_geometry = Scene::GeometryFactory::MakeCylinder(1, 3, 64, color);
			CompleteSceneElement(dest);
		}

		void CreatePyramid(SceneElement *dest, const Math::Vector4f &color)
		{
			dest->m_geometry = Scene::GeometryFactory::MakePyramid(0.5, 2, color);
			CompleteSceneElement(dest);
		}

	public:
		MovingCubeAppImpl() : m_camera(90)
		{
			m_morphableCameraControl.Init(&m_camera);
#if USE_PLANE_CAM_CONTROL
			m_cameraControl.Init(&m_camera, PLANE_NORMAL);
			// m_cameraControl.Init(&m_camera, Scene::PlaneCameraController::DefaultAxis::OYZ);
#else
			m_cameraControl.Init(&m_camera);
#endif
			m_camera.SetZoom(50);
		}

		void Init() override
		{
			m_camera.Init(70, 16, 9, 0.1, 100);

			m_scene.Init();
			m_scene.SetCamera(&m_camera);

			m_shader.AddShaderProgram(ShaderProgramType::VERTEX, "Shader/basic");
			m_shader.AddShaderProgram(ShaderProgramType::FRAGMENT, "Shader/basic");
			m_shader.AddVertexInputDescription(Vertex::GetVertexInputDescription());

			GetGraphicsAppManager()->GetRenderer()->SetScene(&m_scene);
#if USE_PLANE_CAM_CONTROL
			m_cameraControl.Init(&m_camera, PLANE_NORMAL);
			// m_cameraControl.Init(&m_camera, Scene::PlaneCameraController::DefaultAxis::OYZ);
#else
			m_cameraControl.Init(&m_camera);
#endif
			m_cameraControl.SetDefaultKeybindings();

			CreateSceneElement(&m_whiteBox, Math::Vector4f(1, 1, 1, 1), 1);
			CreateSceneElement(&m_redBox, Math::Vector4f(1, 0.2, 0.2, 1.0), 0.3);

			m_simpleAnimationController.SetNode(&m_whiteBox.m_node);
			m_simpleAnimationController.SetDuration(3);
			Math::Pose srcPose(Math::Quaternion<float>(), Math::Vector3f_SIMD(-3, 0, 0));
			Math::Pose destPose(Math::Quaternion<float>(), Math::Vector3f_SIMD(3, 0, 0));
			m_simpleAnimationController.SetPoses(srcPose, destPose);
			m_simpleAnimationController.m_completionEvent +=
			EventHandler(this, &MovingCubeAppImpl::OnSimpleAnimationCompleted);

			m_sequenceAnimationController.EnableLoop(true);
			m_sequenceAnimationController.SetNode(&m_redBox.m_node);
			m_sequenceAnimationController.AddAnimationStep(
			Math::PoseF(Math::Utils::normalize(Math::QuaternionF(1, 0, 0, 1)), Math::Vector3f_SIMD(0, 0, 1)), 5);
			m_sequenceAnimationController.AddAnimationStep(
			Math::PoseF(Math::Utils::normalize(Math::QuaternionF(2, 1, 0, 1)), Math::Vector3f_SIMD(1, 1, -1)), 3);
			m_sequenceAnimationController.AddAnimationStep(
			Math::PoseF(Math::Utils::normalize(Math::QuaternionF(1, 1, 1, 1)), Math::Vector3f_SIMD(2, 1, -2)), 3);
			m_sequenceAnimationController.AddAnimationStep(
			Math::PoseF(Math::Utils::normalize(Math::QuaternionF(0, 1, 1, 0)), Math::Vector3f_SIMD(2, 1, -1)), 3);
			m_sequenceAnimationController.AddAnimationStep(
			Math::PoseF(Math::Utils::normalize(Math::QuaternionF(3, 2, 1, 1)), Math::Vector3f_SIMD(1, 1, -1)), 3);
			m_sequenceAnimationController.AddAnimationStep(
			Math::PoseF(Math::Utils::normalize(Math::QuaternionF(0, 0, 1, 1)), Math::Vector3f_SIMD(0, 1, 0)), 1);
			m_sequenceAnimationController.SetAnimationPoseResetTime(10);

			CreatePlane(&m_plane, Math::Vector4f(0.3, 0.6, 0.9, 1.0));
			m_plane.m_node.worldMat = Math::Utils::translate(Math::Vector3f(3, 0, 0));

			CreateSphere(&m_sphere, Math::Vector4f(0.9, 0.6, 0.9, 1.0));
			m_sphere.m_node.worldMat = Math::Utils::translate(Math::Vector3f(5, 0, 0));

			CreateHemisphere(&m_hemisphere, Math::Vector4f(0.6, 0.3, 0.9, 1.0));
			m_hemisphere.m_node.worldMat = Math::Utils::translate(Math::Vector3f(7, 0, 0));

			CreateTriangle(&m_triangle, Math::Vector4f(0.6, 0.9, 0.3, 1.0));
			m_triangle.m_node.worldMat = Math::Utils::translate(Math::Vector3f(9, 0, 0));

			CreateCylinder(&m_cylinder, Math::Vector4f(0.3, 0.9, 0.9, 1.0));
			m_cylinder.m_node.worldMat = Math::Utils::translate(Math::Vector3f(11, 0, 0));

			CreatePyramid(&m_pyramid, Math::Vector4f(0.9, 0.9, 0.6, 1.0));
			m_pyramid.m_node.worldMat = Math::Utils::translate(Math::Vector3f(13, 0, 0));

			std::shared_ptr<Scene::UI::PerformanceInfo> m_perfInfo = std::make_shared<Scene::UI::PerformanceInfo>();
			m_ui.AddElement(m_perfInfo);
			GetGraphicsAppManager()->GetRenderer()->SetActiveUi(&m_ui);
		}

		void OnSimpleAnimationCompleted(Scene::SimpleAnimationController *anim)
		{
			anim->SwapPoses();
			anim->Reset();
		}

		void Tick() override
		{
			m_cameraControl.Tick();
			m_morphableCameraControl.Tick();
			m_simpleAnimationController.Tick();
			m_sequenceAnimationController.Tick();
		}

		void Close() override {}
	};

	IGraphicsApp *MovingCubeApp::Create() { return new MovingCubeAppImpl(); }

	std::unique_ptr<IGraphicsApp> MovingCubeApp::CreateUnique() { return std::make_unique<MovingCubeAppImpl>(); }
}
