package net.halbear.Terrain4J.EngineCore.Vulkan.Rendering.Pipeline.Images;

import net.halbear.Terrain4J.EngineCore.Logic.Rendering.VulkanContext;
import net.halbear.Terrain4J.EngineCore.Vulkan.Structure.DisplayToScreen.ImageView;

import static org.lwjgl.vulkan.VK13.*;
public class Attachment {
    private final Image VkImage;
    private final ImageView VkImageView;
    private boolean DepthAttachment;

    public Attachment(VulkanContext VkCtx, int Width, int Height, int Format, int Usage){
        var ImageData = new Image.ImageData().Width(Width).Height(Height).Format(Format)
                .Usage(Usage | VK_IMAGE_USAGE_SAMPLED_BIT);
        VkImage = new Image(VkCtx, ImageData);

        int AspectMask = 0;
        if((Usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) > 0){
            AspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
            DepthAttachment = false;
        }
        if((Usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) > 0){
            AspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
            DepthAttachment = true;
        }
        var ImageViewData = new ImageView.ImageViewData().Format(VkImage.GetFormat()).AspectMask(AspectMask);
        VkImageView = new ImageView(VkCtx.GetDevice(), VkImage.getVulkanImage(), ImageViewData,false);
    }

    public Image GetVkImage(){return VkImage;}
    public ImageView GetVkImageView(){return VkImageView;}
    public boolean IsDepthAttached(){return DepthAttachment;}

    public void CleanUp(VulkanContext VkCtx){
        VkImageView.cleanup(VkCtx.GetDevice());
        VkImage.CleanUp(VkCtx);
    }
}
