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.Vec3b;
import speiger.src.coreengine.math.vector.doubles.Vec3d;
import speiger.src.coreengine.math.vector.ints.Vec3i;
import speiger.src.coreengine.math.vector.longs.Vec3l;
import speiger.src.coreengine.math.vector.matrix.Matrix4f;
import speiger.src.coreengine.math.vector.shorts.Vec3s;

public interface Vec3f extends Vecf {
	public static final Vec3f ZERO = of();
	public static final Vec3f MINUS_ONE = of(-1F);
	public static final Vec3f ONE = of(1F);
	
	public static Vec3f mutable() { return new Vec3fMutable(); }
	public static Vec3f mutable(float value) { return new Vec3fMutable(value); }
	public static Vec3f mutable(float x, float y, float z) { return new Vec3fMutable(x, y, z); }
	public static Vec3f mutable(Vec3f vec) { return mutable(vec.x(), vec.y(), vec.z()); }
	public static Vec3f of() { return new Vec3fImmutable(); }
	public static Vec3f of(float value) { return new Vec3fImmutable(value); }
	public static Vec3f of(float x, float y, float z) { return new Vec3fImmutable(x, y, z); }
	public static Vec3f of(Vec3f vec) { return of(vec.x(), vec.y(), vec.z()); }
	
	public float x();
	public float y();
	public float z();
	public Vec3f x(float x);
	public Vec3f y(float y);
	public Vec3f z(float z);
	
	@Override
	public default float[] asArray() { return new float[] {x(), y(), z()}; }
	@Override
	public Vec3f copy();
	@Override
	public default Vec3f abs() { return set(Math.abs(x()), Math.abs(y()), Math.abs(z())); }
	@Override
	public default Vec3f negate() { return set(0F, 0F, 0F); }
	@Override
	public default Vec3f invert() { return set(-x(), -y(), -z()); }
	public default Vec3f normalize() {
		float l = length();
		return l == 0F ? this : multiply(1.0F / l);
	}
	
	@Override
	public default Vec3f add(float value) { return add(value, value, value); }
	public default Vec3f add(Vec3f value) { return add(value.x(), value.y(), value.z()); }
	public default Vec3f add(float x, float y, float z) { return set(x() + x, y() + y, z() + z); }
	@Override
	public default Vec3f sub(float value) { return sub(value, value, value); }
	public default Vec3f sub(Vec3f value) { return sub(value.x(), value.y(), value.z()); }
	public default Vec3f sub(float x, float y, float z) { return set(x() - x, y() - y, z() - z); }
	@Override
	public default Vec3f multiply(float value) { return multiply(value, value, value); }
	public default Vec3f multiply(Vec3f value) { return multiply(value.x(), value.y(), value.z()); }
	public default Vec3f multiply(float x, float y, float z) { return set(x() * x, y() * y, z() * z); }
	@Override
	public default Vec3f devide(float value) { return devide(value, value, value); }
	public default Vec3f devide(Vec3f value) { return devide(value.x(), value.y(), value.z()); }
	public default Vec3f devide(float x, float y, float z) { return set(x() / x, y() / y, z() / z); }
	@Override
	public default Vec3f set(float value) { return set(value, value, value); }
	public default Vec3f set(Vec3f value) { return set(value.x(), value.y(), value.z()); }
	public Vec3f set(float x, float y, float z);
	public default double distanceTo(Vec3f value) { return distanceTo(value.x(), value.y(), value.z()); }
	public default double distanceTo(float x, float y, float z) { return Math.sqrt(distanceToSquared(x, y, z)); }
	public default double distanceToSquared(Vec3f value) { return distanceToSquared(value.x(), value.y(), value.z()); }
	public default double distanceToSquared(float x, float y, float z) {
		double xPos = x() - x;
		double yPos = y() - y;
		double zPos = z() - z;
		return (xPos * xPos) + (yPos * yPos) + (zPos * zPos);
	}
	
	@Override
	public default float lengthSquared() { return (x() * x()) + (y() * y()) + (z() * z()); }
	public default double dotProduct(Vec3f value) { return dotProduct(value.x(), value.y(), value.z()); }
	public default double dotProduct(float x, float y, float z) { return (x() * x) + (y() * y) + (z() * z); }
	public default Vec3f lerp(Vec3f value, float progress, Vec3f result) { return lerp(value.x(), value.y(), value.z(), progress, result); }
	public default Vec3f lerp(float x, float y, float z, float progress, Vec3f result) { return result.set(MathUtils.lerp(x(), x, progress), MathUtils.lerp(y(), y, progress), MathUtils.lerp(z(), z, progress)); }
	public default float angle(Vec3f value) { return angle(value.x(), value.y(), value.z()); }
	public default float angle(float x, float y, float z) { return (float)Math.acos(MathUtils.clamp(-1, 1, angleCos(x, y, z))); }
	public default float angleCos(Vec3f value) { return angleCos(value.x(), value.y(), value.z()); }
	public default float angleCos(float x, float y, float z) {
		double myLength = (x() * x()) + (y() * y()) + (z() * z());
		double otherLength = (x * x) + (y * y) + (z * z);
		float dot = (x() * x) + (y() * y) + (z() * z);
		return dot / (float)(Math.sqrt(myLength * otherLength));
	}
	
	public default Vec3f crossProduct(Vec3f value) { return crossProduct(value.x(), value.y(), value.z(), this); }
	public default Vec3f crossProduct(Vec3f value, Vec3f result) { return crossProduct(value.x(), value.y(), value.z(), result); }
	public default Vec3f crossProduct(float x, float y, float z) { return crossProduct(x, y, z, this); }
	public default Vec3f crossProduct(float x, float y, float z, Vec3f result) { return result.set((y() * z) - (z() * y), (z() * x) - (x() * z), (x() * y) - (y() * x)); }
	public default Vec3f reflect(Vec3f value) { return reflect(value.x(), value.y(), value.z(), this); }
	public default Vec3f reflect(Vec3f value, Vec3f result) { return reflect(value.x(), value.y(), value.z(), result); }
	public default Vec3f reflect(float x, float y, float z) { return reflect(x, y, z, this); }
	public default Vec3f reflect(float x, float y, float z, Vec3f result) {
		double dot = dotProduct(x, y, z) * 2D;
		return result.set(x() - (float)(dot * x), y() - (float)(dot * y), z() - (float)(dot * z));
	}
	
	public default Vec3f rotate(float angle, Vec3f axis) { return rotate(angle, axis.x(), axis.y(), axis.z()); }
	public default Vec3f rotate(float angle, Vec3f axis, Vec3f result) { return rotate(angle, axis.x(), axis.y(), axis.z(), result); }
	public default Vec3f rotate(float angle, float x, float y, float z) { return rotate(angle, x, y, z, this); }
	public default Vec3f rotate(float angle, float x, float y, float z, Vec3f 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((float)xPos, (float)yPos, (float)zPos);
	}
	
	public default Vec3f rotateX(float angle) { return rotateX(angle, this); }
	public default Vec3f rotateX(float angle, Vec3f 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(), (float)y, (float)z);
	}
	
	public default Vec3f rotateY(float angle) { return rotateY(angle, this); }
	public default Vec3f rotateY(float angle, Vec3f 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((float)x, y(), (float)z);
	}
	
	public default Vec3f rotateZ(float angle) { return rotateZ(angle, this); }
	public default Vec3f rotateZ(float angle, Vec3f 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((float)x, (float)y, z());
	}
	
	public default Vec3f transform(Matrix4f matrix) { return transform(matrix, true, this); }
	public default Vec3f transofrm(Matrix4f matrix, boolean pos) { return transform(matrix, pos, this); }
	public default Vec3f transform(Matrix4f matrix, boolean pos, Vec3f result) {
		if((matrix.properties() & Matrix4f.PROPERTY_IDENTITY) != 0) {
			return result.set(this);
		}
		if((matrix.properties() & Matrix4f.PROPERTY_ROTATION) != 0) {
			float w = pos ? 1F : 0F;
			return result.set(
				matrix.fma(Matrix4f.M00, Matrix4f.M10, Matrix4f.M20, Matrix4f.M30, x(), y(), z(), w), 
				matrix.fma(Matrix4f.M01, Matrix4f.M11, Matrix4f.M21, Matrix4f.M31, x(), y(), z(), w), 
				matrix.fma(Matrix4f.M02, Matrix4f.M12, Matrix4f.M22, Matrix4f.M32, x(), y(), z(), w));
		}
		if((matrix.properties() & Matrix4f.PROPERTY_SCALE) != 0) {
			if(pos) return set(
					Math.fma(matrix.get(Matrix4f.M00), x(), matrix.get(Matrix4f.M30)), 
					Math.fma(matrix.get(Matrix4f.M01), y(), matrix.get(Matrix4f.M31)), 
					Math.fma(matrix.get(Matrix4f.M02), z(), matrix.get(Matrix4f.M32)));
			return set(matrix.get(Matrix4f.M00) * x(), matrix.get(Matrix4f.M01) * y(), matrix.get(Matrix4f.M02) * z());
		}
		if(pos) return set(x() + matrix.get(Matrix4f.M30), y() + matrix.get(Matrix4f.M31), z() + matrix.get(Matrix4f.M32));
		return result.set(this);
	}
	
	public default Vec3f smoothStep(Vec3f value, float progress, Vec3f result) { return smoothStep(value.x(), value.y(), value.z(), progress, result); }
	public default Vec3f smoothStep(float x, float y, float z, float progress, Vec3f result) {
		float t2 = progress * progress;
		float t3 = t2 * progress;
		float xPos = ((x() + x() - x - x) * t3 + (3.0F * x - 3.0F * x()) * t2 + x() * progress + x());
		float yPos = ((y() + y() - y - y) * t3 + (3.0F * y - 3.0F * y()) * t2 + y() * progress + y());
		float zPos = ((z() + z() - z - z) * t3 + (3.0F * z - 3.0F * z()) * t2 + z() * progress + z());
		return result.set(xPos, yPos, zPos);
	}
	
	public default Vec3f min(Vec3f other) { return min(other, this); }
	public default Vec3f min(Vec3f other, Vec3f result) { return min(other.x(), other.y(), other.z(), result); }
	public default Vec3f min(float x, float y, float z) { return min(x, y, z, this); }
	public default Vec3f min(float x, float y, float z, Vec3f result) { return result.set(Math.min(x(), x), Math.min(y(), y), Math.min(z(), z)); }
	public default Vec3f max(Vec3f other) { return max(other, this); }
	public default Vec3f max(Vec3f other, Vec3f result) { return max(other.x(), other.y(), other.z(), result); }
	public default Vec3f max(float x, float y, float z) { return max(x, y, z, this); }
	public default Vec3f max(float x, float y, float z, Vec3f result) { return result.set(Math.max(x(), x), Math.max(y(), y), Math.max(z(), z)); }
	public default Vec3f difference(Vec3f other) { return difference(other, this); }
	public default Vec3f difference(Vec3f other, Vec3f result) { return difference(other.x(), other.y(), other.z(), result); }
	public default Vec3f difference(float x, float y, float z) { return difference(x, y, z, this); }
	public default Vec3f difference(float x, float y, float z, Vec3f result) { return result.set(x() - x, y() - y, z() - z); }
	@Override
	public default Vec3f clamp(float min, float max) { return clamp(min, max, ALL); }
	public default Vec3f clamp(float min, float max, Vec3f result) { return clamp(min, max, result, ALL); }
	@Override
	public default Vec3f clamp(float min, float max, int filter) { return clamp(min, max, this, filter); }
	public default Vec3f clamp(float min, float max, Vec3f 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 Vec3f store(ByteBuffer buffer) {
		buffer.putFloat(x()).putFloat(y()).putFloat(z());
		return this;
	}
	
	@Override
	public default Vec3f load(ByteBuffer buffer) { return set(buffer.getFloat(), buffer.getFloat(), buffer.getFloat()); }
	
	@Override
	public default Vec3f store(FloatBuffer buffer) {
		buffer.put(x()).put(y()).put(z());
		return this;
	}
	
	@Override
	public default Vec3f load(FloatBuffer 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 Vec3d asDouble() { return isMutable() ? Vec3d.mutable(x(), y(), z()) : Vec3d.of(x(), y(), z()); }
	@Override
	public default Vec3f asMutable() { return isMutable() ? this : mutable(this); }
	@Override
	public default Vec3f asImmutable() { return isMutable() ? of(this) : this; }
	@Override
	public default Vec3f copyAsMutable() { return mutable(this); }
	@Override
	public default Vec3f copyAsImmutable() { return of(this); }
}
