/*
 * 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 "SimpleDrawable.hpp"
#include "Scene/Geometry.hpp"
#include "Scene/Shader/Shader.hpp"
#include "Base/Logger.hpp"
#include <magic_enum.hpp>
#include <stdexcept>
#include <cassert>

namespace OpenVulkano::Scene
{
	void SimpleDrawable::Init(Shader* shader, Geometry* mesh, Material* material, UniformBuffer* uniBuffer)
	{
		if (m_mesh || m_material || m_uniBuffer) throw std::runtime_error("Drawable is already initialized.");
		m_mesh = mesh;
		m_material = material;
		m_uniBuffer = uniBuffer;
		SetShader(shader);
	}

	void SimpleDrawable::Init(SimpleDrawable* drawable)
	{
		if (m_mesh || m_material || m_uniBuffer) throw std::runtime_error("Drawable is already initialized.");
		m_mesh = drawable->m_mesh;
		m_material = drawable->m_material;
		m_uniBuffer = drawable->m_uniBuffer;
		SetShader(drawable->GetShader());
	}

	std::optional<RayHit> SimpleDrawable::Intersect(const Ray& ray) const
	{
		if (!m_mesh || !GetShader())
		{
			return {};
		}
		if (m_mesh->aabb.IsEmpty())
		{
			m_mesh->CalculateAABB();
		}
		auto bboxHit = ray.IntersectAABB(m_mesh->aabb);
		if (!bboxHit)
		{
			return {};
		}
		if (GetShader()->topology == Topology::TRIANGLE_LIST)
		{
			if (m_mesh->indexCount != 0)
			{
				assert(m_mesh->indexCount % 3 == 0 && "Topology is TRIANGLE_LIST but index count is not divisible by 3");
				for (int i = 0; i < m_mesh->indexCount; i += 3)
				{
					if (m_mesh->indexType == VertexIndexType::UINT16)
					{
						uint16_t* indices = m_mesh->GetIndices16();
						if (auto hit = ray.IntersectTriangle(m_mesh->vertices[indices[i]].position,
						                                     m_mesh->vertices[indices[i + 1]].position,
						                                     m_mesh->vertices[indices[i + 2]].position))
						{
							return hit;
						}
					}
					else
					{
						uint32_t* indices = m_mesh->GetIndices32();
						if (auto hit = ray.IntersectTriangle(m_mesh->vertices[indices[i]].position,
						                                     m_mesh->vertices[indices[i + 1]].position,
						                                     m_mesh->vertices[indices[i + 2]].position))
						{
							return hit;
						}
					}
				}
			}
			else
			{
				assert(m_mesh->indexCount % 3 == 0 && "Topology is TRIANGLE_LIST but vertex count is not divisible by 3");
				for (int i = 0; i < m_mesh->vertexCount; i += 3)
				{
					if (auto hit = ray.IntersectTriangle(m_mesh->vertices[i].position,
					                                     m_mesh->vertices[i + 1].position,
					                                     m_mesh->vertices[i + 2].position))
					{
						return hit;
					}
				}
			}
		}
		else
		{
			Logger::APP->debug("Bbox is hit, but intersection check for topology {} is not implemented", magic_enum::enum_name(GetShader()->topology));
			return bboxHit;
		}
		return {};
	}
}