/*
 * 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 "LabelDrawable.hpp"
#include "Scene/TextDrawable.hpp"
#include "Scene/DrawEncoder.hpp"
#include "Scene/IFontAtlasGenerator.hpp"
#include "Base/Logger.hpp"
#include <optional>

namespace OpenVulkano::Scene
{
	using namespace Math;

	LabelDrawable::LabelDrawable(const std::shared_ptr<AtlasData>& atlasData, const LabelDrawableSettings& settings,
	                             bool isBillboard)
	    : Drawable(DrawEncoder::GetDrawEncoder<LabelDrawable>(), DrawPhase::MAIN)
	{
		if (atlasData->glyphs.empty() || !atlasData->texture.size)
		{
			throw std::runtime_error("Can't create label drawable. Either glyphs or texture is empty");
		}
		m_atlasData = atlasData;
		m_isBillboard = isBillboard;
		SetLabelSettings(settings);
		SetupShaders();
		SetupBuffers();
	}

	void LabelDrawable::SetLabelSettings(const LabelDrawableSettings& settings)
	{
		m_settings = settings;
		m_labelData.color = settings.backgroundColor;
		m_labelData.hasRoundedCorners = settings.hasRoundedCorners;
		m_labelData.hasArrow = settings.hasArrow;
		m_labelData.cornerRadius = settings.cornerRadius * settings.cornerRadius;
		m_labelData.arrowLength = settings.arrowLength;
		m_labelData.arrowWidth = settings.arrowWidth;
	}

	void LabelDrawable::AddText(const std::string& text, const TextConfig& config)
	{
		if (text.empty())
		{
			return;
		}

		TextDrawable& textDrawable = m_texts.emplace_back(m_atlasData, config);
		// do not render glyph's background
		textDrawable.GetConfig().backgroundColor.a = 0;
		textDrawable.SetShader(&m_textShader);
		double lineHeight = m_atlasData->meta.lineHeight;

		textDrawable.GenerateText(text, m_position);
		RecalculateBbox(textDrawable.GetBoundingBox());
		// update position for next text entry
		m_position.y = m_bbox.GetMin().y - lineHeight;

		const auto& min = m_bbox.GetMin();
		const auto& max = m_bbox.GetMax();
		Vertex v, v2, v3, v4;
		const float offset = 0.001;
		const float yOffset = m_settings.hasArrow ? m_settings.arrowLength : 0;
		v.position = Vector3f(min.x - m_settings.horizontalOffset, min.y - m_settings.verticalOffset - yOffset, min.z - offset);
		v2.position = Vector3f(max.x + m_settings.horizontalOffset, min.y - m_settings.verticalOffset - yOffset, min.z - offset);
		v3.position = Vector3f(max.x + m_settings.horizontalOffset, max.y + m_settings.verticalOffset, min.z - offset);
		v4.position = Vector3f(min.x - m_settings.horizontalOffset, max.y + m_settings.verticalOffset, min.z - offset);

		m_labelData.textSize.x = v2.position.x - v.position.x;
		m_labelData.textSize.y = v3.position.y - v.position.y;
		m_labelData.bboxCenter.x = (v2.position.x + v.position.x) / 2;
		m_labelData.bboxCenter.y = (v3.position.y + v.position.y) / 2;
	}

	void LabelDrawable::SetBillboardSettings(const BillboardControlBlock& settings)
	{
		m_billboardSettings = settings;
	}

	void LabelDrawable::RecalculateBbox(const Math::AABB& other)
	{
		if (m_bbox.IsEmpty())
		{
			m_bbox = other;
		}
		else
		{
			auto& currentMin = m_bbox.GetMin();
			auto& currentMax = m_bbox.GetMax();
			currentMin.x = std::min(currentMin.x, other.min.x);
			currentMin.y = std::min(currentMin.y, other.min.y);
			currentMax.x = std::max(currentMax.x, other.max.x);
			currentMax.y = std::max(currentMax.y, other.max.y);
		}
	}

	void LabelDrawable::SetupShaders()
	{
		if (!m_isBillboard)
		{
			m_backgroundShader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/label");
		}
		else
		{
			m_backgroundShader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/labelBillboard");
			// binding for billboard's buffer
			DescriptorSetLayoutBinding binding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
			binding.stageFlags = ShaderProgramType::Type::VERTEX;
			m_backgroundShader.AddDescriptorSetLayoutBinding(binding, 4);
		}
		m_backgroundShader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT, "Shader/label");
		m_backgroundShader.AddDescriptorSetLayoutBinding(Texture::DESCRIPTOR_SET_LAYOUT_BINDING, 2);
		m_backgroundShader.AddDescriptorSetLayoutBinding(UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING, 5);
		m_backgroundShader.topology = Topology::TRIANGLE_STRIP;
		m_backgroundShader.cullMode = CullMode::NONE;
		SetShader(&m_backgroundShader);

		FontAtlasType fontAtlasType(static_cast<FontAtlasType::Type>(m_atlasData->meta.atlasType));
		if (!m_isBillboard)
		{
			m_textShader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/text");
		}
		else
		{
			m_textShader.AddShaderProgram(OpenVulkano::ShaderProgramType::VERTEX, "Shader/billboard");
			DescriptorSetLayoutBinding billboardUniformBinding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
			billboardUniformBinding.stageFlags = ShaderProgramType::Type::VERTEX;
			m_textShader.AddDescriptorSetLayoutBinding(billboardUniformBinding, 4);
		}

		DescriptorSetLayoutBinding textUniformBinding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
		textUniformBinding.stageFlags = ShaderProgramType::FRAGMENT;
		m_textShader.AddDescriptorSetLayoutBinding(textUniformBinding, 3);
		m_textShader.AddShaderProgram(OpenVulkano::ShaderProgramType::FRAGMENT,
		                        std::string(fontAtlasType.GetDefaultFragmentShader()));
		m_textShader.AddVertexInputDescription(OpenVulkano::Vertex::GetVertexInputDescription());
		m_textShader.AddDescriptorSetLayoutBinding(Texture::DESCRIPTOR_SET_LAYOUT_BINDING, 2);
		m_textShader.alphaBlend = true;
		m_textShader.cullMode = CullMode::NONE;
		m_textShader.depthWrite = false;
		m_textShader.depthCompareOp = CompareOp::LESS_OR_EQUAL;
	}

	void LabelDrawable::SetupBuffers()
	{
		m_billboardBuffer.size = sizeof(BillboardControlBlock);
		m_billboardBuffer.data = &m_billboardSettings;
		m_billboardBuffer.setId = 4;
		DescriptorSetLayoutBinding binding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
		binding.stageFlags = ShaderProgramType::Type::VERTEX;
		m_billboardBuffer.binding = binding;

		m_labelBuffer.size = sizeof(LabelUniformData);
		m_labelBuffer.data = &m_labelData;
		m_labelBuffer.setId = 5;
		m_labelBuffer.binding = UniformBuffer::DESCRIPTOR_SET_LAYOUT_BINDING;
	}
}
