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

#if __has_include("msdfgen.h")
#include "Text/FontAtlas.hpp"
#include "SdfFontAtlasGenerator.hpp"
#include "Base/Logger.hpp"
#include <msdfgen.h>
#include <msdfgen-ext.h>
#include <msdf-atlas-gen/msdf-atlas-gen.h>

namespace OpenVulkano::Scene
{
	using namespace msdfgen;
	using namespace msdf_atlas;
	
	SdfFontAtlasGeneratorConfig SdfFontAtlasGeneratorConfig::sdfDefaultConfig = { 42, 1.0, 5 };
	SdfFontAtlasGeneratorConfig SdfFontAtlasGeneratorConfig::msdfDefaultConfig = { 32, 1.0, 3 };

	template<int Channels>
	void SdfFontAtlasGeneratorGeneric<Channels>::GenerateAtlas(const std::string& fontFile, const std::set<uint32_t>& charset,
	                                                 const std::optional<std::string>& pngOutput)
	{
		FreetypeHandle* ft;
		FontHandle* font;
		InitFreetypeFromFile(ft, font, fontFile);
		Charset s;
		std::for_each(s.begin(), s.end(), [&](uint32_t unicode) { s.add(unicode); });
		Generate(ft, font, s, pngOutput);
	}

	template<int Channels> SdfFontAtlasGeneratorGeneric<Channels>::SdfFontAtlasGeneratorGeneric() : FontAtlasGeneratorBase(Channels)
	{ 
		if constexpr (Channels == 1) m_config = SdfFontAtlasGeneratorConfig::sdfDefaultConfig;
		else m_config = SdfFontAtlasGeneratorConfig::msdfDefaultConfig;
	}

	template<int Channels>
	void SdfFontAtlasGeneratorGeneric<Channels>::GenerateAtlas(const Array<char>& fontData, const std::set<uint32_t>& charset,
																									 const std::optional<std::string>& pngOutput)
	{
		FreetypeHandle* ft;
		FontHandle* font;
		InitFreetypeFromBuffer(ft, font, (const msdfgen::byte*)(fontData.Data()), fontData.Size());
		Charset s;
		std::for_each(s.begin(), s.end(), [&](uint32_t unicode) { s.add(unicode); });
		Generate(ft, font, s, pngOutput);
	}

	template<int Channels>
	void SdfFontAtlasGeneratorGeneric<Channels>::GenerateAtlas(const std::string& fontFile, const Charset& charset,
	                                           const std::optional<std::string>& pngOutput)
	{
		// TODO: dynamic atlas and add only those symbols which are not present yet in current atlas
		FreetypeHandle* ft;
		FontHandle* font;
		InitFreetypeFromFile(ft, font, fontFile);
		Generate(ft, font, charset, pngOutput);
	}

	template<int Channels>
	void SdfFontAtlasGeneratorGeneric<Channels>::GenerateAtlas(const msdfgen::byte* fontData, int length,
	                                                     const Charset& charset,
	                                           const std::optional<std::string>& pngOutput)
	{
		FreetypeHandle* ft;
		FontHandle* font;
		InitFreetypeFromBuffer(ft, font, fontData, length);
		Generate(ft, font, charset, pngOutput);
	}

	template<int Channels>
	void SdfFontAtlasGeneratorGeneric<Channels>::InitFreetypeFromFile(FreetypeHandle*& ft, FontHandle*& font,
	                                                            const std::string& fontFile)
	{
		ft = initializeFreetype();
		if (!ft) { throw std::runtime_error("Failed to initialize freetype"); }
		font = loadFont(ft, fontFile.data());
		if (!font)
		{
			deinitializeFreetype(ft);
			ft = nullptr;
			throw std::runtime_error(fmt::format("Failed to load font from file {0}", fontFile.data()));
		}
	}

	template<int Channels>
	void SdfFontAtlasGeneratorGeneric<Channels>::InitFreetypeFromBuffer(FreetypeHandle*& ft, FontHandle*& font,
	                                                    const msdfgen::byte* fontData, int length)
	{
		ft = initializeFreetype();
		if (!ft) { throw std::runtime_error("Failed to initialize freetype"); }
		font = loadFontData(ft, fontData, length);
		if (!font)
		{
			deinitializeFreetype(ft);
			ft = nullptr;
			throw std::runtime_error("Failed to load font data from given buffer");
		}
	}

	template<int Channels>
	void SdfFontAtlasGeneratorGeneric<Channels>::Generate(FreetypeHandle* ft, FontHandle* font, const Charset& chset,
	                                      const std::optional<std::string>& pngOutput)
	{

		std::vector<GlyphGeometry> glyphsGeometry;
		// FontGeometry is a helper class that loads a set of glyphs from a single font.
		FontGeometry fontGeometry(&glyphsGeometry);
		fontGeometry.loadCharset(font, 1, chset);

		if constexpr (Channels == 3)
		{
			const double maxCornerAngle = 3.0;
			for (GlyphGeometry& glyph: glyphsGeometry)
				glyph.edgeColoring(&msdfgen::edgeColoringByDistance, maxCornerAngle, 0);
		}

		TightAtlasPacker packer;
		packer.setDimensionsConstraint(DimensionsConstraint::SQUARE);
		int width, height;
		const int glyphsPerRow = std::sqrt(glyphsGeometry.size());
		const int glyphSize = m_config.glyphSize;
		const int rowWidth = glyphSize * glyphsPerRow;
		packer.setDimensions(rowWidth, rowWidth);
		// something to play with. should not be too high.
		// more value - more sdf impact
		packer.setPixelRange(m_config.pixelRange);
		packer.setMiterLimit(m_config.miterLimit);
		packer.pack(glyphsGeometry.data(), glyphsGeometry.size());
		packer.getDimensions(width, height);

		Generator generator;
		generator.resize(width, height);
		GeneratorAttributes attributes;
		generator.setAttributes(attributes);
		generator.setThreadCount(4);
		generator.generate(glyphsGeometry.data(), glyphsGeometry.size());

		int idx = 0;
		m_atlasData = std::make_shared<FontAtlas>(Math::Vector2ui{ width, height }, fontGeometry.getMetrics().lineHeight,
												  channelsCount == 1 ? FontAtlasType::SDF : FontAtlasType::MSDF, m_channelsCount == 1 ? DataFormat::R8_UNORM : DataFormat::R8G8B8A8_UNORM);

		if constexpr (Channels == 3)
		{
			// store RGB as RGBA
			const BitmapConstRef<msdfgen::byte, 3> storage = generator.atlasStorage();
			msdfgen::byte* data = static_cast<msdfgen::byte*>(m_atlasData->GetTexture()->textureBuffer);
			for (size_t srcPos = 0, dstPos = 0; srcPos < width * height * 3; srcPos += 3, dstPos += 4)
			{
				data[dstPos] = storage.pixels[srcPos];
				data[dstPos + 1] = storage.pixels[srcPos + 1];
				data[dstPos + 2] = storage.pixels[srcPos + 2];
				data[dstPos + 3] = 255;
			}
		}
		else
		{ 
			const msdfgen::BitmapConstRef<msdfgen::byte, 1>& storage = generator.atlasStorage();
			memcpy(m_atlasData->GetTexture()->textureBuffer, storage.pixels, width * height);
		}

		struct Bbox
		{
			double l = 0, r = 0, t = 0, b = 0;
		};

		for (const auto& glyph: glyphsGeometry)
		{
			GlyphInfo& info = m_atlasData->GetGlyphs()[glyph.getCodepoint()];
			const GlyphBox& glyphBox = generator.getLayout()[idx++];

			Bbox glyphBaselineBbox, glyphAtlasBbox;
			glyph.getQuadPlaneBounds(glyphBaselineBbox.l, glyphBaselineBbox.b, glyphBaselineBbox.r,
			                         glyphBaselineBbox.t);
			glyph.getQuadAtlasBounds(glyphAtlasBbox.l, glyphAtlasBbox.b, glyphAtlasBbox.r, glyphAtlasBbox.t);
			double bearingX = glyphBox.bounds.l;
			double bearingY = glyphBox.bounds.t;
			double w = glyphBaselineBbox.r - glyphBaselineBbox.l;
			double h = glyphBaselineBbox.t - glyphBaselineBbox.b;
			double l = glyphAtlasBbox.l;
			double r = glyphAtlasBbox.r;
			double t = glyphAtlasBbox.t;
			double b = glyphAtlasBbox.b;

			Math::AABB glyphAtlasAABB;
			glyphAtlasAABB.min.x = l;
			glyphAtlasAABB.min.y = b;
			glyphAtlasAABB.max.x = r;
			glyphAtlasAABB.max.y = t;
			SetGlyphData(info, { bearingX, bearingY }, { w, h }, glyphAtlasAABB, glyphBox.advance);
		}

		if (pngOutput && !pngOutput->empty()) { m_atlasData->Save(*pngOutput); }
		destroyFont(font);
		deinitializeFreetype(ft);
	}

	template class SdfFontAtlasGeneratorGeneric<1>;
	template class SdfFontAtlasGeneratorGeneric<3>;
}
#endif