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

import java.nio.ByteBuffer;
import java.nio.ShortBuffer;

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.floats.Vec3f;
import speiger.src.coreengine.math.vector.ints.Vec3i;
import speiger.src.coreengine.math.vector.longs.Vec3l;

public interface Vec3s extends Vecs
{
	public static final Vec3s ZERO = newVec();
	public static final Vec3s MINUS_ONE = newVec((short)-1);
	public static final Vec3s ONE = newVec((short)1);
	
	public static Vec3s newMutable(){return new Vec3sMutable();}
	public static Vec3s newMutable(short value){return new Vec3sMutable(value);}
	public static Vec3s newMutable(short x, short y, short z){return new Vec3sMutable(x, y, z);}
	public static Vec3s newMutable(Vec3s vec){return newMutable(vec.getX(), vec.getY(), vec.getZ());}
	
	public static Vec3s newVec(){return new Vec3sImmutable();}
	public static Vec3s newVec(short value){return new Vec3sImmutable(value);}
	public static Vec3s newVec(short x, short y, short z){return new Vec3sImmutable(x, y, z);}
	public static Vec3s newVec(Vec3s vec){return newVec(vec.getX(), vec.getY(), vec.getZ());}
	
	public short getX();
	public short getY();
	public short getZ();
	public Vec3s setX(short x);
	public Vec3s setY(short y);
	public Vec3s setZ(short z);
	@Override
	public default short[] asArray(){return new short[]{getX(), getY(), getZ()};}
	
	@Override
	public Vec3s copy();
	@Override
	public default Vec3s abs(){return set((short)Math.abs(getX()), (short)Math.abs(getY()), (short)Math.abs(getZ()));}
	@Override
	public default Vec3s negate(){return set((short)0, (short)0, (short)0);}
	@Override
	public default Vec3s invert(){return set((short)-getX(), (short)-getY(), (short)-getZ());}
	@Override
	public default Vec3s add(short value){return add(value, value, value);}	
	public default Vec3s add(Vec3s value){return add(value.getX(), value.getY(), value.getZ());}
	public default Vec3s add(short x, short y, short z){return set((short)(getX() + x), (short)(getY() + y), (short)(getZ() + z));}

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

	@Override
	public default Vec3s devide(short value){return devide(value, value, value);}
	public default Vec3s devide(Vec3s value){return devide(value.getX(), value.getY(), value.getZ());}
	public default Vec3s devide(short x, short y, short z){return set((short)(getX() / x), (short)(getY() / y), (short)(getZ() / z));}
	
	@Override
	public default Vec3s set(short value){return set(value, value, value);}
	public default Vec3s set(Vec3s value){return set(value.getX(), value.getY(), value.getZ());}
	public Vec3s set(short x, short y, short z);
	
	public default double distanceTo(Vec3s value){return distanceTo(value.getX(), value.getY(), value.getZ());}
	public default double distanceTo(short x, short y, short z){return Math.sqrt(distanceToSquared(x, y, z));}
	
	public default long distanceToSquared(Vec3s value){return distanceToSquared(value.getX(), value.getY(), value.getZ());}
	public default long distanceToSquared(short x, short y, short z)
	{
		long xPos = getX() - x;
		long yPos = getY() - y;
		long zPos = getZ() - z;
		return (xPos * xPos) + (yPos * yPos) + (zPos * zPos);
	}
	@Override
	public default long lengthSquared() {return (getX() * getX()) + (getY() * getY()) + (getZ() * getZ());}
	
	public default long dotProduct(Vec3s value){return dotProduct(value.getX(), value.getY(), value.getZ());}
	public default long dotProduct(short x, short y, short z){return (getX() * x) + (getY() * y) + (getZ() * z);}
	
	public default Vec3s min(Vec3s other) {return min(other, this);}
	public default Vec3s min(Vec3s other, Vec3s result){return min(other.getX(), other.getY(), other.getZ(), result);}
	public default Vec3s min(short x, short y, short z) {return min(x, y, z, this);}
	public default Vec3s min(short x, short y, short z, Vec3s result){return result.set((short)Math.min(getX(), x), (short)Math.min(getY(), y), (short)Math.min(getZ(), z));}
	
	public default Vec3s max(Vec3s other) {return max(other, this);}
	public default Vec3s max(Vec3s other, Vec3s result){return max(other.getX(), other.getY(), other.getZ(), result);}
	public default Vec3s max(short x, short y, short z) {return max(x, y, z, this);}
	public default Vec3s max(short x, short y, short z, Vec3s result){return result.set((short)Math.max(getX(), x), (short)Math.max(getY(), y), (short)Math.max(getZ(), z));}
	
	public default Vec3s difference(Vec3s other) {return difference(other, this);}
	public default Vec3s difference(Vec3s other, Vec3s result){return difference(other.getX(), other.getY(), other.getZ(), result);}
	public default Vec3s difference(short x, short y, short z) {return difference(x, y, z, this);}
	public default Vec3s difference(short x, short y, short z, Vec3s result){return result.set((short)(getX() - x), (short)(getY() - y), (short)(getZ() - z));}
	
	@Override
	public default Vec3s clamp(short min, short max){return clamp(min, max, ALL);}
	public default Vec3s clamp(short min, short max, Vec3s result){return clamp(min, max, result, ALL);}
	@Override
	public default Vec3s clamp(short min, short max, int filter){return clamp(min, max, this, filter);}
	public default Vec3s clamp(short min, short max, Vec3s 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 Vec3s store(ByteBuffer buffer)
	{
		buffer.putShort(getX()).putShort(getY()).putShort(getZ());
		return this;
	}
	
	@Override
	public default Vec3s load(ByteBuffer buffer)
	{
		return set(buffer.getShort(), buffer.getShort(), buffer.getShort());
	}
	
	@Override
	public default Vec3s store(ShortBuffer buffer)
	{
		buffer.put(getX()).put(getY()).put(getZ());
		return this;
	}
	
	@Override
	public default Vec3s load(ShortBuffer buffer)
	{
		return set(buffer.get(), buffer.get(), buffer.get());
	}
	
	@Override
	public default Vec3b asByte(){return isMutable() ? Vec3b.newMutable((byte)getX(), (byte)getY(), (byte)getZ()) : Vec3b.newVec((byte)getX(), (byte)getY(), (byte)getZ());}
	@Override
	public default Vec3i asInt(){return isMutable() ? Vec3i.newMutable(getX(), getY(), getZ()) : Vec3i.newVec(getX(), getY(), getZ());}
	@Override
	public default Vec3l asLong(){return isMutable() ? Vec3l.newMutable(getX(), getY(), getZ()) : Vec3l.newVec(getX(), getY(), getZ());}
	@Override
	public default Vec3f asFloat() {return isMutable() ? Vec3f.newMutable(getX(), getY(), getZ()) : Vec3f.newVec(getX(), getY(), getZ());}
	@Override
	public default Vec3d asDouble(){return isMutable() ? Vec3d.newMutable(getX(), getY(), getZ()) : Vec3d.newVec(getX(), getY(), getZ());}

	
	@Override
	public default Vec3s asMutable(){return isMutable() ? this : newVec(this);}
	@Override
	public default Vec3s asImmutable(){return isMutable() ? newMutable(this) : this;}
	@Override
	public default Vec3s copyAsMutable(){return newMutable(this);}
	@Override
	public default Vec3s copyAsImmutable(){return newVec(this);}
}
