package net.halbear.Terrain4J.EngineCore.Display;

import org.lwjgl.system.MemoryStack;

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

import static org.lwjgl.stb.STBImage.stbi_load;

public class GLFWImageParser {

    private final ByteBuffer image;
    private int GLFW_Width, GLFW_Height, ImageChannels;

    GLFWImageParser(int width, int height, ByteBuffer image, int ImageChannels) {
        this.image = image;
        this.GLFW_Height = height;
        this.GLFW_Width = width;
        this.ImageChannels = ImageChannels;
    }

    public static GLFWImageParser LoadImage(String path) {
        ByteBuffer image;
        int Width, Height, Channels;
        try (MemoryStack MemStack = MemoryStack.stackPush()) {
            IntBuffer channels = MemStack.mallocInt(1);
            IntBuffer width = MemStack.mallocInt(1);
            IntBuffer height = MemStack.mallocInt(1);
            image = stbi_load(path, width, height, channels, 4);
            if (image == null) {
                System.out.println("Could not load or find image inputted.");
            }
            Width = width.get();
            Height = height.get();
            Channels = channels.get(0);
        }
        return new GLFWImageParser(Width, Height, image, Channels);
    }

    public ByteBuffer GetImage() {
        return image;
    }

    public int GetWidth() {
        return GLFW_Width;
    }

    public int GetHeight() {
        return GLFW_Height;
    }
    public int GetChannels() {
        return ImageChannels;
    }
}
