package speiger.src.coreengine.rendering.shaderOld;

import java.util.function.Consumer;

import speiger.src.coreengine.math.vector.matrix.Matrix4f;
import speiger.src.coreengine.rendering.inputOld.camera.Camera;
import speiger.src.coreengine.rendering.inputOld.window.IWindowListener;
import speiger.src.coreengine.rendering.inputOld.window.ScaledResolution;
import speiger.src.coreengine.rendering.inputOld.window.Window;
import speiger.src.coreengine.rendering.models.buffers.UniformBuffer;

public class ProjectionBuffer implements IWindowListener, Consumer<Camera>
{
	Camera camera;
	Window window;
	UniformBuffer projectionMatrixBuffer;
	UniformBuffer proViewMatrixBuffer;
	UniformBuffer orthoViewMatrixBuffer;
	final Matrix4f proViewMatrix = new Matrix4f();
	final Matrix4f orthoViewMatrix = new Matrix4f();
	
	public ProjectionBuffer(Camera camera, Window window) {
		this.camera = camera;
		this.window = window;
		projectionMatrixBuffer = new UniformBuffer(true);
		proViewMatrixBuffer = new UniformBuffer(true);
		orthoViewMatrixBuffer = new UniformBuffer(true);
		window.addListener(this, true);
		camera.addListener(this);
	}

	@Override
	public void accept(Camera t) {
		proViewMatrix.load(window.getProjectionMatrix()).multiply(camera.getViewMatrix());
		proViewMatrixBuffer.bind().set(proViewMatrix.getData()).unbind();
	}
	
	@Override
	public void onWindowChanged(Window window) {
		ScaledResolution res = window.getUIFrame();
		orthoViewMatrix.setIdentity().ortho(0, 0, res.getScaledWidth(), res.getScaledHeight(), 1000, -1000);
		projectionMatrixBuffer.bind().set(window.getProjectionMatrix().getData()).unbind();
		orthoViewMatrixBuffer.bind().set(orthoViewMatrix.getData()).unbind();
		accept(camera);
	}
	
	public UniformBuffer getProjectionMatrixBuffer() {
		return projectionMatrixBuffer;
	}
	
	public UniformBuffer getProViewMatrixBuffer() {
		return proViewMatrixBuffer;
	}
	
	public UniformBuffer getOrthoViewMatrixBuffer() {
		return orthoViewMatrixBuffer;
	}
	
	public Matrix4f getProViewMatrix() {
		return proViewMatrix;
	}
	
	public Matrix4f getOrthoViewMatrix() {
		return orthoViewMatrix;
	}
}
