#include "client/audio/AudioListener.h"

#include "blitz/misc/Log.h"
#include <AL/al.h>
#include <AL/alc.h>

namespace blitz {
namespace audio {

AudioListener::AudioListener() {
	ALCdevice* device = alcOpenDevice(nullptr);

	if (device) {
		ALCcontext* context = alcCreateContext(device, nullptr);
		alcMakeContextCurrent(context);
	} else {
		utils::LOGE("[AudioListener] Can't initialize sound !");
	}
}

AudioListener::~AudioListener() {
	ALCcontext* context = alcGetCurrentContext();
	ALCdevice* device = alcGetContextsDevice(context);
	alcMakeContextCurrent(NULL);
	alcDestroyContext(context);
	alcCloseDevice(device);
}

void AudioListener::SetGain(float a_Gain) {
	alListenerf(AL_GAIN, a_Gain);
}

void AudioListener::SetPosition(const Vec3f& a_Position) {
	alListener3f(AL_POSITION, a_Position.x, a_Position.y, a_Position.z);
}

void AudioListener::SetVelocity(const Vec3f& a_Velocity) {
	alListener3f(AL_VELOCITY, a_Velocity.x, a_Velocity.y, a_Velocity.z);
}

void AudioListener::SetOrientation(const Vec3f& a_Orientation, const Vec3f& a_Up) {
	Vec3f data[] = {a_Orientation, a_Up};
	alListenerfv(AL_ORIENTATION, reinterpret_cast<float*>(data));
}

}  // namespace audio
}  // namespace blitz
