package speiger.src.coreengine.platform.graphics.opengl.sampler;

import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL12;
import org.lwjgl.opengl.GL33;
import org.lwjgl.opengl.GL45;

import speiger.src.coreengine.platform.graphics.api.sampler.Sampler;
import speiger.src.coreengine.platform.graphics.api.sampler.SamplerSettings;
import speiger.src.coreengine.platform.graphics.opengl.utils.GLUtils;

public class GLSampler extends Sampler {
	int id;
	
	public GLSampler(SamplerSettings settings) {
		super(settings);
		init();
	}
	
	protected void init() {
		this.id = GL45.glCreateSamplers();
		GL45.glSamplerParameteri(id, GL11.GL_TEXTURE_WRAP_S, GLUtils.toGL(settings.wrapS()));
		GL45.glSamplerParameteri(id, GL11.GL_TEXTURE_WRAP_T, GLUtils.toGL(settings.wrapT()));
		settings.wrapR().ifPresent(T -> GL45.glSamplerParameteri(id, GL12.GL_TEXTURE_WRAP_R, GLUtils.toGL(T)));
		GL45.glSamplerParameteri(id, GL11.GL_TEXTURE_WRAP_S, GLUtils.toGL(settings.wrapS()));
		GL45.glSamplerParameteri(id, GL11.GL_TEXTURE_MIN_FILTER, GLUtils.toGL(settings.minSample()));
		GL45.glSamplerParameteri(id, GL11.GL_TEXTURE_MAG_FILTER, GLUtils.toGL(settings.magSample()));
	}
	
	public int id() {
		return id;
	}
	
	@Override
	public boolean isRemoved() {
		return id == 0;
	}
	
	@Override
	public void remove() {
		if(id == 0) return;
		GL33.glDeleteSamplers(id);
	}
	
}
