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

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL14;
import org.lwjgl.opengl.GL33;
import org.lwjgl.opengl.GL43;

import speiger.src.collections.ints.sets.IntOpenHashSet;
import speiger.src.collections.ints.sets.IntSet;
import speiger.src.collections.ints.utils.IntSets;
import speiger.src.coreengine.rendering.utils.values.GLAlphaFunction;
import speiger.src.coreengine.rendering.utils.values.GLBlendFactor;
import speiger.src.coreengine.rendering.utils.values.IGLValue;
import speiger.src.coreengine.rendering.utils.values.IGLValue.ITextureParameter;

public enum GLTextureParameter implements ITextureParameter {
	DEPTH_STENCIL(GL43.GL_DEPTH_STENCIL_TEXTURE_MODE, GLTextureValue.DEPTH_COMPONENT, GLTextureValue.STENCIL_INDEX),
	BASE_LEVEL(GL12.GL_TEXTURE_BASE_LEVEL, true),
	MAX_LEVEL(GL12.GL_TEXTURE_MAX_LEVEL, true),
	MIN_LOD(GL12.GL_TEXTURE_MIN_LOD, false),
	MAX_LOD(GL12.GL_TEXTURE_MAX_LOD, false),
	BORDER_COLOR(GL11.GL_TEXTURE_BORDER_COLOR, false),
	COMPARE_FUNCTION(GL14.GL_TEXTURE_COMPARE_FUNC, GLAlphaFunction.values()),
	COMPARE_MODE(GL14.GL_TEXTURE_COMPARE_MODE, GLTextureValue.COMPARE_REF_TO_TEXTURE, GLTextureValue.NONE),
	MIN_FILTER(GL11.GL_TEXTURE_MIN_FILTER, GLTextureValue.NEAREST, GLTextureValue.LINEAR, GLTextureValue.NEAREST_MIPMAP_NEAREST, GLTextureValue.LINEAR_MIPMAP_NEAREST, GLTextureValue.NEAREST_MIPMAP_LINEAR, GLTextureValue.LINEAR_MIPMAP_LINEAR),
	MAG_FILTER(GL11.GL_TEXTURE_MAG_FILTER, GLTextureValue.NEAREST, GLTextureValue.LINEAR),
	SWIZZLE_R(GL33.GL_TEXTURE_SWIZZLE_R, GLTextureValue.RED, GLTextureValue.GREEN, GLTextureValue.BLUE, GLTextureValue.ALPHA, GLBlendFactor.ZERO, GLBlendFactor.ONE),
	SWIZZLE_G(GL33.GL_TEXTURE_SWIZZLE_G, GLTextureValue.RED, GLTextureValue.GREEN, GLTextureValue.BLUE, GLTextureValue.ALPHA, GLBlendFactor.ZERO, GLBlendFactor.ONE),
	SWIZZLE_B(GL33.GL_TEXTURE_SWIZZLE_B, GLTextureValue.RED, GLTextureValue.GREEN, GLTextureValue.BLUE, GLTextureValue.ALPHA, GLBlendFactor.ZERO, GLBlendFactor.ONE),
	SWIZZLE_A(GL33.GL_TEXTURE_SWIZZLE_A, GLTextureValue.RED, GLTextureValue.GREEN, GLTextureValue.BLUE, GLTextureValue.ALPHA, GLBlendFactor.ZERO, GLBlendFactor.ONE),
	SWIZZLE_RGBA(GL33.GL_TEXTURE_SWIZZLE_RGBA, GLTextureValue.RED, GLTextureValue.GREEN, GLTextureValue.BLUE, GLTextureValue.ALPHA, GLBlendFactor.ZERO, GLBlendFactor.ONE),
	WRAP_S(GL11.GL_TEXTURE_WRAP_S, GLTextureValue.CLAMP_TO_EDGE, GLTextureValue.CLAMP_TO_BORDER, GLTextureValue.MIRRORED_REPEAT, GLTextureValue.REPEAT, GLTextureValue.MIRROR_CLAMP_TO_EDGE),
	WRAP_T(GL11.GL_TEXTURE_WRAP_T, GLTextureValue.CLAMP_TO_EDGE, GLTextureValue.CLAMP_TO_BORDER, GLTextureValue.MIRRORED_REPEAT, GLTextureValue.REPEAT, GLTextureValue.MIRROR_CLAMP_TO_EDGE),
	WRAP_R(GL12.GL_TEXTURE_WRAP_R, GLTextureValue.CLAMP_TO_EDGE, GLTextureValue.CLAMP_TO_BORDER, GLTextureValue.MIRRORED_REPEAT, GLTextureValue.REPEAT, GLTextureValue.MIRROR_CLAMP_TO_EDGE);
	
	int glValue;
	IntSet validValues;
	
	private GLTextureParameter(int glValue, IGLValue... validValues) {
		this.glValue = glValue;
		this.validValues = new IntOpenHashSet(validValues.length);
		for(int i = 0,m=validValues.length;i<m;this.validValues.add(validValues[i++].glValue()));
	}
	
	private GLTextureParameter(int glValue, boolean any) {
		this.glValue = glValue;
		this.validValues = any ? IntSets.empty() : IntSets.singleton(-1);
	}
	
	@Override
	public int glValue() {
		return glValue;
	}

	@Override
	public boolean isValid(int value) {
		return validValues.isEmpty() || validValues.contains(value);
	}
}