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