/*
 * 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 "LabelDrawableExampleApp.hpp"
#include "Scene/Scene.hpp"
#include "Scene/Shader/Shader.hpp"
#include "Scene/Geometry.hpp"
#include "Scene/TextDrawable.hpp"
#include "Scene/GeometryFactory.hpp"
#include "Scene/Material.hpp"
#include "Scene/Vertex.hpp"
#include "Scene/SimpleDrawable.hpp"
#include "Scene/UI/PerformanceInfo.hpp"
#include "Scene/UniformBuffer.hpp"
#include "Scene/Prefabs/LabelDrawable.hpp"
#include "Input/InputManager.hpp"
#include "Host/GraphicsAppManager.hpp"
#include "Host/GLFW/WindowGLFW.hpp"
#include "Host/ResourceLoader.hpp"
#include "Math/Math.hpp"
#include "Base/EngineConfiguration.hpp"
#include "Controller/FreeCamCameraController.hpp"
#include "Image/ImageLoaderPng.hpp"
#include "Scene/FontAtlasGenerator.hpp"
#include "Scene/IFontAtlasGenerator.hpp"
#include <filesystem>

#ifdef _WIN32
	#undef TRANSPARENT
#endif

namespace OpenVulkano
{
	using namespace Scene;
	using namespace Input;
	using namespace Math;
	namespace fs = std::filesystem;

	class LabelDrawableExampleAppImpl final : public LabelDrawableExampleApp
	{
	public:

		void Init() override
		{
			auto engineConfig = OpenVulkano::EngineConfiguration::GetEngineConfiguration();
			engineConfig->SetNumThreads(1);
			engineConfig->SetPreferFramebufferFormatSRGB(false);
			
			std::srand(1); // Fix seed for random numbers
			m_scene.Init();
			m_cam.Init(70, 16, 9, 0.1, 100);
			m_scene.SetCamera(&m_cam);

			auto& resourceLoader = ResourceLoader::GetInstance();
			auto sdfMetadataInfo = resourceLoader.GetResource("sdf_atlas_packed.png");

			TextDrawable textDrawable(sdfMetadataInfo);
			const std::vector<std::string> texts = { "_!?{}.#@", "1", "XYZ", "12345" };
			const int N = texts.size();
			m_nodesPool.resize(N);
			m_drawablesPool.reserve(N);

			BillboardControlBlock billboardSettings;
			LabelDrawableSettings labelSettings;

			for (int i = 0; i < N; i++)
			{
				if (i == 3)
				{
					labelSettings.hasRoundedCorners = labelSettings.hasArrow = true;
				}
				else
				{
					labelSettings.hasRoundedCorners = (i % 2 == 0 ? 0 : 1);
					labelSettings.hasArrow = (i % 2 == 0 ? 1 : 0);
				}
				bool isBillboard = (i % 2 == 0 ? 1 : 0);
				LabelDrawable& label = m_drawablesPool.emplace_back(textDrawable.GetAtlasData(), labelSettings, isBillboard);
				label.SetBillboardSettings(billboardSettings);
				label.AddText(texts[i]);
				if (i == 2)
				{
					for (int j = 0; j < 3; j++)
					{
						label.AddText("Additional text" + std::to_string(j));
					}
				}
				m_scene.GetRoot()->AddChild(&m_nodesPool[i]);
				m_nodesPool[i].SetMatrix(Math::Utils::translate(glm::mat4x4(1.f), Vector3f(-5 + std::rand() % 5, -5 + std::rand() % 5, -std::rand() % 10)));
				m_nodesPool[i].AddDrawable(&m_drawablesPool[i]);
			}
			
			GetGraphicsAppManager()->GetRenderer()->SetScene(&m_scene);
			m_camController.Init(&m_cam);
			m_camController.SetDefaultKeybindings();
			m_camController.SetPosition({ 0, 0, 10 });
			m_camController.SetBoostFactor(5);
			
			std::shared_ptr<OpenVulkano::Scene::UI::PerformanceInfo> m_perfInfo =
			std::make_shared<OpenVulkano::Scene::UI::PerformanceInfo>();
			m_ui.AddElement(m_perfInfo);
			GetGraphicsAppManager()->GetRenderer()->SetActiveUi(&m_ui);
		}

		void Close() override {}

		void Tick() override
		{
			m_camController.Tick();
		}

	private:
		OpenVulkano::Scene::Scene m_scene;
		PerspectiveCamera m_cam;
		OpenVulkano::FreeCamCameraController m_camController;
		std::vector<LabelDrawable> m_drawablesPool;
		std::vector<Node> m_nodesPool;
		Vector3f_SIMD m_position = { 0, 0, -10 };
		OpenVulkano::Scene::UI::SimpleUi m_ui;
		std::shared_ptr<OpenVulkano::Scene::UI::PerformanceInfo> m_perfInfo;
	};

	IGraphicsApp* LabelDrawableExampleApp::Create() { return new LabelDrawableExampleAppImpl(); }

	std::unique_ptr<IGraphicsApp> LabelDrawableExampleApp::CreateUnique()
	{ 
		return std::make_unique<LabelDrawableExampleAppImpl>();
	}

}

#pragma clang diagnostic pop
#pragma clang diagnostic pop