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

import java.util.Arrays;

import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL45;

public class TextureState implements IGLState {
	int[] textures = new int[GL20.GL_TEXTURE31 - GL20.GL_TEXTURE0];
	
	public TextureState bind(int texture) {
		return bind(0, texture);
	}
	
	public TextureState bind(int unit, int texture) {
		if(textures[unit] != texture) {
			this.textures[unit] = texture;
			GL45.glBindTextureUnit(unit, texture);
		}
		return this;
	}
	
	public TextureState unbind(int unit) {
		if(textures[unit] != 0) {
			textures[unit] = 0;
			GL45.glBindTextureUnit(unit, 0);
		}
		return this;
	}
	
	public TextureState unbindAll() {
		setDefault();
		return this;
	}
		
	public int getTextureId(int unit) {
		return textures[unit];
	}
	
	@Override
	public void setDefault() {
		GL45.nglBindTextures(0, textures.length, 0);
		Arrays.fill(textures, 0);
	}
	
	@Override
	public void reapply() {
		GL45.glBindTextures(0, textures);
	}
}