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

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

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

	public default long dotProduct(Vec4s value){return dotProduct(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default long dotProduct(short x, short y, short z, short w){return (getX() * x) + (getY() * y) + (getZ() * z) + (getW() * w);};
	
	public default Vec4s min(Vec4s other) {return min(other, this);}
	public default Vec4s min(Vec4s other, Vec4s result){return min(other.getX(), other.getY(), other.getZ(), other.getW(), result);}
	public default Vec4s min(short x, short y, short z, short w) {return min(x, y, z, w, this);}
	public default Vec4s min(short x, short y, short z, short w, Vec4s result){return result.set((short)Math.min(getX(), x), (short)Math.min(getY(), y), (short)Math.min(getZ(), z), (short)Math.min(getW(), w));}
	
	public default Vec4s max(Vec4s other) {return max(other, this);}
	public default Vec4s max(Vec4s other, Vec4s result){return max(other.getX(), other.getY(), other.getZ(), other.getW(), result);}
	public default Vec4s max(short x, short y, short z, short w) {return max(x, y, z, w, this);}
	public default Vec4s max(short x, short y, short z, short w, Vec4s result){return result.set((short)Math.max(getX(), x), (short)Math.max(getY(), y), (short)Math.max(getZ(), z), (short)Math.max(getZ(), z));}
	
	public default Vec4s difference(Vec4s other) {return difference(other, this);}
	public default Vec4s difference(Vec4s other, Vec4s result){return difference(other.getX(), other.getY(), other.getZ(), other.getW(), result);}
	public default Vec4s difference(short x, short y, short z, short w) {return difference(x, y, z, w, this);}
	public default Vec4s difference(short x, short y, short z, short w, Vec4s result){return result.set((short)(getX() - x), (short)(getY() - y), (short)(getZ() - z), (short)(getW() - w));}
	
	@Override
	public default Vec4s clamp(short min, short max){return clamp(min, max, ALL);}
	public default Vec4s clamp(short min, short max, Vec4s result){return clamp(min, max, result, ALL);}
	@Override
	public default Vec4s clamp(short min, short max, int filter){return clamp(min, max, this, filter);}
	public default Vec4s clamp(short min, short max, Vec4s 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 Vec4s store(ByteBuffer buffer)
	{
		buffer.putShort(getX()).putShort(getY()).putShort(getZ()).putShort(getW());
		return this;
	}
	
	@Override
	public default Vec4s load(ByteBuffer buffer)
	{
		return set(buffer.getShort(), buffer.getShort(), buffer.getShort(), buffer.getShort());
	}
	
	@Override
	public default Vec4s store(ShortBuffer buffer)
	{
		buffer.put(getX()).put(getY()).put(getZ()).put(getW());
		return this;
	}
	
	@Override
	public default Vec4s load(ShortBuffer buffer)
	{
		return set(buffer.get(), buffer.get(), buffer.get(), buffer.get());
	}
	
	@Override
	public default Vec4b asByte(){return isMutable() ? Vec4b.newMutable((byte)getX(), (byte)getY(), (byte)getZ(), (byte)getW()) : Vec4b.newVec((byte)getX(), (byte)getY(), (byte)getZ(), (byte)getW());}
	@Override
	public default Vec4i asInt(){return isMutable() ? Vec4i.newMutable(getX(), getY(), getZ(), getW()) : Vec4i.newVec(getX(), getY(), getZ(), getW());}
	@Override
	public default Vec4l asLong(){return isMutable() ? Vec4l.newMutable(getX(), getY(), getZ(), getW()) : Vec4l.newVec(getX(), getY(), getZ(), getW());}
	@Override
	public default Vec4f asFloat() {return isMutable() ? Vec4f.newMutable(getX(), getY(), getZ(), getW()) : Vec4f.newVec(getX(), getY(), getZ(), getW());}
	@Override
	public default Vec4d asDouble(){return isMutable() ? Vec4d.newMutable(getX(), getY(), getZ(), getW()) : Vec4d.newVec(getX(), getY(), getZ(), getW());}

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