package electrosphere.audio;

import electrosphere.logger.LoggerInterface;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.joml.Matrix4f;
import org.lwjgl.openal.AL;
import org.lwjgl.openal.ALC;
//import org.lwjgl.openal.*;
import org.lwjgl.openal.ALC10;
import org.lwjgl.openal.ALCCapabilities;
import static org.lwjgl.system.MemoryUtil.NULL;

public class AudioEngine {
    private long device;

    private long context;

    private AudioListener listener;

    private final List<AudioBuffer> soundBufferList;

    private final Map<String, AudioSource> soundSourceMap;
    
    
    private float engineGain = 1.0f;
    

    public AudioEngine() {
        soundBufferList = new ArrayList();
        soundSourceMap = new HashMap();
    }

    public void init() {
        try {
            initDevice();
        } catch (Exception ex) {
            LoggerInterface.loggerEngine.ERROR("Error initializing audio device", ex);
        }
        listener = new AudioListener();
    }
    
    void initDevice() throws Exception{
        this.device = ALC10.alcOpenDevice((ByteBuffer) null);
        if (device == NULL) {
            throw new IllegalStateException("Failed to open the default OpenAL device.");
        }
        ALCCapabilities deviceCaps = ALC.createCapabilities(device);
        this.context = ALC10.alcCreateContext(device, (IntBuffer) null);
        if (context == NULL) {
            throw new IllegalStateException("Failed to create OpenAL context.");
        }
        ALC10.alcMakeContextCurrent(context);
        AL.createCapabilities(deviceCaps);
    }
    
    
    public void setGain(float gain){
        engineGain = gain;
    }
    
    public float getGain(){
        return engineGain;
    }
    
    public void shutdown(){
        ALC10.alcDestroyContext​(context);
        ALC10.alcCloseDevice​(device);
    }
    
    
}
