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

import java.nio.ByteBuffer;

import speiger.src.coreengine.math.MathUtils;
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;
import speiger.src.coreengine.math.vector.shorts.Vec2s;


public interface Vec2b extends Vecb
{
	public static final Vec2b ZERO = newVec();
	public static final Vec2b MINUS_ONE = newVec((byte)-1);
	public static final Vec2b ONE = newVec((byte)1);
	
	public static Vec2b newMutable(){return new Vec2bMutable();}
	public static Vec2b newMutable(byte value){return new Vec2bMutable(value);}
	public static Vec2b newMutable(byte x, byte y){return new Vec2bMutable(x, y);}
	public static Vec2b newMutable(Vec2b value){return newMutable(value.getX(), value.getY());}
	
	public static Vec2b newVec(){return new Vec2bImmutable();}
	public static Vec2b newVec(byte value){return new Vec2bImmutable(value);}
	public static Vec2b newVec(byte x, byte y){return new Vec2bImmutable(x, y);}
	public static Vec2b newVec(Vec2b value){return newVec(value.getX(), value.getY());}
	
	public byte getX();
	public byte getY();
	public Vec2b setX(byte x);
	public Vec2b setY(byte y);
	@Override
	public default byte[] asArray(){return new byte[]{getX(), getY()};}
	@Override
	public Vec2b copy();
	@Override
	public default Vec2b abs(){return set((byte)Math.abs(getX()), (byte)Math.abs(getY()));}
	@Override
	public default Vec2b negate(){return set((byte)0, (byte)0);}
	@Override
	public default Vec2b invert(){return set((byte)(-getX()), (byte)(-getY()));};
	
	@Override
	public default Vec2b add(byte value) {return add(value, value);}
	public default Vec2b add(Vec2b value) {return add(value.getX(), value.getY());}
	public default Vec2b add(byte x, byte y) {return set((byte)(x + getX()), (byte)(y + getY()));}
	
	@Override
	public default Vec2b sub(byte value){return sub(value, value);}
	public default Vec2b sub(Vec2b value){return sub(value.getX(), value.getY());}
	public default Vec2b sub(byte x, byte y) {return set((byte)(getX() - x), (byte)(getY() - y));}
	
	@Override
	public default Vec2b multiply(byte value){return multiply(value, value);}
	public default Vec2b multiply(Vec2b value){return multiply(value.getX(), value.getY());}
	public default Vec2b multiply(byte x, byte y) {return set((byte)(x * getX()), (byte)(y * getY()));}
	
	@Override
	public default Vec2b devide(byte value){return devide(value, value);}
	public default Vec2b devide(Vec2b value){return devide(value.getX(), value.getY());}
	public default Vec2b devide(byte x, byte y){return set((byte)(getX() / x), (byte)(getY() / y));}
	
	@Override
	public default Vec2b set(byte value){return set(value, value);};
	public default Vec2b set(Vec2b value){return set(value.getX(), value.getY());}
	public Vec2b set(byte x, byte y);
	
	public default double distanceTo(Vec2b value){return distanceTo(value.getX(), value.getY());}
	public default double distanceTo(byte x, byte y){return Math.sqrt(distanceToSquared(x, y));}
	
	public default long distanceToSquared(Vec2b value){return distanceToSquared(value.getX(), value.getY());}
	public default long distanceToSquared(byte x, byte 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(Vec2b value){return dotProduct(value.getX(), value.getY());}
	public default long dotProduct(byte x, byte y){return (getX() * x) + (getY() * y);}
	
	public default Vec2b rotate(byte angle, Vec2b center){return rotate(angle, center.getX(), center.getY());}
	public default Vec2b rotate(byte angle, byte x, byte y)
	{
		byte xPos = (byte)(getX() - x);
		byte yPos = (byte)(getY() - y);
		double cos = MathUtils.cos(angle);
		double sin = MathUtils.sin(angle);
	    return set((byte)((xPos * cos) + (yPos * sin) + x), (byte)(-(xPos * sin) + (yPos * cos) + y));
	}
	
	public default Vec2b min(Vec2b other) {return min(other, this);}
	public default Vec2b min(Vec2b other, Vec2b result){return min(other.getX(), other.getY(), result);}
	public default Vec2b min(byte x, byte y) {return min(x, y, this);}
	public default Vec2b min(byte x, byte y, Vec2b result){return result.set((byte)Math.min(getX(), x), (byte)Math.min(getY(), y));}
	
	public default Vec2b max(Vec2b other) {return max(other, this);}
	public default Vec2b max(Vec2b other, Vec2b result){return max(other.getX(), other.getY(), result);}
	public default Vec2b max(byte x, byte y) {return max(x, y, this);}
	public default Vec2b max(byte x, byte y, Vec2b result){return result.set((byte)Math.max(getX(), x), (byte)Math.max(getY(), y));}
	
	public default Vec2b difference(Vec2b other) {return difference(other, this);}
	public default Vec2b difference(Vec2b other, Vec2b result){return difference(other.getX(), other.getY(), result);}
	public default Vec2b difference(byte x, byte y) {return difference(x, y, this);}
	public default Vec2b difference(byte x, byte y, Vec2b result){return result.set((byte)(getX() - x), (byte)(getY() - y));}
	
	@Override
	public default Vec2b clamp(byte min, byte max){return clamp(min, max, ALL);}
	public default Vec2b clamp(byte min, byte max, Vec2b result){return clamp(min, max, result, ALL);}
	@Override
	public default Vec2b clamp(byte min, byte max, int filter){return clamp(min, max, this, filter);}
	public default Vec2b clamp(byte min, byte max, Vec2b 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 Vec2b store(ByteBuffer buffer)
	{
		buffer.put(getX()).put(getY());
		return this;
	}
	
	@Override
	public default Vec2b load(ByteBuffer buffer)
	{
		return set(buffer.get(), buffer.get());
	}
	
	@Override
	public default Vec2s asShort(){return isMutable() ? Vec2s.newMutable(getX(), getY()) : Vec2s.newVec(getX(), 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 Vec2b asMutable(){return isMutable() ? this : newMutable(this);}
	@Override
	public default Vec2b asImmutable(){return isMutable() ? newVec(this) : this;}
	@Override
	public default Vec2b copyAsMutable(){return newMutable(this);}
	@Override
	public default Vec2b copyAsImmutable(){return newVec(this);}
}
