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