/*
 * 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 "Drawable.hpp"
#include "Texture.hpp"
#include "VertexBuffer.hpp"
#include "Image/Image.hpp"
#include "Text/FontAtlas.hpp"

namespace OpenVulkano::Scene
{
	class FontAtlas;
	class IFontAtlasGenerator;

	struct TextConfig
	{
		Math::Vector4uc textColor = { 255, 255, 255, 255 };
		Math::Vector4uc backgroundColor = { 0, 255, 0, 0 };
	};

	struct TextGlyphVertex
	{
		std::array<Math::Vector2f, 4> position;
		std::array<Math::Vector2f, 4> uv;
		Math::Vector4uc color = { 255, 255, 255, 255 };
		Math::Vector4uc background = {};
	};

	class TextDrawable : public Drawable
	{
		VertexBuffer m_vertexBuffer;
		std::shared_ptr<FontAtlas> m_atlasData;
		Math::AABB2f m_bbox;
		std::string m_text;
		size_t m_symbolCount = 0;
		TextConfig m_cfg;

		uint32_t GetFallbackGlyph() const;

	public:
		TextDrawable(const TextConfig& config = TextConfig());
		TextDrawable(const std::string& atlasMetadataFile, const TextConfig& config = TextConfig());
		TextDrawable(const Array<char>& atlasMetadata, const TextConfig& config = TextConfig());
		TextDrawable(const std::shared_ptr<FontAtlas>& atlasData, const TextConfig& config = TextConfig());

		void GenerateText(const std::string& text, const Math::Vector2f& pos = Math::Vector2f(0.f));
		void SetConfig(const TextConfig& cfg) { m_cfg = cfg; }
		void SetAtlasData(const std::shared_ptr<FontAtlas>& atlasData);

		[[nodiscard]] Math::AABB2f& GetBoundingBox() { return m_bbox; }
		[[nodiscard]] TextConfig& GetConfig() { return m_cfg; }
		[[nodiscard]] const std::string& GetText() const { return m_text; }
		[[nodiscard]] const std::shared_ptr<FontAtlas>& GetAtlasData() { return m_atlasData; }

		[[nodiscard]] VertexBuffer* GetVertexBuffer() { return &m_vertexBuffer; }
		[[nodiscard]] Texture* GetTexture() { return m_atlasData->GetTexture(); }
		[[nodiscard]] size_t GetSymbolCount() const { return m_symbolCount; }

		[[nodiscard]] static Shader MakeDefaultShader(FontAtlasType type);
		[[nodiscard]] static Shader* GetDefaultShader(FontAtlasType type);
	};
}
