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

import java.nio.ByteBuffer;
import java.nio.IntBuffer;

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

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