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.Vec4b;
import speiger.src.coreengine.math.vector.floats.Vec4f;
import speiger.src.coreengine.math.vector.ints.Vec4i;
import speiger.src.coreengine.math.vector.longs.Vec4l;
import speiger.src.coreengine.math.vector.shorts.Vec4s;

public interface Vec4d extends Vecd
{
	public static final Vec4d ZERO = newVec();
	public static final Vec4d MINUS_ONE = newVec(-1D);
	public static final Vec4d ONE = newVec(1D);
	
	public static Vec4d newMutable(){return new Vec4dMutable();}
	public static Vec4d newMutable(double value){return new Vec4dMutable(value);}
	public static Vec4d newMutable(double x, double y, double z, double w){return new Vec4dMutable(x, y, z, w);}
	public static Vec4d newMutable(Vec4d vec){return newMutable(vec.getX(), vec.getY(), vec.getZ(), vec.getW());}
	
	public static Vec4d newVec(){return new Vec4dImmutable();}
	public static Vec4d newVec(double value){return new Vec4dImmutable(value);}
	public static Vec4d newVec(double x, double y, double z, double w){return new Vec4dImmutable(x, y, z, w);}
	public static Vec4d newVec(Vec4d vec){return newVec(vec.getX(), vec.getY(), vec.getZ(), vec.getW());}
	
	public double getX();
	public double getY();
	public double getZ();
	public double getW();
	public Vec4d setX(double x);
	public Vec4d setY(double y);
	public Vec4d setZ(double z);
	public Vec4d setW(double w);
	@Override
	public default double[] asArray(){return new double[]{getX(), getY(), getZ(), getW()};}
	@Override
	public Vec4d copy();
	@Override
	public default Vec4d abs(){return set(Math.abs(getX()), Math.abs(getY()), Math.abs(getZ()), Math.abs(getW()));}
	@Override
	public default Vec4d negate(){return set(0D, 0D, 0D, 0D);}
	@Override
	public default Vec4d invert(){return set(-getX(), -getY(), -getZ(), -getW());}
	public default Vec4d normalize()
	{
		double l = length();
		return l == 0D ? this : multiply(1.0D / l);
	}
	
	public default Vec4d normalize3D()
	{
		double value = (getX() * getX()) + (getY() * getY()) + (getZ() * getZ());
		return value == 0D ? this : multiply(1D / value);
	}
	
	@Override
	public default Vec4d add(double value){return add(value, value, value, value);}	
	public default Vec4d add(Vec4d value){return add(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default Vec4d add(double x, double y, double z, double w){return set(getX() + x, getY() + y, getZ() + z, getW() + w);}

	@Override
	public default Vec4d sub(double value){return sub(value, value, value, value);}
	public default Vec4d sub(Vec4d value){return sub(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default Vec4d sub(double x, double y, double z, double w){return set(getX() - x, getY() - y, getZ() - z, getW() - w);}
	
	@Override
	public default Vec4d multiply(double value){return multiply(value, value, value, value);}
	public default Vec4d multiply(Vec4d value){return multiply(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default Vec4d multiply(double x, double y, double z, double w){return set(getX() * x, getY() * y, getZ() * z, getW() * w);}
	
	@Override
	public default Vec4d devide(double value){return devide(value, value, value, value);}
	public default Vec4d devide(Vec4d value){return devide(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default Vec4d devide(double x, double y, double z, double w){return set(getX() / x, getY() / y, getZ() / z, getW() / w);}
	
	@Override
	public default Vec4d set(double value){return set(value, value, value, value);}
	public default Vec4d set(Vec4d value){return set(value.getX(), value.getY(), value.getZ(), value.getW());}
	public Vec4d set(double x, double y, double z, double w);
	
	public default double distanceTo(Vec4d value){return distanceTo(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default double distanceTo(double x, double y, double z, double w){return Math.sqrt(distanceTo(x, y, z, w));}
	
	public default double distanceToSquared(Vec4d value){return distanceToSquared(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default double distanceToSquared(double x, double y, double z, double w)
	{
		double xPos = getX() - x;
		double yPos = getY() - y;
		double zPos = getZ() - z;
		double wPos = getW() - w;
		return (xPos * xPos) + (yPos * yPos) + (zPos * zPos) + (wPos * wPos);
	}
	@Override
	public default double lengthSquared() {return (getX() * getX()) + (getY() * getY()) + (getZ() * getZ()) + (getW() * getW());}

	public default double dotProduct(Vec4d value){return dotProduct(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default double dotProduct(double x, double y, double z, double w){return (getX() * x) + (getY() * y) + (getZ() * z) + (getW() * w);};
	
	public default Vec4d lerp(Vec4d value, float progress, Vec4d result){return lerp(value.getX(), value.getY(), value.getZ(), value.getW(), progress, result);}
	public default Vec4d lerp(double x, double y, double z, double w, float progress, Vec4d result){return result.set(MathUtils.lerp(getX(), x, progress), MathUtils.lerp(getY(), y, progress), MathUtils.lerp(getZ(), z, progress), MathUtils.lerp(getW(), w, progress));}
	
	public default double angle(Vec4d value){return angle(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default double angle(double x, double y, double z, double w){return Math.acos(MathUtils.clamp(-1D, 1D, angleCos(x, y, z, w)));}
	
	public default double angleCos(Vec4d value){return angleCos(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default double angleCos(double x, double y, double z, double w){return dotProduct(x, y, z, w) / Math.sqrt(lengthSquared() * (x * x) + (y * y) + (z * z) + (w * w));}
	
	public default Vec4d rotate(double angle, Vec4d axis){return rotate(angle, axis.getX(), axis.getY(), axis.getZ(), axis.getW());}
	public default Vec4d rotate(double angle, Vec4d axis, Vec4d result){return rotate(angle, axis.getX(), axis.getY(), axis.getZ(), axis.getW(), result);}
	public default Vec4d rotate(double angle, double x, double y, double z, double w){return rotate(angle, x, y, z, w, this);}
	public default Vec4d rotate(double angle, double x, double y, double z, double w, Vec4d result)
	{
		double cos = MathUtils.cos(angle);
		double sin = MathUtils.sin(angle);
		double dot = dotProduct(x, y, z, w);
		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, getW());
	}
	public default Vec4d rotateX(double angle){return rotateX(angle, this);}
	public default Vec4d rotateX(double angle, Vec4d 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, getW());
	}
	public default Vec4d rotateY(double angle){return rotateY(angle, this);}
	public default Vec4d rotateY(double angle, Vec4d 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, getW());
	}
	public default Vec4d rotateZ(double angle){return rotateZ(angle, this);}
	public default Vec4d rotateZ(double angle, Vec4d 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(), getW());
	}
	
	public default Vec4d smoothStep(Vec4d value, float progress, Vec4d result){return smoothStep(value.getX(), value.getY(), value.getZ(), value.getW(), progress, result);}
	public default Vec4d smoothStep(double x, double y, double z, double w, float progress, Vec4d 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());
	    double wPos = ((getW() + getW() - w - w) * t3 + (3.0D * w - 3.0D * getW()) * t2 + getW() * progress + getW());
		return result.set(xPos, yPos, zPos, wPos);
	}
	
	public default Vec4d min(Vec4d other) {return min(other, this);}
	public default Vec4d min(Vec4d other, Vec4d result){return min(other.getX(), other.getY(), other.getZ(), other.getW(), result);}
	public default Vec4d min(double x, double y, double z, double w) {return min(x, y, z, w, this);}
	public default Vec4d min(double x, double y, double z, double w, Vec4d result){return result.set(Math.min(getX(), x), Math.min(getY(), y), Math.min(getZ(), z), Math.min(getW(), w));}
	
	public default Vec4d max(Vec4d other) {return max(other, this);}
	public default Vec4d max(Vec4d other, Vec4d result){return max(other.getX(), other.getY(), other.getZ(), other.getW(), result);}
	public default Vec4d max(double x, double y, double z, double w) {return max(x, y, z, w, this);}
	public default Vec4d max(double x, double y, double z, double w, Vec4d result){return result.set(Math.max(getX(), x), Math.max(getY(), y), Math.max(getZ(), z), Math.max(getZ(), z));}
	
	public default Vec4d difference(Vec4d other) {return difference(other, this);}
	public default Vec4d difference(Vec4d other, Vec4d result){return difference(other.getX(), other.getY(), other.getZ(), other.getW(), result);}
	public default Vec4d difference(double x, double y, double z, double w) {return difference(x, y, z, w, this);}
	public default Vec4d difference(double x, double y, double z, double w, Vec4d result){return result.set(getX() - x, getY() - y, getZ() - z, getW() - w);}
	
	@Override
	public default Vec4d clamp(double min, double max){return clamp(min, max, ALL);}
	public default Vec4d clamp(double min, double max, Vec4d result){return clamp(min, max, result, ALL);}
	@Override
	public default Vec4d clamp(double min, double max, int filter){return clamp(min, max, this, filter);}
	public default Vec4d clamp(double min, double max, Vec4d 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()), (filter & W) == 0 ? getW() : MathUtils.clamp(min, max, getW()));}
	
	@Override
	public default Vec4d store(ByteBuffer buffer)
	{
		buffer.putDouble(getX()).putDouble(getY()).putDouble(getZ()).putDouble(getW());
		return this;
	}
	
	@Override
	public default Vec4d load(ByteBuffer buffer)
	{
		return set(buffer.getDouble(), buffer.getDouble(), buffer.getDouble(), buffer.getDouble());
	}
	
	@Override
	public default Vec4d store(DoubleBuffer buffer)
	{
		buffer.put(getX()).put(getY()).put(getZ()).put(getW());
		return this;
	}
	
	@Override
	public default Vec4d load(DoubleBuffer buffer)
	{
		return set(buffer.get(), buffer.get(), buffer.get(), buffer.get());
	}
	
	@Override
	public default Vec4b asByte(){return isMutable() ? Vec4b.newMutable((byte)MathUtils.floor(getX()), (byte)MathUtils.floor(getY()), (byte)MathUtils.floor(getZ()), (byte)MathUtils.floor(getW())) : Vec4b.newVec((byte)MathUtils.floor(getX()), (byte)MathUtils.floor(getY()), (byte)MathUtils.floor(getZ()), (byte)MathUtils.floor(getW()));}
	@Override
	public default Vec4s asShort(){return isMutable() ? Vec4s.newMutable((short)MathUtils.floor(getX()), (short)MathUtils.floor(getY()), (short)MathUtils.floor(getZ()), (short)MathUtils.floor(getW())) : Vec4s.newVec((short)MathUtils.floor(getX()), (short)MathUtils.floor(getY()), (short)MathUtils.floor(getZ()), (short)MathUtils.floor(getW()));}
	@Override
	public default Vec4i asInt(){return isMutable() ? Vec4i.newMutable(MathUtils.floor(getX()), MathUtils.floor(getY()), MathUtils.floor(getZ()), MathUtils.floor(getW())) : Vec4i.newVec(MathUtils.floor(getX()), MathUtils.floor(getY()), MathUtils.floor(getZ()), MathUtils.floor(getW()));}
	@Override
	public default Vec4l asLong() {return isMutable() ? Vec4l.newMutable(MathUtils.floor(getX()), MathUtils.floor(getY()), MathUtils.floor(getZ()), MathUtils.floor(getW())) : Vec4l.newVec(MathUtils.floor(getX()), MathUtils.floor(getY()), MathUtils.floor(getZ()), MathUtils.floor(getW()));}
	@Override
	public default Vec4f asFloat(){return isMutable() ? Vec4f.newMutable((float)getX(), (float)getY(), (float)getZ(), (float)getW()) : Vec4f.newVec((float)getX(), (float)getY(), (float)getZ(), (float)getW());}
	
	@Override
	public default Vec4d asMutable(){return isMutable() ? this : newMutable(this);}
	@Override
	public default Vec4d asImmutable(){return isMutable() ? newVec(this) : this;}
	@Override
	public default Vec4d copyAsMutable(){return newMutable(this);}
	@Override
	public default Vec4d copyAsImmutable(){return newVec(this);}
}
