/*
 * 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 "Base/ICloseable.hpp"
#include "IRecordable.hpp"
#include "Scene/Camera.hpp"
#include "Vulkan/Resources/UniformBuffer.hpp"

namespace OpenVulkano::Vulkan
{
	class VulkanNode : public IRecordable, public ICloseable
	{
	public:
		Scene::Node* node = nullptr;
		UniformBuffer* buffer = nullptr;

		~VulkanNode() override
		{
			if (node) VulkanNode::Close();
		}

		virtual void Init(Scene::Node* node, UniformBuffer* uniformBuffer)
		{
			this->node = node;
			this->buffer = uniformBuffer;
		}

		void Record(VulkanDrawContext* context) override
		{
			buffer->Record(context);
		}

		void Close() override
		{
			if (node) node->renderNode = nullptr;
			delete buffer;
			node = nullptr;
			buffer = nullptr;
		}
	};

	struct VulkanNodeDynamic : VulkanNode
	{
		//uint32_t lastUpdate = -1;

		void Init(Scene::Node* node, UniformBuffer* uniformBuffer) override
		{
			VulkanNode::Init(node, uniformBuffer);
			//lastUpdate = -1;
		}

		void Record(VulkanDrawContext* context) override
		{
			//if(context->currentImageId != lastUpdate) //TODO fix
			{
				//lastUpdate = bufferId;
				buffer->Update(&node->worldMat, sizeof(Math::Matrix4f), context->currentImageId);
			}
			buffer->Record(context);
		}
	};
}
