/*
 * 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 <vulkan/vulkan.hpp>

namespace OpenVulkano
{
	namespace Vulkan
	{
		struct CommandHelper : virtual ICloseable
		{
			vk::Device device;
			vk::CommandPool cmdPool;
			vk::CommandBuffer cmdBuffer;
			vk::CommandBufferLevel level;

			CommandHelper() = default;
			~CommandHelper() { if (cmdPool) CommandHelper::Close(); }

			void Init(vk::Device device, uint32_t queueIndex, vk::CommandBufferLevel level = vk::CommandBufferLevel::eSecondary)
			{
				this->level = level;
				this->device = device;
				cmdPool = device.createCommandPool(vk::CommandPoolCreateInfo({}, queueIndex));
				vk::CommandBufferAllocateInfo bufferAllocInfo = { cmdPool, level, 1 };
				cmdBuffer = device.allocateCommandBuffers(bufferAllocInfo)[0];
			}

			void Reset() const
			{
				device.resetCommandPool(cmdPool, {});
			}
			
			vk::CommandBufferLevel GetLevel() const
			{
				return level;
			}

			void Close() override
			{
				device.freeCommandBuffers(cmdPool, 1, &cmdBuffer);
				device.destroyCommandPool(cmdPool);
				cmdPool = nullptr;
			}
		};
	}
}
