package speiger.src.coreengine.math.vector.bytes;

import java.nio.ByteBuffer;

import speiger.src.coreengine.math.MathUtils;
import speiger.src.coreengine.math.vector.doubles.Vec4d;
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 Vec4b extends Vecb
{
	public static final Vec4b ZERO = newVec();
	public static final Vec4b MINUS_ONE = newVec((byte)-1);
	public static final Vec4b ONE = newVec((byte)1);
	
	public static Vec4b newMutable(){return new Vec4bMutable();}
	public static Vec4b newMutable(byte value){return new Vec4bMutable(value);}
	public static Vec4b newMutable(byte x, byte y, byte z, byte w){return new Vec4bMutable(x, y, z, w);}
	public static Vec4b newMutable(Vec4b vec){return newMutable(vec.getX(), vec.getY(), vec.getZ(), vec.getW());}
	
	public static Vec4b newVec(){return new Vec4bImmutable();}
	public static Vec4b newVec(byte value){return new Vec4bImmutable(value);}
	public static Vec4b newVec(byte x, byte y, byte z, byte w){return new Vec4bImmutable(x, y, z, w);}
	public static Vec4b newVec(Vec4b vec){return newVec(vec.getX(), vec.getY(), vec.getZ(), vec.getW());}
	
	public byte getX();
	public byte getY();
	public byte getZ();
	public byte getW();
	public Vec4b setX(byte x);
	public Vec4b setY(byte y);
	public Vec4b setZ(byte z);
	public Vec4b setW(byte w);
	@Override
	public default byte[] asArray(){return new byte[]{getX(), getY(), getZ(), getW()};}
	@Override
	public Vec4b copy();
	@Override
	public default Vec4b abs(){return set((byte)Math.abs(getX()), (byte)Math.abs(getY()), (byte)Math.abs(getZ()), (byte)Math.abs(getW()));}
	@Override
	public default Vec4b negate(){return set((byte)0, (byte)0, (byte)0, (byte)0);}
	@Override
	public default Vec4b invert(){return set((byte)-getX(), (byte)-getY(), (byte)-getZ(), (byte)-getW());}
	
	@Override
	public default Vec4b add(byte value){return add(value, value, value, value);}	
	public default Vec4b add(Vec4b value){return add(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default Vec4b add(byte x, byte y, byte z, byte w){return set((byte)(getX() + x), (byte)(getY() + y), (byte)(getZ() + z), (byte)(getW() + w));}

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

	public default long dotProduct(Vec4b value){return dotProduct(value.getX(), value.getY(), value.getZ(), value.getW());}
	public default long dotProduct(byte x, byte y, byte z, byte w){return (getX() * x) + (getY() * y) + (getZ() * z) + (getW() * w);};
	
	public default Vec4b min(Vec4b other) {return min(other, this);}
	public default Vec4b min(Vec4b other, Vec4b result){return min(other.getX(), other.getY(), other.getZ(), other.getW(), result);}
	public default Vec4b min(byte x, byte y, byte z, byte w) {return min(x, y, z, w, this);}
	public default Vec4b min(byte x, byte y, byte z, byte w, Vec4b result){return result.set((byte)Math.min(getX(), x), (byte)Math.min(getY(), y), (byte)Math.min(getZ(), z), (byte)Math.min(getW(), w));}
	
	public default Vec4b max(Vec4b other) {return max(other, this);}
	public default Vec4b max(Vec4b other, Vec4b result){return max(other.getX(), other.getY(), other.getZ(), other.getW(), result);}
	public default Vec4b max(byte x, byte y, byte z, byte w) {return max(x, y, z, w, this);}
	public default Vec4b max(byte x, byte y, byte z, byte w, Vec4b result){return result.set((byte)Math.max(getX(), x), (byte)Math.max(getY(), y), (byte)Math.max(getZ(), z), (byte)Math.max(getZ(), z));}
	
	public default Vec4b difference(Vec4b other) {return difference(other, this);}
	public default Vec4b difference(Vec4b other, Vec4b result){return difference(other.getX(), other.getY(), other.getZ(), other.getW(), result);}
	public default Vec4b difference(byte x, byte y, byte z, byte w) {return difference(x, y, z, w, this);}
	public default Vec4b difference(byte x, byte y, byte z, byte w, Vec4b result){return result.set((byte)(getX() - x), (byte)(getY() - y), (byte)(getZ() - z), (byte)(getW() - w));}
	
	@Override
	public default Vec4b clamp(byte min, byte max){return clamp(min, max, ALL);}
	public default Vec4b clamp(byte min, byte max, Vec4b result){return clamp(min, max, result, ALL);}
	@Override
	public default Vec4b clamp(byte min, byte max, int filter){return clamp(min, max, this, filter);}
	public default Vec4b clamp(byte min, byte max, Vec4b 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 Vec4b store(ByteBuffer buffer)
	{
		buffer.put(getX()).put(getY()).put(getZ()).put(getW());
		return this;
	}
	
	@Override
	public default Vec4b load(ByteBuffer buffer)
	{
		return set(buffer.get(), buffer.get(), buffer.get(), buffer.get());
	}
	
	@Override
	public default Vec4s asShort(){return isMutable() ? Vec4s.newMutable(getX(), getY(), getZ(), getW()) : Vec4s.newVec(getX(), getY(), getZ(), getW());}
	@Override
	public default Vec4i asInt(){return isMutable() ? Vec4i.newMutable(getX(), getY(), getZ(), getW()) : Vec4i.newVec(getX(), getY(), getZ(), getW());}
	@Override
	public default Vec4l asLong(){return isMutable() ? Vec4l.newMutable(getX(), getY(), getZ(), getW()) : Vec4l.newVec(getX(), getY(), getZ(), getW());}
	@Override
	public default Vec4f asFloat() {return isMutable() ? Vec4f.newMutable(getX(), getY(), getZ(), getW()) : Vec4f.newVec(getX(), getY(), getZ(), getW());}
	@Override
	public default Vec4d asDouble(){return isMutable() ? Vec4d.newMutable(getX(), getY(), getZ(), getW()) : Vec4d.newVec(getX(), getY(), getZ(), getW());}

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