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.SimplePool;

public class ViewPortStack
{
	SimplePool<Vec4i> pool = new SimplePool<>(100, Vec4i::newMutable);
	Stack<Vec4i> viewPort = new ObjectArrayList<Vec4i>();
	Vec4i defaultPort = Vec4i.newMutable();
	int inUse = 0;
	
	public void setDefault(int x, int y, int w, int h)
	{
		if(defaultPort.getX() != x || defaultPort.getY() != y || defaultPort.getZ() != w || defaultPort.getW() != 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.getX() != x || current.getY() != y || current.getZ() != w || current.getW() != 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.getX(), current.getY(), current.getZ(), current.getW());
	}
	
	public Vec4i getCurrent()
	{
		return viewPort.isEmpty() ? defaultPort : viewPort.top();
	}
}
