/*
 * 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 "TextExampleApp.hpp"
#include "Scene/Scene.hpp"
#include "Scene/Shader/Shader.hpp"
#include "Scene/TextDrawable.hpp"
#include "Scene/Vertex.hpp"
#include "Scene/UI/PerformanceInfo.hpp"
#include "Scene/Text/FontAtlas.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 "Scene/Text/SdfFontAtlasGenerator.hpp"
#include "Scene/Text/BitmapFontAtlasGenerator.hpp"
#include <filesystem>

#ifdef _WIN32
	#undef TRANSPARENT
#endif

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

	constexpr int CREATE_BITMAP_ATLAS = 0;
	//#define CREATE_NEW_ATLAS

	class TextExampleAppImpl final : public TextExampleApp
	{
	public:
		void Init() override
		{
			EngineConfiguration::GetEngineConfiguration()->SetPreferFramebufferFormatSRGB(false);
			
			std::srand(1); // Fix seed for random numbers
			m_scene.Init();
			m_cam.Init(70, 16, 9, 0.1f, 100);
			m_scene.SetCamera(&m_cam);

			static std::vector<std::pair<std::string, TextConfig>> texts;
			texts.push_back(std::make_pair("ABab?.^{}_cdFGETG123)(", TextConfig()));
			texts.push_back(std::make_pair("Hello, World!", TextConfig()));
			texts.push_back(std::make_pair("\u0410\u0411\u0412\u041F", TextConfig()));
			texts.push_back(std::make_pair("Unsupported glyphs \u1E30\u1E31 are coming", TextConfig()));
			texts.push_back(std::make_pair("AAAA, ____", TextConfig()));
			texts.push_back(std::make_pair("0123456789", TextConfig()));
			texts.push_back(std::make_pair("One\tTwo\t\tThree", TextConfig()));
			texts.push_back(std::make_pair("Vertical\vTabs\vHere", TextConfig()));
			texts.push_back(std::make_pair("\"\\\"", TextConfig()));
			texts.push_back(std::make_pair("This is first line\nSecond gg line\nThird G line", TextConfig()));
			texts[1].second.backgroundColor.a = 255;

			constexpr int atlasesCount = 4;
			const int textsCount = texts.size();
			auto& resourceLoader = ResourceLoader::GetInstance();
			const std::string fontPath = resourceLoader.GetResourcePath("Roboto-Regular.ttf");
			m_nodesPool.resize(textsCount * atlasesCount);
			m_drawablesPool.resize(textsCount * atlasesCount);

			if constexpr (CREATE_BITMAP_ATLAS)
			{
				// ReSharper disable once CppDFAUnreachableCode
				std::set<uint32_t> s = BitmapFontAtlasGenerator::LoadAllGlyphs(fontPath);
				BitmapFontAtlasGenerator generator(FontPixelSizeConfig(), SubpixelLayout::RGB);
				generator.GenerateAtlas(fontPath, s);
				generator.GetAtlas()->Save("bitmap_atlas_packed.png");
			}

#if defined(MSDFGEN_AVAILABLE) && defined(CREATE_NEW_ATLAS)
			std::set<uint32_t> s = SdfFontAtlasGenerator::LoadAllGlyphs(fontPath);
			m_atlasGenerator.GenerateAtlas(fontPath, s);
			m_msdfAtlasGenerator.GenerateAtlas(fontPath, s);
			m_atlasGenerator.GetAtlas()->Save("sdf_atlas_packed.png");
			m_msdfAtlasGenerator.GetAtlas()->Save("msdf_atlas_packed.png");
#else 
			auto sdfMetadataInfo = resourceLoader.GetResource("sdf_atlas_packed.png");
			auto msdfMetadataInfo = resourceLoader.GetResource("msdf_atlas_packed.png");
			auto bitmapMetadataInfo = resourceLoader.GetResource("bitmap_atlas_packed.png");
			auto bitmapSubpixelRenderingMetadataInfo = resourceLoader.GetResource("bitmap_subpixel_atlas_packed.png");
#endif
			for (int i = 0, xOffset = -5; i < atlasesCount; i++, xOffset += 20)
			{
				float totalH = 0;
				for (int j = 0; j < texts.size(); j++)
				{
					TextDrawable* t = nullptr;
#if defined(MSDFGEN_AVAILABLE) && defined(CREATE_NEW_ATLAS)
					if (i < texts.size())
					{
						t = new TextDrawable(m_atlasGenerator.GetAtlas(), texts[j].second);
					}
					else
					{
						t = new TextDrawable(m_msdfAtlasGenerator.GetAtlas(), texts[j].second);
					}
#else
					if (i == 0)
					{
						t = new TextDrawable(sdfMetadataInfo, texts[j].second);
					}
					else if (i == 1)
					{
						TextConfig cfg = texts[j].second;
						cfg.minimalSpacingBetweenMultipleLines = false;
						t = new TextDrawable(msdfMetadataInfo, cfg);
					}
					else if (i == 2)
					{
						// bitmap
						t = new TextDrawable(bitmapMetadataInfo, texts[j].second);
					}
					else if (i == 3)
					{
						// bitmap subpixel rendering
						t = new TextDrawable(bitmapSubpixelRenderingMetadataInfo, texts[j].second);
					}
					// OR use separate texture + metadata file
					//auto metadataInfo = resourceLoader.GetResource("atlas_metadata");
					//auto data = resourceLoader.GetResource("roboto-regular-atlas.png");
					//Image::ImageLoaderPng loader;
					//static auto image = loader.loadData(reinterpret_cast<uint8_t*>(data.Data()), data.Size());
					//static Texture tex;
					//tex.resolution = image->resolution;
					//tex.textureBuffer = image->data.Data();
					//tex.format = image->dataFormat;
					//tex.size = image->data.Size(); // 1 channel
					//TextDrawable* t = new TextDrawable(metadataInfo, &tex, texts[i].second);
#endif // MSDFGEN_AVAILABLE
					const int nodeIdx = i * texts.size() + j;
					t->GenerateText(texts[j].first);
					m_drawablesPool[nodeIdx].reset(t);
					m_nodesPool[nodeIdx].Init();
					m_nodesPool[nodeIdx].SetMatrix(
					Math::Utils::translate(glm::mat4x4(1.f), Vector3f(xOffset, 2.f - totalH - (0.5f * j), 0)));
					m_nodesPool[nodeIdx].AddDrawable(m_drawablesPool[nodeIdx].get());
					m_scene.GetRoot()->AddChild(&m_nodesPool[nodeIdx]);
					totalH += t->GetBoundingBox().max.y - t->GetBoundingBox().min.y;
				}
			}

			GetGraphicsAppManager()->GetRenderer()->SetScene(&m_scene);
			m_camController.Init(&m_cam);
			m_camController.SetDefaultKeybindings();
			m_camController.SetPosition({ 10, 0, 15 });
			m_camController.SetBoostFactor(5);

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

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

		void Close() override
		{
			m_drawablesPool.clear();
		}

	private:
		OpenVulkano::Scene::Scene m_scene;
		PerspectiveCamera m_cam;
		FreeCamCameraController m_camController;
#ifdef MSDFGEN_AVAILABLE
		SdfFontAtlasGenerator m_atlasGenerator;
		MsdfFontAtlasGenerator m_msdfAtlasGenerator;
#endif
		std::vector<std::unique_ptr<Drawable>> m_drawablesPool;
		std::vector<Node> m_nodesPool;
		Vector3f_SIMD m_position = { 0, 0, -10 };
		UI::SimpleUi m_ui;
		std::shared_ptr<UI::PerformanceInfo> m_perfInfo;
	};

	IGraphicsApp* TextExampleApp::Create() { return new TextExampleAppImpl(); }

	std::unique_ptr<IGraphicsApp> TextExampleApp::CreateUnique()
	{ 
		return std::make_unique<TextExampleAppImpl>();
	}

}

#pragma clang diagnostic pop
#pragma clang diagnostic pop