package electrosphere.audio;

import org.joml.Vector3d;
import org.joml.Vector3f;

import electrosphere.util.math.MathUtils;

import static org.lwjgl.openal.AL10.*;

/**
 * Encapsulates the listening position in the audio engine
 */
public class AudioListener {
    
    //The position of the listener
    Vector3d position;

    //eye vector for listener
    Vector3f eye = MathUtils.getOriginVectorf();
    
    //up vector for listener
    Vector3f up = new Vector3f(0,1,0);
    
    /**
     * Constructor
     */
    protected AudioListener() {
        this(new Vector3d(0, 0, 0));
    }

    /**
     * Constructor
     * @param position The position of the listener
     */
    protected AudioListener(Vector3d position) {
        this.position = position;
        alListener3f(AL_POSITION, (float)this.position.x, (float)this.position.y, (float)this.position.z);
        alListener3f(AL_VELOCITY, 0, 0, 0);

    }

    /**
     * Sets the speed of the listener
     * @param speed the speed
     */
    protected void setSpeed(Vector3f speed) {
        alListener3f(AL_VELOCITY, speed.x, speed.y, speed.z);
    }

    /**
     * Sets the position of the listener
     * @param position the position
     */
    protected void setPosition(Vector3f position) {
        this.position.set(position.x, position.y, position.z);
        alListener3f(AL_POSITION, position.x, position.y, position.z);
    }

    /**
     * Sets the orientation of the listener
     * @param at the forward vector of the camera
     * @param up The up vector of the camera
     */
    protected void setOrientation(Vector3f at, Vector3f up) {
        this.eye.set(at);
        this.up.set(up);
        float[] data = new float[6];
        data[0] = at.x;
        data[1] = at.y;
        data[2] = at.z;
        data[3] = up.x;
        data[4] = up.y;
        data[5] = up.z;
        alListenerfv(AL_ORIENTATION, data);
    }

    /**
     * Gets the position of the listener
     * @return The position
     */
    public Vector3d getPosition(){
        return position;
    }

    /**
     * Gets the eye vector
     * @return the eye vector
     */
    public Vector3f getEyeVector(){
        return eye;
    }

    /**
     * Gets the up vector
     * @return the up vector
     */
    public Vector3f getUpVector(){
        return up;
    }
    
    
}
