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 = of();
	public static final Vec2d MINUS_ONE = of(-1D);
	public static final Vec2d ONE = of(1D);
	
	public static Vec2d mutable() { return new Vec2dMutable(); }
	public static Vec2d mutable(double value) { return new Vec2dMutable(value); }
	public static Vec2d mutable(double x, double y) { return new Vec2dMutable(x, y); }
	public static Vec2d mutable(Vec2d value) { return mutable(value.x(), value.y()); }
	public static Vec2d of() { return new Vec2dImmutable(); }
	public static Vec2d of(double value) { return new Vec2dImmutable(value); }
	public static Vec2d of(double x, double y) { return new Vec2dImmutable(x, y); }
	public static Vec2d of(Vec2d value) { return of(value.x(), value.y()); }
	
	public double x();
	public double y();
	public Vec2d x(double x);
	public Vec2d y(double y);
	@Override
	public default double[] asArray() { return new double[] {x(), y()}; }
	@Override
	public Vec2d copy();
	@Override
	public default Vec2d abs() { return set(Math.abs(x()), Math.abs(y())); }
	@Override
	public default Vec2d negate() { return set(0D, 0D); }
	@Override
	public default Vec2d invert() { return set(-x(), -y()); };
	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.x(), value.y()); }
	public default Vec2d add(double x, double y) { return set(x + x(), y + y()); }
	@Override
	public default Vec2d sub(double value) { return sub(value, value); }
	public default Vec2d sub(Vec2d value) { return sub(value.x(), value.y()); }
	public default Vec2d sub(double x, double y) { return set(x() - x, y() - y); }
	@Override
	public default Vec2d multiply(double value) { return multiply(value, value); }
	public default Vec2d multiply(Vec2d value) { return multiply(value.x(), value.y()); }
	public default Vec2d multiply(double x, double y) { return set(x * x(), y * y()); }
	@Override
	public default Vec2d devide(double value) { return devide(value, value); }
	public default Vec2d devide(Vec2d value) { return devide(value.x(), value.y()); }
	public default Vec2d devide(double x, double y) { return set(x() / x, y() / y); }
	@Override
	public default Vec2d set(double value) { return set(value, value); };
	public default Vec2d set(Vec2d value) { return set(value.x(), value.y()); }
	public Vec2d set(double x, double y);
	
	public default double distanceTo(Vec2d value) { return distanceTo(value.x(), value.y()); }
	public default double distanceTo(double x, double y) { return Math.sqrt(distanceToSquared(x, y)); }
	public default double distanceToSquared(Vec2d value) { return distanceToSquared(value.x(), value.y()); }
	public default double distanceToSquared(double x, double y) {
		double xPos = x() - x;
		double yPos = y() - y;
		return (xPos * xPos) + (yPos * yPos);
	}
	
	@Override
	public default double lengthSquared() { return (x() * x()) + (y() * y()); }
	public default double dotProduct(Vec2d value) { return dotProduct(value.x(), value.y()); }
	public default double dotProduct(double x, double y) { return (x() * x) + (y() * y); }
	public default Vec2d lerp(Vec2d value, float progress, Vec2d result) { return lerp(value.x(), value.y(), progress, result); }
	public default Vec2d lerp(double x, double y, float progress, Vec2d result) { return result.set(MathUtils.lerp(x(), x, progress), MathUtils.lerp(y(), y, progress)); };
	public default double angle(Vec2d value) { return angle(value.x(), value.y()); }
	public default double angle(double x, double y) { return (float)Math.atan2((x() * y) - (y() * x), (x() * x) + (y() * y)); }
	public default double directionAngle(Vec2d value) { return directionAngle(value.x(), value.y()); }
	public default double directionAngle(double x, double y) { return (float)(-Math.toDegrees(Math.atan2(x() - x, y() - y)) % 360D); }
	public default Vec2d reflect(Vec2d value) { return reflect(value.x(), value.y(), 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.x(), value.y(), 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(x() - x2, y() - y2);
	}
	
	public default Vec2d rotate(double angle, Vec2d center) { return rotate(angle, center.x(), center.y()); }
	public default Vec2d rotate(double angle, double x, double y) {
		double xPos = x() - x;
		double 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 Vec2d min(Vec2d other) { return min(other, this); }
	public default Vec2d min(Vec2d other, Vec2d result) { return min(other.x(), other.y(), 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(x(), x), Math.min(y(), y)); }
	public default Vec2d max(Vec2d other) { return max(other, this); }
	public default Vec2d max(Vec2d other, Vec2d result) { return max(other.x(), other.y(), 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(x(), x), Math.max(y(), y)); }
	public default Vec2d difference(Vec2d other) { return difference(other, this); }
	public default Vec2d difference(Vec2d other, Vec2d result) { return difference(other.x(), other.y(), 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(x() - x, y() - 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 ? x() : MathUtils.clamp(min, max, x()), (filter & Y) == 0 ? y() : MathUtils.clamp(min, max, y())); }
	
	@Override
	public default Vec2d store(ByteBuffer buffer) {
		buffer.putDouble(x()).putDouble(y());
		return this;
	}
	@Override
	public default Vec2d load(ByteBuffer buffer) { return set(buffer.getDouble(), buffer.getDouble()); }
	@Override
	public default Vec2d store(DoubleBuffer buffer) {
		buffer.put(x()).put(y());
		return this;
	}
	@Override
	public default Vec2d load(DoubleBuffer 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 Vec2f asFloat() { return isMutable() ? Vec2f.mutable((float)x(), (float)y()) : Vec2f.of((float)x(), (float)y()); }
	@Override
	public default Vec2d asMutable() { return isMutable() ? this : mutable(this); }
	@Override
	public default Vec2d asImmutable() { return isMutable() ? of(this) : this; }
	@Override
	public default Vec2d copyAsMutable() { return mutable(this); }
	@Override
	public default Vec2d copyAsImmutable() { return of(this); }
}
