package speiger.src.coreengine.rendering.inputOld.camera;

import speiger.src.coreengine.math.vector.floats.Vec2f;
import speiger.src.coreengine.math.vector.floats.Vec3f;
import speiger.src.coreengine.math.vector.floats.Vec4f;
import speiger.src.coreengine.math.vector.matrix.Matrix4f;
import speiger.src.coreengine.rendering.inputOld.window.IWindowListener;
import speiger.src.coreengine.rendering.inputOld.window.Window;
import speiger.src.coreengine.utils.helpers.InternalThreadPools;

public class Frustrum implements IWindowListener
{
	public static boolean FREEZE_FRUSTUM = false;
	Camera cam;
	Matrix4f projectMatrix = new Matrix4f();
	Vec4f[] frustrumPlanes = new Vec4f[6];
	
	public Frustrum(Camera camera)
	{
		cam = camera;
		projectMatrix = new Matrix4f();
		for(int i = 0;i<6;i++)
		{
			frustrumPlanes[i] = Vec4f.mutable();
		}
	}
	
	@Override
	public void onWindowChanged(Window window)
	{
		projectMatrix.load(window.getProjectionMatrix());
		update();
	}
	
	public void update()
	{
		if(FREEZE_FRUSTUM)
		{
			return;
		}
		Matrix4f checkMatrix = InternalThreadPools.MATRIX.get().load(projectMatrix).multiply(cam.getViewMatrix());
		for(int i = 0;i<6;i++)
		{
			checkMatrix.storeFrustrumPlane(i, frustrumPlanes[i]);
		}
		InternalThreadPools.MATRIX.accept(checkMatrix);
	}
	
	public boolean isPointInFrustrum(Vec3f vec)
	{
		return isPointInFrustrum(vec.x(), vec.y(), vec.z());
	}
	
	public boolean isPointInFrustrum(float x, float y, float z)
	{
		return isSphereInFrustrum(x, y, z, 0);
	}
	
	public boolean isSphereInFrustrum(Vec3f vec, float radius)
	{
		return isSphereInFrustrum(vec.x(), vec.y(), vec.z(), radius);
	}
	
	public boolean isSphereInFrustrum(float x, float y, float z, float radius)
	{
		for(int i = 0;i<6;i++)
		{
			if(getDistance(i, x, y, z) < -radius)
			{
				return false;
			}
		}
		return true;
	}
	
	public boolean isCubeInFrustrum(Vec3f min, Vec3f max)
	{
		return isCubeInFrustrum(min.x(), min.y(), min.z(), max.x(), max.y(), max.z());
	}
	
	public boolean isCubeInFrustrum(float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
	{
		for(int i = 0;i<6;i++)
		{
			if(isOutsideOfPlane(i, minX, minY, minZ, maxX, maxY, maxZ))
			{
				return false;
			}
		}
		return true;
	}
	
	public boolean isPlaneInFrustrum(Vec2f min, Vec2f max)
	{
		return isPlaneInFrustrum(min.x(), min.y(), max.x(), min.y());
	}
	
	public boolean isPlaneInFrustrum(float minX, float minZ, float maxX, float maxZ)
	{
		for(int i = 0;i<6;i++)
		{
			if(isOutsideOfPlane(i, minX, minZ, maxX, maxZ))
			{
				return false;
			}
		}
		return true;
	}

	private float getDistance(int index, float x, float y, float z)
	{
		Vec4f vec = frustrumPlanes[index];
		return vec.x() * x + vec.y() * y + vec.z() * z + vec.w();
	}
	
	private boolean isOutsideOfPlane(int index, float minX, float minZ, float maxX, float maxZ)
	{
		Vec4f vec = frustrumPlanes[index];
		return vec.x() * (vec.x() < 0 ? minX : maxX) + vec.z() * (vec.z() < 0 ? minZ : maxZ) >= -vec.w();
	}
	
	private boolean isOutsideOfPlane(int index, float minX, float minY, float minZ, float maxX, float maxY, float maxZ)
	{
		Vec4f vec = frustrumPlanes[index];
		return vec.x() * (vec.x() < 0 ? minX : maxX) + vec.y() * (vec.y() < 0 ? minY : maxY) + vec.z() * (vec.z() < 0 ? minZ : maxZ) < -vec.w();
	}
}
