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.Vec4b;
import speiger.src.coreengine.math.vector.doubles.Vec4d;
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 Vec4f extends Vecf
{
	public static final Vec4f ZERO = newVec();
	public static final Vec4f MINUS_ONE = newVec(-1F);
	public static final Vec4f ONE = newVec(1F);
	
	public static Vec4f newMutable(){return new Vec4fMutable();}
	public static Vec4f newMutable(float value){return new Vec4fMutable(value);}
	public static Vec4f newMutable(float x, float y, float z, float w){return new Vec4fMutable(x, y, z, w);}
	public static Vec4f newMutable(Vec4f vec){return newMutable(vec.getX(), vec.getY(), vec.getZ(), vec.getW());}
	
	public static Vec4f newVec(){return new Vec4fImmutable();}
	public static Vec4f newVec(float value){return new Vec4fImmutable(value);}
	public static Vec4f newVec(float x, float y, float z, float w){return new Vec4fImmutable(x, y, z, w);}
	public static Vec4f newVec(Vec4f vec){return newVec(vec.getX(), vec.getY(), vec.getZ(), vec.getW());}
	
	public float getX();
	public float getY();
	public float getZ();
	public float getW();
	public Vec4f setX(float x);
	public Vec4f setY(float y);
	public Vec4f setZ(float z);
	public Vec4f setW(float w);
	@Override
	public default float[] asArray(){return new float[]{getX(), getY(), getZ(), getW()};}
	@Override
	public Vec4f copy();
	@Override
	public default Vec4f abs(){return set(Math.abs(getX()), Math.abs(getY()), Math.abs(getZ()), Math.abs(getW()));}
	@Override
	public default Vec4f negate(){return set(0F, 0F, 0F, 0F);}
	@Override
	public default Vec4f invert(){return set(-getX(), -getY(), -getZ(), -getW());}
	public default Vec4f normalize()
	{
		float l = length();
		return l == 0F ? this : multiply(1.0F / l);
	}
	
	public default Vec4f normalize3D()
	{
		float value = (getX() * getX()) + (getY() * getY()) + (getZ() * getZ());
		return value == 0F ? this : multiply(1F / value);
	}
	
	@Override
	public default Vec4f add(float value){return add(value, value, value, value);}	
	public default Vec4f add(Vec4f value){return add(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default Vec4f add(float x, float y, float z, float w){return set(getX() + x, getY() + y, getZ() + z, getW() + w);}

	@Override
	public default Vec4f sub(float value){return sub(value, value, value, value);}
	public default Vec4f sub(Vec4f value){return sub(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default Vec4f sub(float x, float y, float z, float w){return set(getX() - x, getY() - y, getZ() - z, getW() - w);}
	
	@Override
	public default Vec4f multiply(float value){return multiply(value, value, value, value);}
	public default Vec4f multiply(Vec4f value){return multiply(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default Vec4f multiply(float x, float y, float z, float w){return set(getX() * x, getY() * y, getZ() * z, getW() * w);}
	
	@Override
	public default Vec4f devide(float value){return devide(value, value, value, value);}
	public default Vec4f devide(Vec4f value){return devide(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default Vec4f devide(float x, float y, float z, float w){return set(getX() / x, getY() / y, getZ() / z, getW() / w);}
	
	@Override
	public default Vec4f set(float value){return set(value, value, value, value);}
	public default Vec4f set(Vec4f value){return set(value.getX(), value.getY(), value.getZ(), value.getW());}
	public Vec4f set(float x, float y, float z, float w);
	
	public default double distanceTo(Vec4f value){return distanceTo(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default double distanceTo(float x, float y, float z, float w){return Math.sqrt(distanceTo(x, y, z, w));}
	
	public default double distanceToSquared(Vec4f value){return distanceToSquared(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default double distanceToSquared(float x, float y, float z, float w)
	{
		double xPos = getX() - x;
		double yPos = getY() - y;
		double zPos = getZ() - z;
		double wPos = getW() - w;
		return (xPos * xPos) + (yPos * yPos) + (zPos * zPos) + (wPos * wPos);
	}
	@Override
	public default float lengthSquared() {return (getX() * getX()) + (getY() * getY()) + (getZ() * getZ()) + (getW() * getW());}

	public default double dotProduct(Vec4f value){return dotProduct(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default double dotProduct(float x, float y, float z, float w){return (getX() * x) + (getY() * y) + (getZ() * z) + (getW() * w);};
	
	public default Vec4f lerp(Vec4f value, float progress, Vec4f result){return lerp(value.getX(), value.getY(), value.getZ(), value.getW(), progress, result);}
	public default Vec4f lerp(float x, float y, float z, float w, float progress, Vec4f result){return result.set(MathUtils.lerp(getX(), x, progress), MathUtils.lerp(getY(), y, progress), MathUtils.lerp(getZ(), z, progress), MathUtils.lerp(getW(), w, progress));}
	
	public default float angle(Vec4f value){return angle(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default float angle(float x, float y, float z, float w){return (float)Math.acos(MathUtils.clamp(-1F, 1F, angleCos(x, y, z, w)));}
	
	public default float angleCos(Vec4f value){return angleCos(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default float angleCos(float x, float y, float z, float w){return (float)(dotProduct(x, y, z, w) / Math.sqrt(lengthSquared() * (x * x) + (y * y) + (z * z) + (w * w)));}
	
	public default Vec4f rotate(float angle, Vec4f axis){return rotate(angle, axis.getX(), axis.getY(), axis.getZ(), axis.getW());}
	public default Vec4f rotate(float angle, Vec4f axis, Vec4f result){return rotate(angle, axis.getX(), axis.getY(), axis.getZ(), axis.getW(), result);}
	public default Vec4f rotate(float angle, float x, float y, float z, float w){return rotate(angle, x, y, z, w, this);}
	public default Vec4f rotate(float angle, float x, float y, float z, float w, Vec4f result)
	{
		double cos = MathUtils.cos(angle);
		double sin = MathUtils.sin(angle);
		double dot = dotProduct(x, y, z, w);
		double xPos = x * dot * (1D - cos) + (getX() * cos) + (-z * getY() + y * getZ()) * sin;
		double yPos = y * dot * (1D - cos) + (getY() * cos) + (z * getX() - x * getZ()) * sin;
		double zPos = z * dot * (1D - cos) + (getZ() * cos) + (-y * getX() + x * getY()) * sin;		
		return result.set((float)xPos, (float)yPos, (float)zPos, getW());
	}
	public default Vec4f rotateX(float angle){return rotateX(angle, this);}
	public default Vec4f rotateX(float angle, Vec4f result)
	{
		double cos = MathUtils.cos(angle);
		double sin = MathUtils.sin(angle);
		double y = getY() * cos + getZ() * sin;
		double z = getZ() * cos - getY() * sin;
		return result.set(getX(), (float)y, (float)z, getW());
	}
	public default Vec4f rotateY(float angle){return rotateY(angle, this);}
	public default Vec4f rotateY(float angle, Vec4f result)
	{
		double cos = MathUtils.cos(angle);
		double sin = MathUtils.sin(angle);
		double x = getX() * cos + getZ() * sin;
		double z = getZ() * cos - getX() * sin;
		return result.set((float)x, getY(), (float)z, getW());
	}
	public default Vec4f rotateZ(float angle){return rotateZ(angle, this);}
	public default Vec4f rotateZ(float angle, Vec4f result)
	{
		double cos = MathUtils.cos(angle);
		double sin = MathUtils.sin(angle);
		double x = cos * getX() - sin * getY();
		double y = sin * getX() + cos * getY();
		return result.set((float)x, (float)y, getZ(), getW());
	}
	
	public default Vec4f smoothStep(Vec4f value, float progress, Vec4f result){return smoothStep(value.getX(), value.getY(), value.getZ(), value.getW(), progress, result);}
	public default Vec4f smoothStep(float x, float y, float z, float w, float progress, Vec4f result)
	{
	    float t2 = progress * progress;
	    float t3 = t2 * progress;
	    float xPos = ((getX() + getX() - x - x) * t3 + (3.0F * x - 3.0F * getX()) * t2 + getX() * progress + getX());
	    float yPos = ((getY() + getY() - y - y) * t3 + (3.0F * y - 3.0F * getY()) * t2 + getY() * progress + getY());
	    float zPos = ((getZ() + getZ() - z - z) * t3 + (3.0F * z - 3.0F * getZ()) * t2 + getZ() * progress + getZ());
	    float wPos = ((getW() + getW() - w - w) * t3 + (3.0F * w - 3.0F * getW()) * t2 + getW() * progress + getW());
		return result.set(xPos, yPos, zPos, wPos);
	}
	
	public default Vec4f min(Vec4f other) {return min(other, this);}
	public default Vec4f min(Vec4f other, Vec4f result){return min(other.getX(), other.getY(), other.getZ(), other.getW(), result);}
	public default Vec4f min(float x, float y, float z, float w) {return min(x, y, z, w, this);}
	public default Vec4f min(float x, float y, float z, float w, Vec4f result){return result.set(Math.min(getX(), x), Math.min(getY(), y), Math.min(getZ(), z), Math.min(getW(), w));}
	
	public default Vec4f max(Vec4f other) {return max(other, this);}
	public default Vec4f max(Vec4f other, Vec4f result){return max(other.getX(), other.getY(), other.getZ(), other.getW(), result);}
	public default Vec4f max(float x, float y, float z, float w) {return max(x, y, z, w, this);}
	public default Vec4f max(float x, float y, float z, float w, Vec4f result){return result.set(Math.max(getX(), x), Math.max(getY(), y), Math.max(getZ(), z), Math.max(getZ(), z));}
	
	public default Vec4f difference(Vec4f other) {return difference(other, this);}
	public default Vec4f difference(Vec4f other, Vec4f result){return difference(other.getX(), other.getY(), other.getZ(), other.getW(), result);}
	public default Vec4f difference(float x, float y, float z, float w) {return difference(x, y, z, w, this);}
	public default Vec4f difference(float x, float y, float z, float w, Vec4f result){return result.set(getX() - x, getY() - y, getZ() - z, getW() - w);}
	
	@Override
	public default Vec4f clamp(float min, float max){return clamp(min, max, ALL);}
	public default Vec4f clamp(float min, float max, Vec4f result){return clamp(min, max, result, ALL);}
	@Override
	public default Vec4f clamp(float min, float max, int filter){return clamp(min, max, this, filter);}
	public default Vec4f clamp(float min, float max, Vec4f result, int filter){ return result.set((filter & X) == 0 ? getX() : MathUtils.clamp(min, max, getX()), (filter & Y) == 0 ? getY() : MathUtils.clamp(min, max, getY()), (filter & Z) == 0 ? getZ() : MathUtils.clamp(min, max, getZ()), (filter & W) == 0 ? getW() : MathUtils.clamp(min, max, getW()));}
	
	@Override
	public default Vec4f store(ByteBuffer buffer)
	{
		buffer.putFloat(getX()).putFloat(getY()).putFloat(getZ()).putFloat(getW());
		return this;
	}
	
	@Override
	public default Vec4f load(ByteBuffer buffer)
	{
		return set(buffer.getFloat(), buffer.getFloat(), buffer.getFloat(), buffer.getFloat());
	}
	
	@Override
	public default Vec4f store(FloatBuffer buffer)
	{
		buffer.put(getX()).put(getY()).put(getZ()).put(getW());
		return this;
	}
	
	@Override
	public default Vec4f load(FloatBuffer buffer)
	{
		return set(buffer.get(), buffer.get(), buffer.get(), buffer.get());
	}
	
	@Override
	public default Vec4b asByte(){return isMutable() ? Vec4b.newMutable((byte)MathUtils.floor(getX()), (byte)MathUtils.floor(getY()), (byte)MathUtils.floor(getZ()), (byte)MathUtils.floor(getW())) : Vec4b.newVec((byte)MathUtils.floor(getX()), (byte)MathUtils.floor(getY()), (byte)MathUtils.floor(getZ()), (byte)MathUtils.floor(getW()));}
	@Override
	public default Vec4s asShort(){return isMutable() ? Vec4s.newMutable((short)MathUtils.floor(getX()), (short)MathUtils.floor(getY()), (short)MathUtils.floor(getZ()), (short)MathUtils.floor(getW())) : Vec4s.newVec((short)MathUtils.floor(getX()), (short)MathUtils.floor(getY()), (short)MathUtils.floor(getZ()), (short)MathUtils.floor(getW()));}
	@Override
	public default Vec4i asInt(){return isMutable() ? Vec4i.newMutable(MathUtils.floor(getX()), MathUtils.floor(getY()), MathUtils.floor(getZ()), MathUtils.floor(getW())) : Vec4i.newVec(MathUtils.floor(getX()), MathUtils.floor(getY()), MathUtils.floor(getZ()), MathUtils.floor(getW()));}
	@Override
	public default Vec4l asLong() {return isMutable() ? Vec4l.newMutable(MathUtils.floor(getX()), MathUtils.floor(getY()), MathUtils.floor(getZ()), MathUtils.floor(getW())) : Vec4l.newVec(MathUtils.floor(getX()), MathUtils.floor(getY()), MathUtils.floor(getZ()), MathUtils.floor(getW()));}
	@Override
	public default Vec4d asDouble(){return isMutable() ? Vec4d.newMutable(getX(), getY(), getZ(), getW()) : Vec4d.newVec(getX(), getY(), getZ(), getW());}

	
	@Override
	public default Vec4f asMutable(){return isMutable() ? this : newMutable(this);}
	@Override
	public default Vec4f asImmutable(){return isMutable() ? newVec(this) : this;}
	@Override
	public default Vec4f copyAsMutable(){return newMutable(this);}
	@Override
	public default Vec4f copyAsImmutable(){return newVec(this);}
}
