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

import java.nio.ByteBuffer;
import java.nio.FloatBuffer;

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

public interface Vec2f extends Vecf
{
	public static final Vec2f ZERO = newVec();
	public static final Vec2f MINUS_ONE = newVec(-1F);
	public static final Vec2f ONE = newVec(1F);
	
	public static Vec2f newMutable(){return new Vec2fMutable();}
	public static Vec2f newMutable(float value){return new Vec2fMutable(value);}
	public static Vec2f newMutable(float x, float y){return new Vec2fMutable(x, y);}
	public static Vec2f newMutable(Vec2f value){return newMutable(value.getX(), value.getY());}
	
	public static Vec2f newVec(){return new Vec2fImmutable();}
	public static Vec2f newVec(float value){return new Vec2fImmutable(value);}
	public static Vec2f newVec(float x, float y){return new Vec2fImmutable(x, y);}
	public static Vec2f newVec(Vec2f value){return newVec(value.getX(), value.getY());}
	
	public float getX();
	public float getY();
	public Vec2f setX(float x);
	public Vec2f setY(float y);
	@Override
	public default float[] asArray(){return new float[]{getX(), getY()};}
	@Override
	public Vec2f copy();
	@Override
	public default Vec2f abs(){return set(Math.abs(getX()), Math.abs(getY()));}
	@Override
	public default Vec2f negate(){return set(0F, 0F);}
	@Override
	public default Vec2f invert(){return set(-getX(), -getY());};
	public default Vec2f normalize()
	{
		float l = length();
		return l == 0F ? this : multiply(1F / l);
	}
	
	@Override
	public default Vec2f add(float value) {return add(value, value);}
	public default Vec2f add(Vec2f value) {return add(value.getX(), value.getY());}
	public default Vec2f add(float x, float y) {return set(x + getX(), y + getY());}
	
	@Override
	public default Vec2f sub(float value){return sub(value, value);}
	public default Vec2f sub(Vec2f value){return sub(value.getX(), value.getY());}
	public default Vec2f sub(float x, float y) {return set(getX() - x, getY() - y);}
	
	@Override
	public default Vec2f multiply(float value){return multiply(value, value);}
	public default Vec2f multiply(Vec2f value){return multiply(value.getX(), value.getY());}
	public default Vec2f multiply(float x, float y) {return set(x * getX(), y * getY());}
	
	@Override
	public default Vec2f devide(float value){return devide(value, value);}
	public default Vec2f devide(Vec2f value){return devide(value.getX(), value.getY());}
	public default Vec2f devide(float x, float y){return set(getX() / x, getY() / y);}
	
	@Override
	public default Vec2f set(float value){return set(value, value);};
	public default Vec2f set(Vec2f value){return set(value.getX(), value.getY());}
	public Vec2f set(float x, float y);
	
	public default double distanceTo(Vec2f value){return distanceTo(value.getX(), value.getY());}
	public default double distanceTo(float x, float y){return Math.sqrt(distanceToSquared(x, y));}
	
	public default double distanceToSquared(Vec2f value){return distanceToSquared(value.getX(), value.getY());}
	public default double distanceToSquared(float x, float y)
	{
		double xPos = getX() - x;
		double yPos = getY() - y;
		return (xPos * xPos) + (yPos * yPos);
	}
	@Override
	public default float lengthSquared() {return (getX() * getX()) + (getY() * getY());}
	
	public default double dotProduct(Vec2f value){return dotProduct(value.getX(), value.getY());}
	public default double dotProduct(float x, float y){return (getX() * x) + (getY() * y);}
	
	public default Vec2f lerp(Vec2f value, float progress, Vec2f result){return lerp(value.getX(), value.getY(), progress, result);}
	public default Vec2f lerp(float x, float y, float progress, Vec2f result){return result.set(MathUtils.lerp(getX(), x, progress), MathUtils.lerp(getY(), y, progress));};
	
	public default float angle(Vec2f value){return angle(value.getX(), value.getY());}
	public default float angle(float x, float y){return (float)Math.atan2((getX() * y) - (getY() * x), (getX() * x) + (getY() * y));}
	
	public default float directionAngle(Vec2f value){return directionAngle(value.getX(), value.getY());}
	public default float directionAngle(float x, float y){return (float)(-Math.toDegrees(Math.atan2(getX() - x, getY() - y)) % 360D);}
	
	public default Vec2f reflect(Vec2f value){return reflect(value.getX(), value.getY(), this);}
	public default Vec2f reflect(float x, float y){return reflect(x, y, this);};
	public default Vec2f reflect(Vec2f value, Vec2f result){return reflect(value.getX(), value.getY(), result);}
	public default Vec2f reflect(float x, float y, Vec2f result)
	{
		double dot = dotProduct(x, y);
		float x2 = (float)((dot + dot) * x);
		float y2 = (float)((dot + dot) * y);
		return result.set(getX() - x2, getY() - y2);	
	}
	
	public default Vec2f rotate(float angle, Vec2f center){return rotate(angle, center.getX(), center.getY());}
	public default Vec2f rotate(float angle, float x, float y)
	{
		float xPos = getX() - x;
		float 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 Vec2f min(Vec2f other) {return min(other, this);}
	public default Vec2f min(Vec2f other, Vec2f result){return min(other.getX(), other.getY(), result);}
	public default Vec2f min(float x, float y) {return min(x, y, this);}
	public default Vec2f min(float x, float y, Vec2f result){return result.set(Math.min(getX(), x), Math.min(getY(), y));}
	
	public default Vec2f max(Vec2f other) {return max(other, this);}
	public default Vec2f max(Vec2f other, Vec2f result){return max(other.getX(), other.getY(), result);}
	public default Vec2f max(float x, float y) {return max(x, y, this);}
	public default Vec2f max(float x, float y, Vec2f result){return result.set(Math.max(getX(), x), Math.max(getY(), y));}
	
	public default Vec2f difference(Vec2f other) {return difference(other, this);}
	public default Vec2f difference(Vec2f other, Vec2f result){return difference(other.getX(), other.getY(), result);}
	public default Vec2f difference(float x, float y) {return difference(x, y, this);}
	public default Vec2f difference(float x, float y, Vec2f result){return result.set(getX() - x, getY() - y);}
	
	@Override
	public default Vec2f clamp(float min, float max){return clamp(min, max, ALL);}
	public default Vec2f clamp(float min, float max, Vec2f result){return clamp(min, max, result, ALL);}
	@Override
	public default Vec2f clamp(float min, float max, int filter){return clamp(min, max, this, filter);}
	public default Vec2f clamp(float min, float max, Vec2f 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 Vec2f store(ByteBuffer buffer)
	{
		buffer.putFloat(getX()).putFloat(getY());
		return this;
	}
	
	@Override
	public default Vec2f load(ByteBuffer buffer)
	{
		return set(buffer.getFloat(), buffer.getFloat());
	}
	
	@Override
	public default Vec2f store(FloatBuffer buffer)
	{
		buffer.put(getX()).put(getY());
		return this;
	}
	
	@Override
	public default Vec2f load(FloatBuffer 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 Vec2d asDouble(){return isMutable() ? Vec2d.newMutable(getX(), getY()) : Vec2d.newVec(getX(), getY());}

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