package speiger.src.coreengine.math.vector.floats;

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

import speiger.src.coreengine.math.MathUtils;
import speiger.src.coreengine.math.vector.bytes.Vec3b;
import speiger.src.coreengine.math.vector.doubles.Vec3d;
import speiger.src.coreengine.math.vector.ints.Vec3i;
import speiger.src.coreengine.math.vector.longs.Vec3l;
import speiger.src.coreengine.math.vector.shorts.Vec3s;

public interface Vec3f extends Vecf
{
	public static final Vec3f ZERO = newVec();
	public static final Vec3f MINUS_ONE = newVec(-1F);
	public static final Vec3f ONE = newVec(1F);
	
	public static Vec3f newMutable(){return new Vec3fMutable();}
	public static Vec3f newMutable(float value){return new Vec3fMutable(value);}
	public static Vec3f newMutable(float x, float y, float z){return new Vec3fMutable(x, y, z);}
	public static Vec3f newMutable(Vec3f vec){return newMutable(vec.getX(), vec.getY(), vec.getZ());}
	
	public static Vec3f newVec(){return new Vec3fImmutable();}
	public static Vec3f newVec(float value){return new Vec3fImmutable(value);}
	public static Vec3f newVec(float x, float y, float z){return new Vec3fImmutable(x, y, z);}
	public static Vec3f newVec(Vec3f vec){return newVec(vec.getX(), vec.getY(), vec.getZ());}
	
	public float getX();
	public float getY();
	public float getZ();
	public Vec3f setX(float x);
	public Vec3f setY(float y);
	public Vec3f setZ(float z);
	@Override
	public default float[] asArray(){return new float[]{getX(), getY(), getZ()};}
	
	@Override
	public Vec3f copy();
	@Override
	public default Vec3f abs(){return set(Math.abs(getX()), Math.abs(getY()), Math.abs(getZ()));}
	@Override
	public default Vec3f negate(){return set(0F, 0F, 0F);}
	@Override
	public default Vec3f invert(){return set(-getX(), -getY(), -getZ());}
	public default Vec3f normalize()
	{
		float l = length();
		return l == 0F ? this : multiply(1.0F / l);
	}
	@Override
	public default Vec3f add(float value){return add(value, value, value);}	
	public default Vec3f add(Vec3f value){return add(value.getX(), value.getY(), value.getZ());}
	public default Vec3f add(float x, float y, float z){return set(getX() + x, getY() + y, getZ() + z);}

	@Override
	public default Vec3f sub(float value){return sub(value, value, value);}
	public default Vec3f sub(Vec3f value){return sub(value.getX(), value.getY(), value.getZ());}
	public default Vec3f sub(float x, float y, float z){return set(getX() - x, getY() - y, getZ() - z);}
	
	@Override
	public default Vec3f multiply(float value){return multiply(value, value, value);}
	public default Vec3f multiply(Vec3f value){return multiply(value.getX(), value.getY(), value.getZ());}
	public default Vec3f multiply(float x, float y, float z){return set(getX() * x, getY() * y, getZ() * z);}

	@Override
	public default Vec3f devide(float value){return devide(value, value, value);}
	public default Vec3f devide(Vec3f value){return devide(value.getX(), value.getY(), value.getZ());}
	public default Vec3f devide(float x, float y, float z){return set(getX() / x, getY() / y, getZ() / z);}
	
	@Override
	public default Vec3f set(float value){return set(value, value, value);}
	public default Vec3f set(Vec3f value){return set(value.getX(), value.getY(), value.getZ());}
	public Vec3f set(float x, float y, float z);
	
	public default double distanceTo(Vec3f value){return distanceTo(value.getX(), value.getY(), value.getZ());}
	public default double distanceTo(float x, float y, float z){return Math.sqrt(distanceToSquared(x, y, z));}
	
	public default double distanceToSquared(Vec3f value){return distanceToSquared(value.getX(), value.getY(), value.getZ());}
	public default double distanceToSquared(float x, float y, float z)
	{
		double xPos = getX() - x;
		double yPos = getY() - y;
		double zPos = getZ() - z;
		return (xPos * xPos) + (yPos * yPos) + (zPos * zPos);
	}
	@Override
	public default float lengthSquared() {return (getX() * getX()) + (getY() * getY()) + (getZ() * getZ());}
	
	public default double dotProduct(Vec3f value){return dotProduct(value.getX(), value.getY(), value.getZ());}
	public default double dotProduct(float x, float y, float z){return (getX() * x) + (getY() * y) + (getZ() * z);}
	
	public default Vec3f lerp(Vec3f value, float progress, Vec3f result){return lerp(value.getX(), value.getY(), value.getZ(), progress, result);}
	public default Vec3f lerp(float x, float y, float z, float progress, Vec3f result){return result.set(MathUtils.lerp(getX(), x, progress), MathUtils.lerp(getY(), y, progress), MathUtils.lerp(getZ(), z, progress));}
	
	public default float angle(Vec3f value){return angle(value.getX(), value.getY(), value.getZ());}
	public default float angle(float x, float y, float z){return (float)Math.acos(MathUtils.clamp(-1, 1, angleCos(x, y, z)));}
	
	public default float angleCos(Vec3f value){return angleCos(value.getX(), value.getY(), value.getZ());}
	public default float angleCos(float x, float y, float z)
	{
		double myLength = (getX() * getX()) + (getY() * getY()) + (getZ() * getZ());
		double otherLength = (x * x) + (y * y) + (z * z);
		float dot = (getX() * x) + (getY() * y) + (getZ() * z);
		return dot / (float)(Math.sqrt(myLength * otherLength));
	}
	
	public default Vec3f crossProduct(Vec3f value){return crossProduct(value.getX(), value.getY(), value.getZ(), this);}
	public default Vec3f crossProduct(Vec3f value, Vec3f result){return crossProduct(value.getX(), value.getY(), value.getZ(), result);}
	public default Vec3f crossProduct(float x, float y, float z){return crossProduct(x, y, z, this);}
	public default Vec3f crossProduct(float x, float y, float z, Vec3f result){return result.set((getY() * z) - (getZ() * y), (getZ() * x) - (getX() * z), (getX() * y) - (getY() * x));}
	
	public default Vec3f reflect(Vec3f value){return reflect(value.getX(), value.getY(), value.getZ(), this);}
	public default Vec3f reflect(Vec3f value, Vec3f result){return reflect(value.getX(), value.getY(), value.getZ(), result);}
	public default Vec3f reflect(float x, float y, float z){return reflect(x, y, z, this);}
	public default Vec3f reflect(float x, float y, float z, Vec3f result)
	{
		double dot = dotProduct(x, y, z) * 2D;
		return result.set(getX() - (float)(dot * x), getY() - (float)(dot * y), getZ() - (float)(dot * z));
	}
	
	public default Vec3f rotate(float angle, Vec3f axis){return rotate(angle, axis.getX(), axis.getY(), axis.getZ());}
	public default Vec3f rotate(float angle, Vec3f axis, Vec3f result){return rotate(angle, axis.getX(), axis.getY(), axis.getZ(), result);}
	public default Vec3f rotate(float angle, float x, float y, float z){return rotate(angle, x, y, z, this);}
	public default Vec3f rotate(float angle, float x, float y, float z, Vec3f result)
	{
		double cos = MathUtils.cos(angle);
		double sin = MathUtils.sin(angle);
		double dot = dotProduct(x, y, z);
		double xPos = x * dot * (1D - cos) + (getX() * cos) + (-z * getY() + y * getZ()) * sin;
		double yPos = y * dot * (1D - cos) + (getY() * cos) + (z * getX() - x * getZ()) * sin;
		double zPos = z * dot * (1D - cos) + (getZ() * cos) + (-y * getX() + x * getY()) * sin;		
		return result.set((float)xPos, (float)yPos, (float)zPos);
	}
	public default Vec3f rotateX(float angle){return rotateX(angle, this);}
	public default Vec3f rotateX(float angle, Vec3f result)
	{
		double cos = MathUtils.cos(angle);
		double sin = MathUtils.sin(angle);
		double y = getY() * cos + getZ() * sin;
		double z = getZ() * cos - getY() * sin;
		return result.set(getX(), (float)y, (float)z);
	}
	public default Vec3f rotateY(float angle){return rotateY(angle, this);}
	public default Vec3f rotateY(float angle, Vec3f result)
	{
		double cos = MathUtils.cos(angle);
		double sin = MathUtils.sin(angle);
		double x = getX() * cos + getZ() * sin;
		double z = getZ() * cos - getX() * sin;
		return result.set((float)x, getY(), (float)z);
	}
	public default Vec3f rotateZ(float angle){return rotateZ(angle, this);}
	public default Vec3f rotateZ(float angle, Vec3f result)
	{
		double cos = MathUtils.cos(angle);
		double sin = MathUtils.sin(angle);
		double x = cos * getX() - sin * getY();
		double y = sin * getX() + cos * getY();
		return result.set((float)x, (float)y, getZ());
	}
	
	public default Vec3f smoothStep(Vec3f value, float progress, Vec3f result){return smoothStep(value.getX(), value.getY(), value.getZ(), progress, result);}
	public default Vec3f smoothStep(float x, float y, float z, float progress, Vec3f result)
	{
	    float t2 = progress * progress;
	    float t3 = t2 * progress;
	    float xPos = ((getX() + getX() - x - x) * t3 + (3.0F * x - 3.0F * getX()) * t2 + getX() * progress + getX());
	    float yPos = ((getY() + getY() - y - y) * t3 + (3.0F * y - 3.0F * getY()) * t2 + getY() * progress + getY());
	    float zPos = ((getZ() + getZ() - z - z) * t3 + (3.0F * z - 3.0F * getZ()) * t2 + getZ() * progress + getZ());
		return result.set(xPos, yPos, zPos);
	}

	public default Vec3f min(Vec3f other) {return min(other, this);}
	public default Vec3f min(Vec3f other, Vec3f result){return min(other.getX(), other.getY(), other.getZ(), result);}
	public default Vec3f min(float x, float y, float z) {return min(x, y, z, this);}
	public default Vec3f min(float x, float y, float z, Vec3f result){return result.set(Math.min(getX(), x), Math.min(getY(), y), Math.min(getZ(), z));}
	
	public default Vec3f max(Vec3f other) {return max(other, this);}
	public default Vec3f max(Vec3f other, Vec3f result){return max(other.getX(), other.getY(), other.getZ(), result);}
	public default Vec3f max(float x, float y, float z) {return max(x, y, z, this);}
	public default Vec3f max(float x, float y, float z, Vec3f result){return result.set(Math.max(getX(), x), Math.max(getY(), y), Math.max(getZ(), z));}
	
	public default Vec3f difference(Vec3f other) {return difference(other, this);}
	public default Vec3f difference(Vec3f other, Vec3f result){return difference(other.getX(), other.getY(), other.getZ(), result);}
	public default Vec3f difference(float x, float y, float z) {return difference(x, y, z, this);}
	public default Vec3f difference(float x, float y, float z, Vec3f result){return result.set(getX() - x, getY() - y, getZ() - z);}
	
	@Override
	public default Vec3f clamp(float min, float max){return clamp(min, max, ALL);}
	public default Vec3f clamp(float min, float max, Vec3f result){return clamp(min, max, result, ALL);}
	@Override
	public default Vec3f clamp(float min, float max, int filter){return clamp(min, max, this, filter);}
	public default Vec3f clamp(float min, float max, Vec3f result, int filter){ return result.set((filter & X) == 0 ? getX() : MathUtils.clamp(min, max, getX()), (filter & Y) == 0 ? getY() : MathUtils.clamp(min, max, getY()), (filter & Z) == 0 ? getZ() : MathUtils.clamp(min, max, getZ()));}
	
	@Override
	public default Vec3f store(ByteBuffer buffer)
	{
		buffer.putFloat(getX()).putFloat(getY()).putFloat(getZ());
		return this;
	}
	
	@Override
	public default Vec3f load(ByteBuffer buffer)
	{
		return set(buffer.getFloat(), buffer.getFloat(), buffer.getFloat());
	}
	
	@Override
	public default Vec3f store(FloatBuffer buffer)
	{
		buffer.put(getX()).put(getY()).put(getZ());
		return this;
	}
	
	@Override
	public default Vec3f load(FloatBuffer buffer)
	{
		return set(buffer.get(), buffer.get(), buffer.get());
	}
	
	@Override
	public default Vec3b asByte(){return isMutable() ? Vec3b.newMutable((byte)MathUtils.floor(getX()), (byte)MathUtils.floor(getY()), (byte)MathUtils.floor(getZ())) : Vec3b.newVec((byte)MathUtils.floor(getX()), (byte)MathUtils.floor(getY()), (byte)MathUtils.floor(getZ()));}
	@Override
	public default Vec3s asShort(){return isMutable() ? Vec3s.newMutable((short)MathUtils.floor(getX()), (short)MathUtils.floor(getY()), (short)MathUtils.floor(getZ())) : Vec3s.newVec((short)MathUtils.floor(getX()), (short)MathUtils.floor(getY()), (short)MathUtils.floor(getZ()));}
	@Override
	public default Vec3i asInt(){return isMutable() ? Vec3i.newMutable(MathUtils.floor(getX()), MathUtils.floor(getY()), MathUtils.floor(getZ())) : Vec3i.newVec(MathUtils.floor(getX()), MathUtils.floor(getY()), MathUtils.floor(getZ()));}
	@Override
	public default Vec3l asLong() {return isMutable() ? Vec3l.newMutable(MathUtils.floor(getX()), MathUtils.floor(getY()), MathUtils.floor(getZ())) : Vec3l.newVec(MathUtils.floor(getX()), MathUtils.floor(getY()), MathUtils.floor(getZ()));}
	@Override
	public default Vec3d asDouble(){return isMutable() ? Vec3d.newMutable(getX(), getY(), getZ()) : Vec3d.newVec(getX(), getY(), getZ());}
	
	
	@Override
	public default Vec3f asMutable(){return isMutable() ? this : newMutable(this);}
	@Override
	public default Vec3f asImmutable(){return isMutable() ? newVec(this) : this;}
	@Override
	public default Vec3f copyAsMutable(){return newMutable(this);}
	@Override
	public default Vec3f copyAsImmutable(){return newVec(this);}
}
