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

import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;

import speiger.src.coreengine.math.MathUtils;
import speiger.src.coreengine.math.vector.bytes.Vec3b;
import speiger.src.coreengine.math.vector.floats.Vec3f;
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 Vec3d extends Vecd
{
	public static final Vec3d ZERO = newVec();
	public static final Vec3d MINUS_ONE = newVec(-1D);
	public static final Vec3d ONE = newVec(1D);
	
	public static Vec3d newMutable(){return new Vec3dMutable();}
	public static Vec3d newMutable(double value){return new Vec3dMutable(value);}
	public static Vec3d newMutable(double x, double y, double z){return new Vec3dMutable(x, y, z);}
	public static Vec3d newMutable(Vec3d vec){return newMutable(vec.getX(), vec.getY(), vec.getZ());}
	
	public static Vec3d newVec(){return new Vec3dImmutable();}
	public static Vec3d newVec(double value){return new Vec3dImmutable(value);}
	public static Vec3d newVec(double x, double y, double z){return new Vec3dImmutable(x, y, z);}
	public static Vec3d newVec(Vec3d vec){return newVec(vec.getX(), vec.getY(), vec.getZ());}
	
	public double getX();
	public double getY();
	public double getZ();
	public Vec3d setX(double x);
	public Vec3d setY(double y);
	public Vec3d setZ(double z);
	@Override
	public default double[] asArray(){return new double[]{getX(), getY(), getZ()};}
	
	@Override
	public Vec3d copy();
	@Override
	public default Vec3d abs(){return set(Math.abs(getX()), Math.abs(getY()), Math.abs(getZ()));}
	@Override
	public default Vec3d negate(){return set(0D, 0D, 0D);}
	@Override
	public default Vec3d invert(){return set(-getX(), -getY(), -getZ());}
	public default Vec3d normalize()
	{
		double l = length();
		return l == 0D ? this : multiply(1.0D / l);
	}
	@Override
	public default Vec3d add(double value){return add(value, value, value);}	
	public default Vec3d add(Vec3d value){return add(value.getX(), value.getY(), value.getZ());}
	public default Vec3d add(double x, double y, double z){return set(getX() + x, getY() + y, getZ() + z);}

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

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

	public default Vec3d min(Vec3d other) {return min(other, this);}
	public default Vec3d min(Vec3d other, Vec3d result){return min(other.getX(), other.getY(), other.getZ(), result);}
	public default Vec3d min(double x, double y, double z) {return min(x, y, z, this);}
	public default Vec3d min(double x, double y, double z, Vec3d result){return result.set(Math.min(getX(), x), Math.min(getY(), y), Math.min(getZ(), z));}
	
	public default Vec3d max(Vec3d other) {return max(other, this);}
	public default Vec3d max(Vec3d other, Vec3d result){return max(other.getX(), other.getY(), other.getZ(), result);}
	public default Vec3d max(double x, double y, double z) {return max(x, y, z, this);}
	public default Vec3d max(double x, double y, double z, Vec3d result){return result.set(Math.max(getX(), x), Math.max(getY(), y), Math.max(getZ(), z));}
	
	public default Vec3d difference(Vec3d other) {return difference(other, this);}
	public default Vec3d difference(Vec3d other, Vec3d result){return difference(other.getX(), other.getY(), other.getZ(), result);}
	public default Vec3d difference(double x, double y, double z) {return difference(x, y, z, this);}
	public default Vec3d difference(double x, double y, double z, Vec3d result){return result.set(getX() - x, getY() - y, getZ() - z);}
	
	@Override
	public default Vec3d clamp(double min, double max){return clamp(min, max, ALL);}
	public default Vec3d clamp(double min, double max, Vec3d result){return clamp(min, max, result, ALL);}
	@Override
	public default Vec3d clamp(double min, double max, int filter){return clamp(min, max, this, filter);}
	public default Vec3d clamp(double min, double max, Vec3d 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 Vec3d store(ByteBuffer buffer)
	{
		buffer.putDouble(getX()).putDouble(getY()).putDouble(getZ());
		return this;
	}
	
	@Override
	public default Vec3d load(ByteBuffer buffer)
	{
		return set(buffer.getDouble(), buffer.getDouble(), buffer.getDouble());
	}
	
	@Override
	public default Vec3d store(DoubleBuffer buffer)
	{
		buffer.put(getX()).put(getY()).put(getZ());
		return this;
	}
	
	@Override
	public default Vec3d load(DoubleBuffer 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 Vec3f asFloat(){return isMutable() ? Vec3f.newMutable((float)getX(), (float)getY(), (float)getZ()) : Vec3f.newVec((float)getX(), (float)getY(), (float)getZ());}
	
	@Override
	public default Vec3d asMutable(){return isMutable() ? this : newMutable(this);}
	@Override
	public default Vec3d asImmutable(){return isMutable() ? newVec(this) : this;}
	@Override
	public default Vec3d copyAsMutable(){return newMutable(this);}
	@Override
	public default Vec3d copyAsImmutable(){return newVec(this);}
}
