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.Vec3b;
import speiger.src.coreengine.math.vector.floats.Vec3f;
import speiger.src.coreengine.math.vector.ints.Vec3i;
import speiger.src.coreengine.math.vector.longs.Vec3l;
import speiger.src.coreengine.math.vector.shorts.Vec3s;

public interface Vec3d extends Vecd {
	public static final Vec3d ZERO = of();
	public static final Vec3d MINUS_ONE = of(-1D);
	public static final Vec3d ONE = of(1D);
	
	public static Vec3d mutable() { return new Vec3dMutable(); }
	
	public static Vec3d mutable(double value) { return new Vec3dMutable(value); }
	
	public static Vec3d mutable(double x, double y, double z) { return new Vec3dMutable(x, y, z); }
	
	public static Vec3d mutable(Vec3d vec) { return mutable(vec.x(), vec.y(), vec.z()); }
	
	public static Vec3d of() { return new Vec3dImmutable(); }
	
	public static Vec3d of(double value) { return new Vec3dImmutable(value); }
	
	public static Vec3d of(double x, double y, double z) { return new Vec3dImmutable(x, y, z); }
	
	public static Vec3d of(Vec3d vec) { return of(vec.x(), vec.y(), vec.z()); }
	
	public double x();
	public double y();
	public double z();
	public Vec3d x(double x);
	public Vec3d y(double y);
	public Vec3d z(double z);
	
	@Override
	public default double[] asArray() { return new double[] {x(), y(), z()}; }
	
	@Override
	public Vec3d copy();
	@Override
	public default Vec3d abs() { return set(Math.abs(x()), Math.abs(y()), Math.abs(z())); }
	@Override
	public default Vec3d negate() { return set(0D, 0D, 0D); }
	@Override
	public default Vec3d invert() { return set(-x(), -y(), -z()); }
	public default Vec3d normalize() {
		double l = length();
		return l == 0D ? this : multiply(1.0D / l);
	}
	
	@Override
	public default Vec3d add(double value) { return add(value, value, value); }
	public default Vec3d add(Vec3d value) { return add(value.x(), value.y(), value.z()); }
	public default Vec3d add(double x, double y, double z) { return set(x() + x, y() + y, z() + z); }
	@Override
	public default Vec3d sub(double value) { return sub(value, value, value); }
	public default Vec3d sub(Vec3d value) { return sub(value.x(), value.y(), value.z()); }
	public default Vec3d sub(double x, double y, double z) { return set(x() - x, y() - y, z() - z); }
	@Override
	public default Vec3d multiply(double value) { return multiply(value, value, value); }
	public default Vec3d multiply(Vec3d value) { return multiply(value.x(), value.y(), value.z()); }
	public default Vec3d multiply(double x, double y, double z) { return set(x() * x, y() * y, z() * z); }
	@Override
	public default Vec3d devide(double value) { return devide(value, value, value); }
	public default Vec3d devide(Vec3d value) { return devide(value.x(), value.y(), value.z()); }
	public default Vec3d devide(double x, double y, double z) { return set(x() / x, y() / y, z() / z); }
	@Override
	public default Vec3d set(double value) { return set(value, value, value); }
	public default Vec3d set(Vec3d value) { return set(value.x(), value.y(), value.z()); }
	public Vec3d set(double x, double y, double z);
	public default double distanceTo(Vec3d value) { return distanceTo(value.x(), value.y(), value.z()); }
	public default double distanceTo(double x, double y, double z) { return Math.sqrt(distanceToSquared(x, y, z)); }
	public default double distanceToSquared(Vec3d value) { return distanceToSquared(value.x(), value.y(), value.z()); }
	public default double distanceToSquared(double x, double y, double z) {
		double xPos = x() - x;
		double yPos = y() - y;
		double zPos = z() - z;
		return (xPos * xPos) + (yPos * yPos) + (zPos * zPos);
	}
	
	@Override
	public default double lengthSquared() { return (x() * x()) + (y() * y()) + (z() * z()); }
	public default double dotProduct(Vec3d value) { return dotProduct(value.x(), value.y(), value.z()); }
	public default double dotProduct(double x, double y, double z) { return (x() * x) + (y() * y) + (z() * z); }
	public default Vec3d lerp(Vec3d value, float progress, Vec3d result) { return lerp(value.x(), value.y(), value.z(), progress, result); }
	public default Vec3d lerp(double x, double y, double z, float progress, Vec3d result) { return result.set(MathUtils.lerp(x(), x, progress), MathUtils.lerp(y(), y, progress), MathUtils.lerp(z(), z, progress)); }
	public default double angle(Vec3d value) { return angle(value.x(), value.y(), value.z()); }
	public default double angle(double x, double y, double z) { return Math.acos(MathUtils.clamp(-1, 1, angleCos(x, y, z))); }
	public default double angleCos(Vec3d value) { return angleCos(value.x(), value.y(), value.z()); }
	public default double angleCos(double x, double y, double z) {
		double myLength = (x() * x()) + (y() * y()) + (z() * z());
		double otherLength = (x * x) + (y * y) + (z * z);
		double dot = (x() * x) + (y() * y) + (z() * z);
		return dot / (Math.sqrt(myLength * otherLength));
	}
	
	public default Vec3d crossProduct(Vec3d value) { return crossProduct(value.x(), value.y(), value.z(), this); }
	public default Vec3d crossProduct(Vec3d value, Vec3d result) { return crossProduct(value.x(), value.y(), value.z(), result); }
	public default Vec3d crossProduct(double x, double y, double z) { return crossProduct(x, y, z, this); }
	public default Vec3d crossProduct(double x, double y, double z, Vec3d result) { return result.set((y() * z) - (z() * y), (z() * x) - (x() * z), (x() * y) - (y() * x)); }
	public default Vec3d reflect(Vec3d value) { return reflect(value.x(), value.y(), value.z(), this); }
	public default Vec3d reflect(Vec3d value, Vec3d result) { return reflect(value.x(), value.y(), value.z(), result); }
	public default Vec3d reflect(double x, double y, double z) { return reflect(x, y, z, this); }
	public default Vec3d reflect(double x, double y, double z, Vec3d result) { 
		double dot = dotProduct(x, y, z) * 2D;
		return result.set(x() - dot * x, y() - dot * y, z() - dot * z);
	}
	
	public default Vec3d rotate(double angle, Vec3d axis) { return rotate(angle, axis.x(), axis.y(), axis.z()); }
	public default Vec3d rotate(double angle, Vec3d axis, Vec3d result) { return rotate(angle, axis.x(), axis.y(), axis.z(), result); }
	public default Vec3d rotate(double angle, double x, double y, double z) { return rotate(angle, x, y, z, this); }
	public default Vec3d rotate(double angle, double x, double y, double z, Vec3d result) {
		double cos = MathUtils.cos(angle);
		double sin = MathUtils.sin(angle);
		double dot = dotProduct(x, y, z);
		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);
	}
	
	public default Vec3d rotateX(double angle) { return rotateX(angle, this); }
	public default Vec3d rotateX(double angle, Vec3d 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);
	}
	
	public default Vec3d rotateY(double angle) { return rotateY(angle, this); }
	public default Vec3d rotateY(double angle, Vec3d 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);
	}
	
	public default Vec3d rotateZ(double angle) { return rotateZ(angle, this); }
	public default Vec3d rotateZ(double angle, Vec3d 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());
	}
	
	public default Vec3d smoothStep(Vec3d value, float progress, Vec3d result) { return smoothStep(value.x(), value.y(), value.z(), progress, result);	}
	public default Vec3d smoothStep(double x, double y, double z, float progress, Vec3d 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());
		return result.set(xPos, yPos, zPos);
	}
	
	public default Vec3d min(Vec3d other) { return min(other, this); }
	public default Vec3d min(Vec3d other, Vec3d result) { return min(other.x(), other.y(), other.z(), result); }
	public default Vec3d min(double x, double y, double z) { return min(x, y, z, this); }
	public default Vec3d min(double x, double y, double z, Vec3d result) { return result.set(Math.min(x(), x), Math.min(y(), y), Math.min(z(), z)); }
	public default Vec3d max(Vec3d other) { return max(other, this); }
	public default Vec3d max(Vec3d other, Vec3d result) { return max(other.x(), other.y(), other.z(), result); }
	public default Vec3d max(double x, double y, double z) { return max(x, y, z, this); }
	public default Vec3d max(double x, double y, double z, Vec3d result) { return result.set(Math.max(x(), x), Math.max(y(), y), Math.max(z(), z)); }
	public default Vec3d difference(Vec3d other) { return difference(other, this); }
	
	public default Vec3d difference(Vec3d other, Vec3d result) { return difference(other.x(), other.y(), other.z(), result); }
	public default Vec3d difference(double x, double y, double z) { return difference(x, y, z, this); }
	public default Vec3d difference(double x, double y, double z, Vec3d result) { return result.set(x() - x, y() - y, z() - z); }
	@Override
	public default Vec3d clamp(double min, double max) { return clamp(min, max, ALL); }
	public default Vec3d clamp(double min, double max, Vec3d result) { return clamp(min, max, result, ALL); }
	@Override
	public default Vec3d clamp(double min, double max, int filter) { return clamp(min, max, this, filter); }
	public default Vec3d clamp(double min, double max, Vec3d 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())); }
	
	@Override
	public default Vec3d store(ByteBuffer buffer) {
		buffer.putDouble(x()).putDouble(y()).putDouble(z());
		return this;
	}
	
	@Override
	public default Vec3d load(ByteBuffer buffer) { return set(buffer.getDouble(), buffer.getDouble(), buffer.getDouble()); }
	
	@Override
	public default Vec3d store(DoubleBuffer buffer) {
		buffer.put(x()).put(y()).put(z());
		return this;
	}
	
	@Override
	public default Vec3d load(DoubleBuffer buffer) { return set(buffer.get(), buffer.get(), buffer.get()); }
	@Override
	public default Vec3b asByte() { return isMutable() ? Vec3b.mutable((byte)MathUtils.floor(x()), (byte)MathUtils.floor(y()), (byte)MathUtils.floor(z())) : Vec3b.of((byte)MathUtils.floor(x()), (byte)MathUtils.floor(y()), (byte)MathUtils.floor(z())); }
	@Override
	public default Vec3s asShort() { return isMutable() ? Vec3s.mutable((short)MathUtils.floor(x()), (short)MathUtils.floor(y()), (short)MathUtils.floor(z())) : Vec3s.of((short)MathUtils.floor(x()), (short)MathUtils.floor(y()), (short)MathUtils.floor(z())); }
	@Override
	public default Vec3i asInt() { return isMutable() ? Vec3i.mutable(MathUtils.floor(x()), MathUtils.floor(y()), MathUtils.floor(z())) : Vec3i.of(MathUtils.floor(x()), MathUtils.floor(y()), MathUtils.floor(z())); }
	@Override
	public default Vec3l asLong() { return isMutable() ? Vec3l.mutable(MathUtils.floor(x()), MathUtils.floor(y()), MathUtils.floor(z())) : Vec3l.of(MathUtils.floor(x()), MathUtils.floor(y()), MathUtils.floor(z())); }
	@Override
	public default Vec3f asFloat() { return isMutable() ? Vec3f.mutable((float)x(), (float)y(), (float)z()) : Vec3f.of((float)x(), (float)y(), (float)z()); }
	@Override
	public default Vec3d asMutable() { return isMutable() ? this : mutable(this); }
	@Override
	public default Vec3d asImmutable() { return isMutable() ? of(this) : this; }
	@Override
	public default Vec3d copyAsMutable() { return mutable(this); }
	@Override
	public default Vec3d copyAsImmutable() { return of(this); }
}
