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.Vec4b;
import speiger.src.coreengine.math.vector.floats.Vec4f;
import speiger.src.coreengine.math.vector.ints.Vec4i;
import speiger.src.coreengine.math.vector.longs.Vec4l;
import speiger.src.coreengine.math.vector.shorts.Vec4s;

public interface Vec4d extends Vecd {
	public static final Vec4d ZERO = of();
	public static final Vec4d MINUS_ONE = of(-1D);
	public static final Vec4d ONE = of(1D);
	
	public static Vec4d mutable() { return new Vec4dMutable(); }
	public static Vec4d mutable(double value) { return new Vec4dMutable(value); }
	public static Vec4d mutable(double x, double y, double z, double w) { return new Vec4dMutable(x, y, z, w); }
	public static Vec4d mutable(Vec4d vec) { return mutable(vec.x(), vec.y(), vec.z(), vec.w()); }
	public static Vec4d of() { return new Vec4dImmutable(); }
	public static Vec4d of(double value) { return new Vec4dImmutable(value); }
	public static Vec4d of(double x, double y, double z, double w) { return new Vec4dImmutable(x, y, z, w); }
	public static Vec4d of(Vec4d vec) { return of(vec.x(), vec.y(), vec.z(), vec.w()); }
	
	public double x();
	public double y();
	public double z();
	public double w();
	public Vec4d x(double x);
	public Vec4d y(double y);
	public Vec4d z(double z);
	public Vec4d w(double w);
	
	@Override
	public default double[] asArray() { return new double[] {x(), y(), z(), w()}; }
	@Override
	public Vec4d copy();
	@Override
	public default Vec4d abs() { return set(Math.abs(x()), Math.abs(y()), Math.abs(z()), Math.abs(w())); }
	@Override
	public default Vec4d negate() { return set(0D, 0D, 0D, 0D); }
	@Override
	public default Vec4d invert() { return set(-x(), -y(), -z(), -w()); }
	public default Vec4d normalize() {
		double l = length();
		return l == 0D ? this : multiply(1.0D / l);
	}
	public default Vec4d normalize3D() {
		double value = (x() * x()) + (y() * y()) + (z() * z());
		return value == 0D ? this : multiply(1D / value);
	}
	@Override
	public default Vec4d add(double value) { return add(value, value, value, value); }
	public default Vec4d add(Vec4d value) { return add(value.x(), value.y(), value.z(), value.w()); }
	public default Vec4d add(double x, double y, double z, double w) { return set(x() + x, y() + y, z() + z, w() + w); }
	@Override
	public default Vec4d sub(double value) { return sub(value, value, value, value); }
	public default Vec4d sub(Vec4d value) { return sub(value.x(), value.y(), value.z(), value.w()); }
	public default Vec4d sub(double x, double y, double z, double w) { return set(x() - x, y() - y, z() - z, w() - w); }
	@Override
	public default Vec4d multiply(double value) { return multiply(value, value, value, value); }
	public default Vec4d multiply(Vec4d value) { return multiply(value.x(), value.y(), value.z(), value.w()); }
	public default Vec4d multiply(double x, double y, double z, double w) { return set(x() * x, y() * y, z() * z, w() * w); }
	@Override
	public default Vec4d devide(double value) { return devide(value, value, value, value); }
	public default Vec4d devide(Vec4d value) { return devide(value.x(), value.y(), value.z(), value.w()); }
	public default Vec4d devide(double x, double y, double z, double w) { return set(x() / x, y() / y, z() / z, w() / w); }
	@Override
	public default Vec4d set(double value) { return set(value, value, value, value); }
	public default Vec4d set(Vec4d value) { return set(value.x(), value.y(), value.z(), value.w()); }
	public Vec4d set(double x, double y, double z, double w);
	
	public default double distanceTo(Vec4d value) { return distanceTo(value.x(), value.y(), value.z(), value.w()); }
	public default double distanceTo(double x, double y, double z, double w) { return Math.sqrt(distanceTo(x, y, z, w)); }
	public default double distanceToSquared(Vec4d value) { return distanceToSquared(value.x(), value.y(), value.z(), value.w()); }
	public default double distanceToSquared(double x, double y, double z, double w) {
		double xPos = x() - x;
		double yPos = y() - y;
		double zPos = z() - z;
		double wPos = w() - w;
		return (xPos * xPos) + (yPos * yPos) + (zPos * zPos) + (wPos * wPos);
	}
	
	@Override
	public default double lengthSquared() { return (x() * x()) + (y() * y()) + (z() * z()) + (w() * w()); }
	public default double dotProduct(Vec4d value) { return dotProduct(value.x(), value.y(), value.z(), value.w()); }
	public default double dotProduct(double x, double y, double z, double w) { return (x() * x) + (y() * y) + (z() * z) + (w() * w); };
	public default Vec4d lerp(Vec4d value, float progress, Vec4d result) { return lerp(value.x(), value.y(), value.z(), value.w(), progress, result); }
	public default Vec4d lerp(double x, double y, double z, double w, float progress, Vec4d result) { return result.set(MathUtils.lerp(x(), x, progress), MathUtils.lerp(y(), y, progress), MathUtils.lerp(z(), z, progress), MathUtils.lerp(w(), w, progress)); }
	public default double angle(Vec4d value) { return angle(value.x(), value.y(), value.z(), value.w()); }
	public default double angle(double x, double y, double z, double w) { return Math.acos(MathUtils.clamp(-1D, 1D, angleCos(x, y, z, w))); }
	public default double angleCos(Vec4d value) { return angleCos(value.x(), value.y(), value.z(), value.w()); }
	public default double angleCos(double x, double y, double z, double w) { return dotProduct(x, y, z, w) / Math.sqrt(lengthSquared() * (x * x) + (y * y) + (z * z) + (w * w)); }
	public default Vec4d rotate(double angle, Vec4d axis) { return rotate(angle, axis.x(), axis.y(), axis.z(), axis.w()); }
	public default Vec4d rotate(double angle, Vec4d axis, Vec4d result) { return rotate(angle, axis.x(), axis.y(), axis.z(), axis.w(), result); }
	public default Vec4d rotate(double angle, double x, double y, double z, double w) { return rotate(angle, x, y, z, w, this); }
	public default Vec4d rotate(double angle, double x, double y, double z, double w, Vec4d result) {
		double cos = MathUtils.cos(angle);
		double sin = MathUtils.sin(angle);
		double dot = dotProduct(x, y, z, w);
		double xPos = x * dot * (1D - cos) + (x() * cos) + (-z * y() + y * z()) * sin;
		double yPos = y * dot * (1D - cos) + (y() * cos) + (z * x() - x * z()) * sin;
		double zPos = z * dot * (1D - cos) + (z() * cos) + (-y * x() + x * y()) * sin;
		return result.set(xPos, yPos, zPos, w());
	}
	
	public default Vec4d rotateX(double angle) { return rotateX(angle, this); }
	public default Vec4d rotateX(double angle, Vec4d result) {
		double cos = MathUtils.cos(angle);
		double sin = MathUtils.sin(angle);
		double y = y() * cos + z() * sin;
		double z = z() * cos - y() * sin;
		return result.set(x(), y, z, w());
	}
	
	public default Vec4d rotateY(double angle) { return rotateY(angle, this); }
	public default Vec4d rotateY(double angle, Vec4d result) {
		double cos = MathUtils.cos(angle);
		double sin = MathUtils.sin(angle);
		double x = x() * cos + z() * sin;
		double z = z() * cos - x() * sin;
		return result.set(x, y(), z, w());
	}
	
	public default Vec4d rotateZ(double angle) { return rotateZ(angle, this); }
	public default Vec4d rotateZ(double angle, Vec4d result) {
		double cos = MathUtils.cos(angle);
		double sin = MathUtils.sin(angle);
		double x = cos * x() - sin * y();
		double y = sin * x() + cos * y();
		return result.set(x, y, z(), w());
	}
	
	public default Vec4d smoothStep(Vec4d value, float progress, Vec4d result) { return smoothStep(value.x(), value.y(), value.z(), value.w(), progress, result); }
	public default Vec4d smoothStep(double x, double y, double z, double w, float progress, Vec4d result) {
		double t2 = progress * progress;
		double t3 = t2 * progress;
		double xPos = ((x() + x() - x - x) * t3 + (3.0D * x - 3.0D * x()) * t2 + x() * progress + x());
		double yPos = ((y() + y() - y - y) * t3 + (3.0D * y - 3.0D * y()) * t2 + y() * progress + y());
		double zPos = ((z() + z() - z - z) * t3 + (3.0D * z - 3.0D * z()) * t2 + z() * progress + z());
		double wPos = ((w() + w() - w - w) * t3 + (3.0D * w - 3.0D * w()) * t2 + w() * progress + w());
		return result.set(xPos, yPos, zPos, wPos);
	}
	
	public default Vec4d min(Vec4d other) { return min(other, this); }
	public default Vec4d min(Vec4d other, Vec4d result) { return min(other.x(), other.y(), other.z(), other.w(), result); }
	public default Vec4d min(double x, double y, double z, double w) { return min(x, y, z, w, this); }
	public default Vec4d min(double x, double y, double z, double w, Vec4d result) { return result.set(Math.min(x(), x), Math.min(y(), y), Math.min(z(), z), Math.min(w(), w)); }
	public default Vec4d max(Vec4d other) { return max(other, this); }
	public default Vec4d max(Vec4d other, Vec4d result) { return max(other.x(), other.y(), other.z(), other.w(), result); }
	public default Vec4d max(double x, double y, double z, double w) { return max(x, y, z, w, this); }
	public default Vec4d max(double x, double y, double z, double w, Vec4d result) { return result.set(Math.max(x(), x), Math.max(y(), y), Math.max(z(), z), Math.max(z(), z)); }
	public default Vec4d difference(Vec4d other) { return difference(other, this); }
	public default Vec4d difference(Vec4d other, Vec4d result) { return difference(other.x(), other.y(), other.z(), other.w(), result); }
	public default Vec4d difference(double x, double y, double z, double w) { return difference(x, y, z, w, this); }
	public default Vec4d difference(double x, double y, double z, double w, Vec4d result) { return result.set(x() - x, y() - y, z() - z, w() - w); }
	@Override
	public default Vec4d clamp(double min, double max) { return clamp(min, max, ALL); }
	public default Vec4d clamp(double min, double max, Vec4d result) { return clamp(min, max, result, ALL); }
	@Override
	public default Vec4d clamp(double min, double max, int filter) { return clamp(min, max, this, filter); }
	public default Vec4d clamp(double min, double max, Vec4d result, int filter) { return result.set((filter & X) == 0 ? x() : MathUtils.clamp(min, max, x()), (filter & Y) == 0 ? y() : MathUtils.clamp(min, max, y()), (filter & Z) == 0 ? z() : MathUtils.clamp(min, max, z()), (filter & W) == 0 ? w() : MathUtils.clamp(min, max, w())); }
	
	@Override
	public default Vec4d store(ByteBuffer buffer) {
		buffer.putDouble(x()).putDouble(y()).putDouble(z()).putDouble(w());
		return this;
	}
	
	@Override
	public default Vec4d load(ByteBuffer buffer) { return set(buffer.getDouble(), buffer.getDouble(), buffer.getDouble(), buffer.getDouble()); }
	
	@Override
	public default Vec4d store(DoubleBuffer buffer) {
		buffer.put(x()).put(y()).put(z()).put(w());
		return this;
	}
	
	@Override
	public default Vec4d load(DoubleBuffer buffer) { return set(buffer.get(), buffer.get(), buffer.get(), buffer.get()); }
	@Override
	public default Vec4b asByte() { return isMutable() ? Vec4b.mutable((byte)MathUtils.floor(x()), (byte)MathUtils.floor(y()), (byte)MathUtils.floor(z()), (byte)MathUtils.floor(w())) : Vec4b.of((byte)MathUtils.floor(x()), (byte)MathUtils.floor(y()), (byte)MathUtils.floor(z()), (byte)MathUtils.floor(w())); }
	@Override
	public default Vec4s asShort() { return isMutable() ? Vec4s.mutable((short)MathUtils.floor(x()), (short)MathUtils.floor(y()), (short)MathUtils.floor(z()), (short)MathUtils.floor(w())) : Vec4s.of((short)MathUtils.floor(x()), (short)MathUtils.floor(y()), (short)MathUtils.floor(z()), (short)MathUtils.floor(w())); }
	@Override
	public default Vec4i asInt() { return isMutable() ? Vec4i.mutable(MathUtils.floor(x()), MathUtils.floor(y()), MathUtils.floor(z()), MathUtils.floor(w())) : Vec4i.of(MathUtils.floor(x()), MathUtils.floor(y()), MathUtils.floor(z()), MathUtils.floor(w())); }
	@Override
	public default Vec4l asLong() { return isMutable() ? Vec4l.mutable(MathUtils.floor(x()), MathUtils.floor(y()), MathUtils.floor(z()), MathUtils.floor(w())) : Vec4l.of(MathUtils.floor(x()), MathUtils.floor(y()), MathUtils.floor(z()), MathUtils.floor(w())); }
	@Override
	public default Vec4f asFloat() { return isMutable() ? Vec4f.mutable((float)x(), (float)y(), (float)z(), (float)w()) : Vec4f.of((float)x(), (float)y(), (float)z(), (float)w()); }
	@Override
	public default Vec4d asMutable() { return isMutable() ? this : mutable(this); }
	@Override
	public default Vec4d asImmutable() { return isMutable() ? of(this) : this; }
	@Override
	public default Vec4d copyAsMutable() { return mutable(this); }
	@Override
	public default Vec4d copyAsImmutable() { return of(this); }
}
