package speiger.src.coreengine.rendering.utils.values;

import org.lwjgl.opengl.GL11;

import speiger.src.coreengine.rendering.utils.values.IGLValue.IGLMode;

public enum GLMode implements IGLMode {
	POINT(GL11.GL_POINT, 1),
	LINES(GL11.GL_LINES, 2),
	LINE_STRIP(GL11.GL_LINE_STRIP, 2, true),
	LINE_LOOP(GL11.GL_LINE_LOOP, 2, true),
	TRIANGLES(GL11.GL_TRIANGLES, 3),
	TRIANGLE_STRIP(GL11.GL_TRIANGLE_STRIP, 3, true),
	TRIANGLE_FAN(GL11.GL_TRIANGLE_FAN, 3, true),
	QUADS(GL11.GL_QUADS, 4);
	
	int glMode;
	int length;
	boolean isConnective;
	
	private GLMode(int glMode, int length) {
		this(glMode, length, false);
	}
	
	private GLMode(int glMode, int length, boolean isConnective) {
		this.glMode = glMode;
		this.length = length;
		this.isConnective = isConnective;
		GL11.glDrawElements(glMode, length, glMode, length);
	}
	
	public static GLMode byId(int id) {
		for(GLMode value : GLMode.values()) {
			if(value.glValue() == id) return value;
		}
		throw new ArrayIndexOutOfBoundsException("GL["+id+"] isn't a valid GL Draw Mode");
	}
	
	@Override
	public int glValue() { return glMode; }
	@Override
	public int primitiveLength() { return length; }
	@Override
	public boolean isConnective() { return isConnective; }
	@Override
	public int indeciesCount(int vertecies) {
		return switch(this) {
			case POINT, LINE_STRIP, TRIANGLES, TRIANGLE_STRIP, TRIANGLE_FAN -> vertecies;
			case LINES, QUADS -> vertecies / 4 * 6;
			default -> 0;
		};
	}
}
