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 = of();
	public static final Vec2f MINUS_ONE = of(-1F);
	public static final Vec2f ONE = of(1F);
	
	public static Vec2f mutable() { return new Vec2fMutable(); }
	public static Vec2f mutable(float value) { return new Vec2fMutable(value); }
	public static Vec2f mutable(float x, float y) { return new Vec2fMutable(x, y); }
	public static Vec2f mutable(Vec2f value) { return mutable(value.x(), value.y()); }
	public static Vec2f of() { return new Vec2fImmutable(); }
	public static Vec2f of(float value) { return new Vec2fImmutable(value); }
	public static Vec2f of(float x, float y) { return new Vec2fImmutable(x, y); }
	public static Vec2f of(Vec2f value) { return of(value.x(), value.y()); }
	
	public float x();
	public float y();
	public Vec2f x(float x);
	public Vec2f y(float y);
	
	@Override
	public default float[] asArray() { return new float[] {x(), y()}; }
	@Override
	public Vec2f copy();
	@Override
	public default Vec2f abs() { return set(Math.abs(x()), Math.abs(y())); }
	@Override
	public default Vec2f negate() { return set(0F, 0F); }
	@Override
	public default Vec2f invert() { return set(-x(), -y()); };
	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.x(), value.y()); }
	public default Vec2f add(float x, float y) { return set(x + x(), y + y()); }
	@Override
	public default Vec2f sub(float value) { return sub(value, value); }
	public default Vec2f sub(Vec2f value) { return sub(value.x(), value.y()); }
	public default Vec2f sub(float x, float y) { return set(x() - x, y() - y); }
	@Override
	public default Vec2f multiply(float value) { return multiply(value, value); }
	public default Vec2f multiply(Vec2f value) { return multiply(value.x(), value.y()); }
	public default Vec2f multiply(float x, float y) { return set(x * x(), y * y()); }
	@Override
	public default Vec2f devide(float value) { return devide(value, value); }
	public default Vec2f devide(Vec2f value) { return devide(value.x(), value.y()); }
	public default Vec2f devide(float x, float y) { return set(x() / x, y() / y); }
	@Override
	public default Vec2f set(float value) { return set(value, value); };
	public default Vec2f set(Vec2f value) { return set(value.x(), value.y()); }
	public Vec2f set(float x, float y);
	public default double distanceTo(Vec2f value) { return distanceTo(value.x(), value.y()); }
	public default double distanceTo(float x, float y) { return Math.sqrt(distanceToSquared(x, y)); }
	public default double distanceToSquared(Vec2f value) { return distanceToSquared(value.x(), value.y()); }
	public default double distanceToSquared(float x, float y) {
		double xPos = x() - x;
		double yPos = y() - y;
		return (xPos * xPos) + (yPos * yPos);
	}
	
	@Override
	public default float lengthSquared() { return (x() * x()) + (y() * y()); }
	public default double dotProduct(Vec2f value) { return dotProduct(value.x(), value.y()); }
	public default double dotProduct(float x, float y) { return (x() * x) + (y() * y); }
	public default Vec2f lerp(Vec2f value, float progress, Vec2f result) { return lerp(value.x(), value.y(), progress, result); }
	public default Vec2f lerp(float x, float y, float progress, Vec2f result) { return result.set(MathUtils.lerp(x(), x, progress), MathUtils.lerp(y(), y, progress)); };
	public default float angle(Vec2f value) { return angle(value.x(), value.y()); }
	public default float angle(float x, float y) { return (float)Math.atan2((x() * y) - (y() * x), (x() * x) + (y() * y)); }
	public default float directionAngle(Vec2f value) { return directionAngle(value.x(), value.y()); }
	public default float directionAngle(float x, float y) { return (float)(-Math.toDegrees(Math.atan2(x() - x, y() - y)) % 360D); }
	public default Vec2f reflect(Vec2f value) { return reflect(value.x(), value.y(), 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.x(), value.y(), 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(x() - x2, y() - y2);
	}
	
	public default Vec2f rotate(float angle, Vec2f center) { return rotate(angle, center.x(), center.y()); }
	public default Vec2f rotate(float angle, float x, float y) {
		float xPos = x() - x;
		float yPos = y() - 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.x(), other.y(), 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(x(), x), Math.min(y(), y)); }
	public default Vec2f max(Vec2f other) { return max(other, this); }
	public default Vec2f max(Vec2f other, Vec2f result) { return max(other.x(), other.y(), 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(x(), x), Math.max(y(), y)); }
	public default Vec2f difference(Vec2f other) { return difference(other, this); }
	public default Vec2f difference(Vec2f other, Vec2f result) { return difference(other.x(), other.y(), 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(x() - x, y() - 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 ? x() : MathUtils.clamp(min, max, x()), (filter & Y) == 0 ? y() : MathUtils.clamp(min, max, y())); }
	@Override
	public default Vec2f store(ByteBuffer buffer) {
		buffer.putFloat(x()).putFloat(y());
		return this;
	}
	
	@Override
	public default Vec2f load(ByteBuffer buffer) { return set(buffer.getFloat(), buffer.getFloat()); }
	@Override
	public default Vec2f store(FloatBuffer buffer) {
		buffer.put(x()).put(y());
		return this;
	}
	
	@Override
	public default Vec2f load(FloatBuffer buffer) { return set(buffer.get(), buffer.get()); }
	@Override
	public default Vec2b asByte() { return isMutable() ? Vec2b.mutable((byte)MathUtils.floor(x()), (byte)MathUtils.floor(y())) : Vec2b.of((byte)MathUtils.floor(x()), (byte)MathUtils.floor(y())); }
	@Override
	public default Vec2s asShort() { return isMutable() ? Vec2s.mutable((short)MathUtils.floor(x()), (short)MathUtils.floor(y())) : Vec2s.of((short)MathUtils.floor(x()), (short)MathUtils.floor(y())); }
	@Override
	public default Vec2i asInt() { return isMutable() ? Vec2i.mutable(MathUtils.floor(x()), MathUtils.floor(y())) : Vec2i.of(MathUtils.floor(x()), MathUtils.floor(y())); }
	@Override
	public default Vec2l asLong() { return isMutable() ? Vec2l.mutable(MathUtils.floor(x()), MathUtils.floor(y())) : Vec2l.of(MathUtils.floor(x()), MathUtils.floor(y())); }
	@Override
	public default Vec2d asDouble() { return isMutable() ? Vec2d.mutable(x(), y()) : Vec2d.of(x(), y()); }
	@Override
	public default Vec2f asMutable() { return isMutable() ? this : mutable(this); }
	@Override
	public default Vec2f asImmutable() { return isMutable() ? of(this) : this; }
	@Override
	public default Vec2f copyAsMutable() { return mutable(this); }
	@Override
	public default Vec2f copyAsImmutable() { return of(this); }
}
