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.Vec2b;
import speiger.src.coreengine.math.vector.doubles.Vec2d;
import speiger.src.coreengine.math.vector.floats.Vec2f;
import speiger.src.coreengine.math.vector.ints.Vec2i;
import speiger.src.coreengine.math.vector.longs.Vec2l;

public interface Vec2s extends Vecs
{
	public static final Vec2s ZERO = newVec();
	public static final Vec2s MINUS_ONE = newVec((short)-1);
	public static final Vec2s ONE = newVec((short)1);
	
	public static Vec2s newMutable(){return new Vec2sMutable();}
	public static Vec2s newMutable(short value){return new Vec2sMutable(value);}
	public static Vec2s newMutable(short x, short y){return new Vec2sMutable(x, y);}
	public static Vec2s newMutable(Vec2s value){return newMutable(value.getX(), value.getY());}
	
	public static Vec2s newVec(){return new Vec2sImmutable();}
	public static Vec2s newVec(short value){return new Vec2sImmutable(value);}
	public static Vec2s newVec(short x, short y){return new Vec2sImmutable(x, y);}
	public static Vec2s newVec(Vec2s value){return newVec(value.getX(), value.getY());}
	
	public short getX();
	public short getY();
	public Vec2s setX(short x);
	public Vec2s setY(short y);
	@Override
	public default short[] asArray(){return new short[]{getX(), getY()};}
	@Override
	public Vec2s copy();
	@Override
	public default Vec2s abs(){return set((short)Math.abs(getX()), (short)Math.abs(getY()));}
	@Override
	public default Vec2s negate(){return set((short)0, (short)0);}
	@Override
	public default Vec2s invert(){return set((short)(-getX()), (short)(-getY()));};
	
	@Override
	public default Vec2s add(short value) {return add(value, value);}
	public default Vec2s add(Vec2s value) {return add(value.getX(), value.getY());}
	public default Vec2s add(short x, short y) {return set((short)(x + getX()), (short)(y + getY()));}
	
	@Override
	public default Vec2s sub(short value){return sub(value, value);}
	public default Vec2s sub(Vec2s value){return sub(value.getX(), value.getY());}
	public default Vec2s sub(short x, short y) {return set((short)(getX() - x), (short)(getY() - y));}
	
	@Override
	public default Vec2s multiply(short value){return multiply(value, value);}
	public default Vec2s multiply(Vec2s value){return multiply(value.getX(), value.getY());}
	public default Vec2s multiply(short x, short y) {return set((short)(x * getX()), (short)(y * getY()));}
	
	@Override
	public default Vec2s devide(short value){return devide(value, value);}
	public default Vec2s devide(Vec2s value){return devide(value.getX(), value.getY());}
	public default Vec2s devide(short x, short y){return set((short)(getX() / x), (short)(getY() / y));}
	
	@Override
	public default Vec2s set(short value){return set(value, value);};
	public default Vec2s set(Vec2s value){return set(value.getX(), value.getY());}
	public Vec2s set(short x, short y);
	
	public default double distanceTo(Vec2s value){return distanceTo(value.getX(), value.getY());}
	public default double distanceTo(short x, short y){return Math.sqrt(distanceToSquared(x, y));}
	
	public default long distanceToSquared(Vec2s value){return distanceToSquared(value.getX(), value.getY());}
	public default long distanceToSquared(short x, short y)
	{
		long xPos = getX() - x;
		long yPos = getY() - y;
		return (xPos * xPos) + (yPos * yPos);
	}
	@Override
	public default long lengthSquared() {return (getX() * getX()) + (getY() * getY());}
	
	public default long dotProduct(Vec2s value){return dotProduct(value.getX(), value.getY());}
	public default long dotProduct(short x, short y){return (getX() * x) + (getY() * y);}
	
	public default Vec2s rotate(short angle, Vec2s center){return rotate(angle, center.getX(), center.getY());}
	public default Vec2s rotate(short angle, short x, short y)
	{
		short xPos = (short)(getX() - x);
		short yPos = (short)(getY() - y);
		double cos = MathUtils.cos(angle);
		double sin = MathUtils.sin(angle);
	    return set((short)((xPos * cos) + (yPos * sin) + x), (short)(-(xPos * sin) + (yPos * cos) + y));
	}
	
	public default Vec2s min(Vec2s other) {return min(other, this);}
	public default Vec2s min(Vec2s other, Vec2s result){return min(other.getX(), other.getY(), result);}
	public default Vec2s min(short x, short y) {return min(x, y, this);}
	public default Vec2s min(short x, short y, Vec2s result){return result.set((short)Math.min(getX(), x), (short)Math.min(getY(), y));}
	
	public default Vec2s max(Vec2s other) {return max(other, this);}
	public default Vec2s max(Vec2s other, Vec2s result){return max(other.getX(), other.getY(), result);}
	public default Vec2s max(short x, short y) {return max(x, y, this);}
	public default Vec2s max(short x, short y, Vec2s result){return result.set((short)Math.max(getX(), x), (short)Math.max(getY(), y));}
	
	public default Vec2s difference(Vec2s other) {return difference(other, this);}
	public default Vec2s difference(Vec2s other, Vec2s result){return difference(other.getX(), other.getY(), result);}
	public default Vec2s difference(short x, short y) {return difference(x, y, this);}
	public default Vec2s difference(short x, short y, Vec2s result){return result.set((short)(getX() - x), (short)(getY() - y));}
	
	@Override
	public default Vec2s clamp(short min, short max){return clamp(min, max, ALL);}
	public default Vec2s clamp(short min, short max, Vec2s result){return clamp(min, max, result, ALL);}
	@Override
	public default Vec2s clamp(short min, short max, int filter){return clamp(min, max, this, filter);}
	public default Vec2s clamp(short min, short max, Vec2s result, int filter){ return result.set((filter & X) == 0 ? getX() : MathUtils.clamp(min, max, getX()), (filter & Y) == 0 ? getY() : MathUtils.clamp(min, max, getY()));}
	
	@Override
	public default Vec2s store(ByteBuffer buffer)
	{
		buffer.putShort(getX()).putShort(getY());
		return this;
	}
	
	@Override
	public default Vec2s load(ByteBuffer buffer)
	{
		return set(buffer.getShort(), buffer.getShort());
	}
	
	@Override
	public default Vec2s store(ShortBuffer buffer)
	{
		buffer.put(getX()).put(getY());
		return this;
	}
	
	@Override
	public default Vec2s load(ShortBuffer buffer)
	{
		return set(buffer.get(), buffer.get());
	}
	
	@Override
	public default Vec2b asByte(){return isMutable() ? Vec2b.newMutable((byte)getX(), (byte)getY()) : Vec2b.newVec((byte)getX(), (byte)getY());}
	@Override
	public default Vec2i asInt(){return isMutable() ? Vec2i.newMutable(getX(), getY()) : Vec2i.newVec(getX(), getY());}
	@Override
	public default Vec2l asLong(){return isMutable() ? Vec2l.newMutable(getX(), getY()) : Vec2l.newVec(getX(), getY());}
	@Override
	public default Vec2f asFloat() {return isMutable() ? Vec2f.newMutable(getX(), getY()) : Vec2f.newVec(getX(), getY());}
	@Override
	public default Vec2d asDouble(){return isMutable() ? Vec2d.newMutable(getX(), getY()) : Vec2d.newVec(getX(), getY());}
	
	@Override
	public default Vec2s asMutable(){return isMutable() ? this : newVec(this);}
	@Override
	public default Vec2s asImmutable(){return isMutable() ? newMutable(this) : this;}
	@Override
	public default Vec2s copyAsMutable(){return newMutable(this);}
	@Override
	public default Vec2s copyAsImmutable(){return newVec(this);}
}
