package speiger.src.coreengine.platform.graphics.opengl.utils.states;

import org.lwjgl.opengl.GL11;

public class GLState implements IGLState {
	final int id;
	final boolean defaultValue;
	boolean value;
	
	public GLState(int id) {
		this(id, GL11.glGetBoolean(id));
	}
	
	public GLState(int id, boolean defaultValue) {
		this.id = id;
		this.defaultValue = this.value = defaultValue;
	}
	
	public GLState enable() {
		return set(true);
	}
	
	public GLState disable() {
		return set(false);
	}
	
	public GLState set(boolean value) {
		if(this.value != value) {
			this.value = value;
			setValue(value);
		}
		return this;
	}
	
	public boolean enabled() { return value; }
	
	protected void setValue(boolean value) {
		if(value) GL11.glEnable(id);
		else GL11.glDisable(id);
	}
	
	@Override
	public void reapply() {
		setValue(value);
	}
	
	@Override
	public void setDefault() {
		set(defaultValue);
	}
}
