/*
 * 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 "IRecordable.hpp"
#include "Scene/Scene.hpp"
#include "Scene/Geometry.hpp"
#include "Vulkan/Resources/ManagedBuffer.hpp"

namespace OpenVulkano::Vulkan
{
	class VulkanGeometry final : public IRenderResource<Scene::Geometry>
	{
		ManagedBuffer::Ptr m_vertexBuffer;
		ManagedBuffer::Ptr m_indexBuffer;
		vk::IndexType m_indexType;
		vk::DeviceSize m_offsets = 0;

	public:
		VulkanGeometry(Scene::Geometry* geo, ManagedBuffer::Ptr& vertexBuffer, ManagedBuffer::Ptr& indexBuffer)
				: IRenderResource<Scene::Geometry>(geo)
				, m_vertexBuffer(std::move(vertexBuffer)), m_indexBuffer(std::move(indexBuffer))
				, m_indexType((geo->indexType == Scene::VertexIndexType::UINT16) ? vk::IndexType::eUint16 : vk::IndexType::eUint32)
		{}

		~VulkanGeometry() override = default;

		void RecordBind(vk::CommandBuffer& cmdBuffer)
		{
			cmdBuffer.bindVertexBuffers(0, 1, &m_vertexBuffer->buffer, &m_offsets);
			if (m_indexBuffer) { cmdBuffer.bindIndexBuffer(m_indexBuffer->buffer, 0, m_indexType); }
		}

		void RecordDraw(vk::CommandBuffer& cmdBuffer)
		{
			if (GetOwner()->GetIndexCount()) { cmdBuffer.drawIndexed(GetOwner()->GetIndexCount(), 1, 0, 0, 0); }
			else { cmdBuffer.draw(GetOwner()->GetVertexCount(), 1, 0, 0); }
		}

		void Release() override
		{
			//TODO
		}
	};
}
