package speiger.src.coreengine.rendering.utils;

import org.lwjgl.opengl.GL11;

import speiger.src.collections.objects.lists.ObjectArrayList;
import speiger.src.collections.utils.Stack;
import speiger.src.coreengine.math.vector.ints.Vec4i;
import speiger.src.coreengine.utils.collections.pools.IPool;
import speiger.src.coreengine.utils.collections.pools.SimplePool;

public class ViewPortStack {
	IPool<Vec4i> pool = new SimplePool<>(100, Vec4i::mutable);
	Stack<Vec4i> viewPort = new ObjectArrayList<>();
	Vec4i defaultPort = Vec4i.mutable();
	int inUse = 0;
	
	public void setDefault(int x, int y, int w, int h) {
		if(defaultPort.x() != x || defaultPort.y() != y || defaultPort.z() != w || defaultPort.w() != h) {
			defaultPort.set(x, y, w, h);
			if(viewPort.isEmpty()) GL11.glViewport(x, y, w, h);
		}
	}
	
	public void push(int x, int y, int w, int h) {
		Vec4i current = getCurrent();
		if(current.x() != x || current.y() != y || current.z() != w || current.w() != h) {
			viewPort.push(pool.get().set(x, y, w, h));
			GL11.glViewport(x, y, w, h);
		}
	}
	
	public void pop() {
		if(viewPort.isEmpty()) return;
		pool.accept(viewPort.pop());
		Vec4i current = getCurrent();
		GL11.glViewport(current.x(), current.y(), current.z(), current.w());
	}
	
	public Vec4i getCurrent() { return viewPort.isEmpty() ? defaultPort : viewPort.top(); }
}
