#include "game/audio/audio.hpp"

#include <al.h>
#include <alc.h>

#include "game/core.hpp"
#include "common/logger.hpp"

static struct {
    ALCcontext* context;
    ALCdevice* device;

    unsigned int main_source;
    unsigned int buffer;
} audio_structures;

namespace polygun::audio {

    void init() {
        audio_structures.device = alcOpenDevice(0);
        if (audio_structures.device == NULL) {
			LOG_ERROR("Couldn't open the device, audio won't be initalized");
			return;
		}

        audio_structures.context = alcCreateContext(audio_structures.device, 0);
        if (!alcMakeContextCurrent(audio_structures.context)) {
			LOG_ERROR("Couldn't make the context current, audio won't be initalized");
			alcCloseDevice(audio_structures.device);
			return;
		}
    }

	void cleanup() {
		alcMakeContextCurrent(nullptr);
		if(audio_structures.context)
			alcDestroyContext(audio_structures.context);
		if(audio_structures.device)
			alcCloseDevice(audio_structures.device);
	}
}
