#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_FATAL("Couldn't open the device, audio won't be initalized");

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

        // Create main source on which everything would be played
        // TODO: create other sources for music, sound effects etc.
        alGenSources(1, &audio_structures.main_source);

        alSourcef(audio_structures.main_source, AL_PITCH,    1);
        alSourcef(audio_structures.main_source, AL_GAIN,     1);

        alSource3f(audio_structures.main_source, AL_POSITION, 0, 0, 0);
        alSource3f(audio_structures.main_source, AL_VELOCITY, 0, 0, 0);
    }

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