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 = of();
	public static final Vec2b MINUS_ONE = of((byte)-1);
	public static final Vec2b ONE = of((byte)1);
	
	public static Vec2b mutable() { return new Vec2bMutable(); }
	public static Vec2b mutable(byte value) { return new Vec2bMutable(value); }
	public static Vec2b mutable(byte x, byte y) { return new Vec2bMutable(x, y); }
	public static Vec2b mutable(Vec2b value) { return mutable(value.x(), value.y()); }
	
	public static Vec2b of() { return new Vec2bImmutable(); }
	public static Vec2b of(byte value) { return new Vec2bImmutable(value); }
	public static Vec2b of(byte x, byte y) { return new Vec2bImmutable(x, y); }
	public static Vec2b of(Vec2b value) { return of(value.x(), value.y()); }
	
	public byte x();
	public byte y();
	public Vec2b x(byte x);
	public Vec2b y(byte y);
	
	@Override
	public default byte[] asArray() { return new byte[] {x(), y()}; }
	@Override
	public Vec2b copy();
	@Override
	public default Vec2b abs() { return set((byte)Math.abs(x()), (byte)Math.abs(y())); }
	@Override
	public default Vec2b negate() { return set((byte)0, (byte)0); }
	@Override
	public default Vec2b invert() { return set((byte)(-x()), (byte)(-y())); };
	@Override
	public default Vec2b add(byte value) { return add(value, value); }
	public default Vec2b add(Vec2b value) { return add(value.x(), value.y()); }
	public default Vec2b add(byte x, byte y) { return set((byte)(x + x()), (byte)(y + y())); }
	@Override
	public default Vec2b sub(byte value) { return sub(value, value); }
	public default Vec2b sub(Vec2b value) { return sub(value.x(), value.y()); }
	public default Vec2b sub(byte x, byte y) { return set((byte)(x() - x), (byte)(y() - y)); }
	@Override
	public default Vec2b multiply(byte value) { return multiply(value, value); }
	public default Vec2b multiply(Vec2b value) { return multiply(value.x(), value.y()); }
	public default Vec2b multiply(byte x, byte y) { return set((byte)(x * x()), (byte)(y * y())); }
	@Override
	public default Vec2b devide(byte value) { return devide(value, value); }
	public default Vec2b devide(Vec2b value) { return devide(value.x(), value.y()); }
	public default Vec2b devide(byte x, byte y) { return set((byte)(x() / x), (byte)(y() / y)); }
	@Override
	public default Vec2b set(byte value) { return set(value, value); };
	public default Vec2b set(Vec2b value) { return set(value.x(), value.y()); }
	public Vec2b set(byte x, byte y);
	public default double distanceTo(Vec2b value) { return distanceTo(value.x(), value.y()); }
	public default double distanceTo(byte x, byte y) { return Math.sqrt(distanceToSquared(x, y)); }
	public default long distanceToSquared(Vec2b value) { return distanceToSquared(value.x(), value.y()); }
	public default long distanceToSquared(byte x, byte y) {
		long xPos = x() - x;
		long yPos = y() - y;
		return (xPos * xPos) + (yPos * yPos);
	}
	
	@Override
	public default long lengthSquared() { return (x() * x()) + (y() * y()); }
	public default long dotProduct(Vec2b value) { return dotProduct(value.x(), value.y()); }
	public default long dotProduct(byte x, byte y) { return (x() * x) + (y() * y); }
	public default Vec2b rotate(byte angle, Vec2b center) { return rotate(angle, center.x(), center.y()); }
	public default Vec2b rotate(byte angle, byte x, byte y) {
		byte xPos = (byte)(x() - x);
		byte yPos = (byte)(y() - 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.x(), other.y(), 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(x(), x), (byte)Math.min(y(), y)); }
	public default Vec2b max(Vec2b other) { return max(other, this); }
	public default Vec2b max(Vec2b other, Vec2b result) { return max(other.x(), other.y(), 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(x(), x), (byte)Math.max(y(), y)); }
	public default Vec2b difference(Vec2b other) { return difference(other, this); }
	public default Vec2b difference(Vec2b other, Vec2b result) { return difference(other.x(), other.y(), 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)(x() - x), (byte)(y() - 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 ? x() : MathUtils.clamp(min, max, x()), (filter & Y) == 0 ? y() : MathUtils.clamp(min, max, y())); }
	@Override
	public default Vec2b store(ByteBuffer buffer) {
		buffer.put(x()).put(y());
		return this;
	}
	@Override
	public default Vec2b load(ByteBuffer buffer) { return set(buffer.get(), buffer.get()); }
	
	@Override
	public default Vec2s asShort() { return isMutable() ? Vec2s.mutable(x(), y()) : Vec2s.of(x(), y()); }
	@Override
	public default Vec2i asInt() { return isMutable() ? Vec2i.mutable(x(), y()) : Vec2i.of(x(), y()); }
	@Override
	public default Vec2l asLong() { return isMutable() ? Vec2l.mutable(x(), y()) : Vec2l.of(x(), y()); }
	@Override
	public default Vec2f asFloat() { return isMutable() ? Vec2f.mutable(x(), y()) : Vec2f.of(x(), y()); }
	@Override
	public default Vec2d asDouble() { return isMutable() ? Vec2d.mutable(x(), y()) : Vec2d.of(x(), y()); }
	@Override
	public default Vec2b asMutable() { return isMutable() ? this : mutable(this); }
	@Override
	public default Vec2b asImmutable() { return isMutable() ? of(this) : this; }
	@Override
	public default Vec2b copyAsMutable() { return mutable(this); }
	@Override
	public default Vec2b copyAsImmutable() { return of(this); }
}
