#include "swapchain_creator.h"
#include "device.h"
#include <algorithm>
namespace vulkanapi {
	SwapchainCreator::SwapchainCreator(Device& device, int frames, uint32_t width, uint32_t height,
		VkSurfaceKHR presentation_surface)
		: device(device)
		, presentation_surface(presentation_surface)
		, frames(frames)
		, width(width)
		, height(height)
		, imageFormat(VK_FORMAT_B8G8R8A8_SRGB)
		, imageColorSpace(VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
		, presentMode(VK_PRESENT_MODE_MAILBOX_KHR)
		, imageUsage(VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)
		, maxFrameInFlightCount(2)
	{

	}
	VkExtent2D SwapchainCreator::EnableImageExtent2D(VkSurfaceCapabilitiesKHR& capabilities)
	{
		VkExtent2D image_extent{ width , height };
		if (capabilities.currentExtent.width != 0xFFFFFFFF)
		{
			image_extent = capabilities.currentExtent;
		}
		else
		{
			image_extent.width = std::clamp(image_extent.width, capabilities.minImageExtent.width, capabilities.maxImageExtent.width);
			image_extent.height = std::clamp(image_extent.height, capabilities.minImageExtent.height, capabilities.maxImageExtent.height);
		}
		return image_extent;
	}
	bool SwapchainCreator::CreateSwapchainImages(VkSwapchainKHR swapchain, vector<VkImage>& swapchain_images)
	{
		uint32_t imageCount = 0;
		vkGetSwapchainImagesKHR(device.Ptr(), swapchain, &imageCount, nullptr);
		vector<VkImage> scImages = vector<VkImage>(imageCount);
		swapchain_images.resize(imageCount);
		vkGetSwapchainImagesKHR(device.Ptr(), swapchain, &imageCount, swapchain_images.data());
		return true;
	}
}