#include "VulkanFramebuffers.h"

#include "utilities/Logger.h"
#include <vulkan/vulkan_core.h>

void VulkanFramebuffers::Initialize(FFramebufferConfig InConfig)
{
	FramebufferConfig = InConfig;
}

void VulkanFramebuffers::Cleanup()
{
	for (auto Framebuffer : SwapChainFramebuffers)
	{
		vkDestroyFramebuffer(FramebufferConfig.Device, Framebuffer, nullptr);
	}
}

// void VulkanFramebuffers::CreateFramebuffers(VkRenderPass RenderPass, std::vector<VkImageView>
// SwapChainImageViews, VkExtent2D SwapChainExtent)
void VulkanFramebuffers::CreateFramebuffers(VkImageView DepthImageView)
{
	SwapChainFramebuffers.resize(FramebufferConfig.SwapChainImageViews.size());

	for (size_t i = 0; i < FramebufferConfig.SwapChainImageViews.size(); i++)
	{
		std::array<VkImageView, 2> Attachments = { FramebufferConfig.SwapChainImageViews[i],
												   DepthImageView };

		VkFramebufferCreateInfo FramebufferInfo{};
		FramebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
		FramebufferInfo.renderPass = FramebufferConfig.RenderPass;
		FramebufferInfo.attachmentCount = static_cast<uint32_t>(Attachments.size());
		FramebufferInfo.pAttachments = Attachments.data();
		FramebufferInfo.width = FramebufferConfig.SwapChainExtent.width;
		FramebufferInfo.height = FramebufferConfig.SwapChainExtent.height;
		FramebufferInfo.layers = 1;

		if (vkCreateFramebuffer(
				FramebufferConfig.Device,
				&FramebufferInfo,
				nullptr,
				&SwapChainFramebuffers[i]
			)
			!= VK_SUCCESS)
		{
			Log::Error("Failed to create frame buffer!");
		}
		else
		{
			Log::Info("Successfully created framebuffers.");
		}
	}
}
