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

import java.nio.ByteBuffer;
import java.nio.DoubleBuffer;

import speiger.src.coreengine.math.MathUtils;
import speiger.src.coreengine.math.vector.bytes.Vec2b;
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 Vec2d extends Vecd
{
	public static final Vec2d ZERO = newVec();
	public static final Vec2d MINUS_ONE = newVec(-1D);
	public static final Vec2d ONE = newVec(1D);
	
	public static Vec2d newMutable(){return new Vec2dMutable();}
	public static Vec2d newMutable(double value){return new Vec2dMutable(value);}
	public static Vec2d newMutable(double x, double y){return new Vec2dMutable(x, y);}
	public static Vec2d newMutable(Vec2d value){return newMutable(value.getX(), value.getY());}
	
	public static Vec2d newVec(){return new Vec2dImmutable();}
	public static Vec2d newVec(double value){return new Vec2dImmutable(value);}
	public static Vec2d newVec(double x, double y){return new Vec2dImmutable(x, y);}
	public static Vec2d newVec(Vec2d value){return newVec(value.getX(), value.getY());}
	
	public double getX();
	public double getY();
	public Vec2d setX(double x);
	public Vec2d setY(double y);
	@Override
	public default double[] asArray(){return new double[]{getX(), getY()};}
	@Override
	public Vec2d copy();
	@Override
	public default Vec2d abs(){return set(Math.abs(getX()), Math.abs(getY()));}
	@Override
	public default Vec2d negate(){return set(0D, 0D);}
	@Override
	public default Vec2d invert(){return set(-getX(), -getY());};
	public default Vec2d normalize()
	{
		double l = length();
		return l == 0D ? this : multiply(1D / l);
	}
	
	@Override
	public default Vec2d add(double value) {return add(value, value);}
	public default Vec2d add(Vec2d value) {return add(value.getX(), value.getY());}
	public default Vec2d add(double x, double y) {return set(x + getX(), y + getY());}
	
	@Override
	public default Vec2d sub(double value){return sub(value, value);}
	public default Vec2d sub(Vec2d value){return sub(value.getX(), value.getY());}
	public default Vec2d sub(double x, double y) {return set(getX() - x, getY() - y);}
	
	@Override
	public default Vec2d multiply(double value){return multiply(value, value);}
	public default Vec2d multiply(Vec2d value){return multiply(value.getX(), value.getY());}
	public default Vec2d multiply(double x, double y) {return set(x * getX(), y * getY());}
	
	@Override
	public default Vec2d devide(double value){return devide(value, value);}
	public default Vec2d devide(Vec2d value){return devide(value.getX(), value.getY());}
	public default Vec2d devide(double x, double y){return set(getX() / x, getY() / y);}
	
	@Override
	public default Vec2d set(double value){return set(value, value);};
	public default Vec2d set(Vec2d value){return set(value.getX(), value.getY());}
	public Vec2d set(double x, double y);
	
	public default double distanceTo(Vec2d value){return distanceTo(value.getX(), value.getY());}
	public default double distanceTo(double x, double y){return Math.sqrt(distanceToSquared(x, y));}
	
	public default double distanceToSquared(Vec2d value){return distanceToSquared(value.getX(), value.getY());}
	public default double distanceToSquared(double x, double y)
	{
		double xPos = getX() - x;
		double yPos = getY() - y;
		return (xPos * xPos) + (yPos * yPos);
	}
	@Override
	public default double lengthSquared() {return (getX() * getX()) + (getY() * getY());}
	
	public default double dotProduct(Vec2d value){return dotProduct(value.getX(), value.getY());}
	public default double dotProduct(double x, double y){return (getX() * x) + (getY() * y);}
	
	public default Vec2d lerp(Vec2d value, float progress, Vec2d result){return lerp(value.getX(), value.getY(), progress, result);}
	public default Vec2d lerp(double x, double y, float progress, Vec2d result){return result.set(MathUtils.lerp(getX(), x, progress), MathUtils.lerp(getY(), y, progress));};
	
	public default double angle(Vec2d value){return angle(value.getX(), value.getY());}
	public default double angle(double x, double y){return (float)Math.atan2((getX() * y) - (getY() * x), (getX() * x) + (getY() * y));}
	
	public default double directionAngle(Vec2d value){return directionAngle(value.getX(), value.getY());}
	public default double directionAngle(double x, double y){return (float)(-Math.toDegrees(Math.atan2(getX() - x, getY() - y)) % 360D);}
	
	public default Vec2d reflect(Vec2d value){return reflect(value.getX(), value.getY(), this);}
	public default Vec2d reflect(double x, double y){return reflect(x, y, this);};
	public default Vec2d reflect(Vec2d value, Vec2d result){return reflect(value.getX(), value.getY(), result);}
	public default Vec2d reflect(double x, double y, Vec2d result)
	{
		double dot = dotProduct(x, y);
		double x2 = (float)((dot + dot) * x);
		double y2 = (float)((dot + dot) * y);
		return result.set(getX() - x2, getY() - y2);	
	}
	
	public default Vec2d rotate(double angle, Vec2d center){return rotate(angle, center.getX(), center.getY());}
	public default Vec2d rotate(double angle, double x, double y)
	{
		double xPos = getX() - x;
		double yPos = getY() - y;
		double cos = MathUtils.cos(angle);
		double sin = MathUtils.sin(angle);
	    return set((float)((xPos * cos) + (yPos * sin) + x), (float)(-(xPos * sin) + (yPos * cos) + y));
	}
	
	public default Vec2d min(Vec2d other) {return min(other, this);}
	public default Vec2d min(Vec2d other, Vec2d result){return min(other.getX(), other.getY(), result);}
	public default Vec2d min(double x, double y) {return min(x, y, this);}
	public default Vec2d min(double x, double y, Vec2d result){return result.set(Math.min(getX(), x), Math.min(getY(), y));}
	
	public default Vec2d max(Vec2d other) {return max(other, this);}
	public default Vec2d max(Vec2d other, Vec2d result){return max(other.getX(), other.getY(), result);}
	public default Vec2d max(double x, double y) {return max(x, y, this);}
	public default Vec2d max(double x, double y, Vec2d result){return result.set(Math.max(getX(), x), Math.max(getY(), y));}
	
	public default Vec2d difference(Vec2d other) {return difference(other, this);}
	public default Vec2d difference(Vec2d other, Vec2d result){return difference(other.getX(), other.getY(), result);}
	public default Vec2d difference(double x, double y) {return difference(x, y, this);}
	public default Vec2d difference(double x, double y, Vec2d result){return result.set(getX() - x, getY() - y);}
	
	@Override
	public default Vec2d clamp(double min, double max){return clamp(min, max, ALL);}
	public default Vec2d clamp(double min, double max, Vec2d result){return clamp(min, max, result, ALL);}
	@Override
	public default Vec2d clamp(double min, double max, int filter){return clamp(min, max, this, filter);}
	public default Vec2d clamp(double min, double max, Vec2d 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 Vec2d store(ByteBuffer buffer)
	{
		buffer.putDouble(getX()).putDouble(getY());
		return this;
	}
	
	@Override
	public default Vec2d load(ByteBuffer buffer)
	{
		return set(buffer.getDouble(), buffer.getDouble());
	}
	
	@Override
	public default Vec2d store(DoubleBuffer buffer)
	{
		buffer.put(getX()).put(getY());
		return this;
	}
	
	@Override
	public default Vec2d load(DoubleBuffer buffer)
	{
		return set(buffer.get(), buffer.get());
	}
	@Override
	public default Vec2b asByte(){return isMutable() ? Vec2b.newMutable((byte)MathUtils.floor(getX()), (byte)MathUtils.floor(getY())) : Vec2b.newVec((byte)MathUtils.floor(getX()), (byte)MathUtils.floor(getY()));}
	@Override
	public default Vec2s asShort(){return isMutable() ? Vec2s.newMutable((short)MathUtils.floor(getX()), (short)MathUtils.floor(getY())) : Vec2s.newVec((short)MathUtils.floor(getX()), (short)MathUtils.floor(getY()));}
	@Override
	public default Vec2i asInt(){return isMutable() ? Vec2i.newMutable(MathUtils.floor(getX()), MathUtils.floor(getY())) : Vec2i.newVec(MathUtils.floor(getX()), MathUtils.floor(getY()));}
	@Override
	public default Vec2l asLong() {return isMutable() ? Vec2l.newMutable(MathUtils.floor(getX()), MathUtils.floor(getY())) : Vec2l.newVec(MathUtils.floor(getX()), MathUtils.floor(getY()));}
	@Override
	public default Vec2f asFloat(){return isMutable() ? Vec2f.newMutable((float)getX(), (float)getY()) : Vec2f.newVec((float)getX(), (float)getY());}

	
	@Override
	public default Vec2d asMutable(){return isMutable() ? this : newMutable(this);}
	@Override
	public default Vec2d asImmutable(){return isMutable() ? newVec(this) : this;}
	@Override
	public default Vec2d copyAsMutable(){return newMutable(this);}
	@Override
	public default Vec2d copyAsImmutable(){return newVec(this);}
}
