package speiger.src.coreengine.platform.input.window;

import org.jspecify.annotations.NonNull;
import org.lwjgl.PointerBuffer;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;

public class GLFWUtil {
	@NonNull
	public static String glfwError(int error, long description) {
		return switch(error) {
			case GLFW.GLFW_NO_ERROR -> "GLFW_NO_ERROR";
			case GLFW.GLFW_NOT_INITIALIZED -> "GLFW_NOT_INITIALIZED";
			case GLFW.GLFW_NO_CURRENT_CONTEXT -> "GLFW_NO_CURRENT_CONTEXT";
			case GLFW.GLFW_INVALID_ENUM -> "GLFW_INVALID_ENUM";
			case GLFW.GLFW_INVALID_VALUE -> "GLFW_INVALID_VALUE";
			case GLFW.GLFW_OUT_OF_MEMORY -> "GLFW_OUT_OF_MEMORY";
			case GLFW.GLFW_API_UNAVAILABLE -> "GLFW_API_UNAVAILABLE";
			case GLFW.GLFW_VERSION_UNAVAILABLE -> "GLFW_VERSION_UNAVAILABLE";
			case GLFW.GLFW_PLATFORM_ERROR -> "GLFW_PLATFORM_ERROR";
			case GLFW.GLFW_FORMAT_UNAVAILABLE -> "GLFW_FORMAT_UNAVAILABLE";
			case GLFW.GLFW_NO_WINDOW_CONTEXT -> "GLFW_NO_WINDOW_CONTEXT";
			case GLFW.GLFW_CURSOR_UNAVAILABLE -> "GLFW_CURSOR_UNAVAILABLE";
			case GLFW.GLFW_FEATURE_UNAVAILABLE -> "GLFW_FEATURE_UNAVAILABLE";
			case GLFW.GLFW_FEATURE_UNIMPLEMENTED -> "GLFW_FEATURE_UNIMPLEMENTED";
			case GLFW.GLFW_PLATFORM_UNAVAILABLE -> "GLFW_PLATFORM_UNAVAILABLE";
			default -> "UNKNOWN (" + error + ')';
		} + (description != MemoryUtil.NULL ? ": " + MemoryUtil.memUTF8(description) : "");
	}
	
	@NonNull
    public static String glfwError() {
        try(MemoryStack stack = MemoryStack.stackPush()) {
            PointerBuffer description = stack.callocPointer(1);
            return glfwError(GLFW.glfwGetError(description), description.get(0));
        }
    }
}
