package electrosphere.audio;

import org.joml.Vector3f;
import org.lwjgl.openal.AL11;

import electrosphere.logger.LoggerInterface;

import org.lwjgl.openal.AL10;

/**
 * A source of audio in the aural space
 */
public class AudioSource {
    
    //The id for the source
    int sourceId;
    
    /**
     * Creates an audio source object
     * @param loop if true, will loop audio, otherwise will not
     * @param relative if true, will make the audio source position relative to the listener based on position
     */
    protected static AudioSource create(boolean loop, boolean relative){
        AudioSource rVal = new AudioSource();
        rVal.sourceId = AL10.alGenSources();
        if (loop) {
            AL10.alSourcei(rVal.sourceId, AL10.AL_LOOPING, AL10.AL_TRUE);
        }
        if (relative) {
            AL10.alSourcei(rVal.sourceId, AL10.AL_SOURCE_RELATIVE, AL10.AL_TRUE);
        }
        return rVal;
    }
    
    /**
     * Sets the buffer that this source pulls from
     * @param bufferId The id of the buffer
     */
    public void setBuffer(int bufferId) {
        stop();
        AL10.alSourcei(sourceId, AL10.AL_BUFFER, bufferId);
    }

    /**
     * Sets the position of the audio source
     * @param position the position
     */
    public void setPosition(Vector3f position) {
        AL10.alSource3f(sourceId, AL10.AL_POSITION, position.x, position.y, position.z);
    }

    /**
     * Sets the speed of the audio source
     * @param speed the speed
     */
    public void setSpeed(Vector3f speed) {
        AL10.alSource3f(sourceId, AL10.AL_VELOCITY, speed.x, speed.y, speed.z);
    }

    /**
     * Sets the temporal offset of the source (ie how far into the clip to start playingf)
     * @param time The time in seconds
     */
    public void setOffset(float time){
        AL10.alSourcef(sourceId, AL11.AL_SEC_OFFSET, time);
    }

    /**
     * Sets the gain of the audio source
     * @param gain the gain
     */
    public void setGain(float gain) {
        LoggerInterface.loggerAudio.DEBUG("Set Gain: " + gain);
        AL10.alSourcef(sourceId, AL10.AL_GAIN, gain);
    }

    /**
     * Sets an arbitrary property on the audio source
     * @param param The param flag
     * @param value The value to set the param to
     */
    public void setProperty(int param, float value) {
        AL10.alSourcef(sourceId, param, value);
    }

    /**
     * Plays the audio source
     */
    public void play() {
        AL10.alSourcePlay(sourceId);
    }

    /**
     * Gets whether the audio source is currently playing or not
     * @return True if it is playing, false otherwise
     */
    public boolean isPlaying() {
        return AL10.alGetSourcei(sourceId, AL10.AL_SOURCE_STATE) == AL10.AL_PLAYING;
    }

    /**
     * Pauses the audio source
     */
    public void pause() {
        AL10.alSourcePause(sourceId);
    }

    /**
     * Stops the audio source
     */
    public void stop() {
        AL10.alSourceStop(sourceId);
    }

    /**
     * Cleans up the source
     */
    public void cleanup() {
        stop();
        AL10.alDeleteSources(sourceId);
    }
}
