#include "client/audio/AudioSource.h"

#include <AL/al.h>

namespace blitz {
namespace audio {

AudioSource::AudioSource() {
	alGenSources(1, &m_ID);
}

AudioSource::~AudioSource() {
	alDeleteSources(1, &m_ID);
}

void AudioSource::SetGain(float a_Gain) {
	alSourcef(m_ID, AL_GAIN, a_Gain);
}

void AudioSource::SetPitch(float a_Pitch) {
	alSourcef(m_ID, AL_PITCH, a_Pitch);
}

void AudioSource::SetPosition(const Vec3f& a_Position) {
	alSource3f(m_ID, AL_POSITION, a_Position.x, a_Position.y, a_Position.z);
}

void AudioSource::SetDirection(const Vec3f& a_Direction) {
	alSource3f(m_ID, AL_DIRECTION, a_Direction.x, a_Direction.y, a_Direction.z);
}

void AudioSource::SetVelocity(const Vec3f& a_Velocity) {
	alSource3f(m_ID, AL_VELOCITY, a_Velocity.x, a_Velocity.y, a_Velocity.z);
}

void AudioSource::SetLooping(bool a_Looping) {
	alSourcei(m_ID, AL_LOOPING, a_Looping);
}

void AudioSource::SetBuffer(AudioBufferPtr&& a_Buffer) {
	alSourcei(m_ID, AL_BUFFER, a_Buffer->GetID());
	m_Buffer = std::move(a_Buffer);
}

AudioSource::SourceState AudioSource::GetSourceState() const {
	int state;

	alGetSourcei(m_ID, AL_SOURCE_STATE, &state);

	return SourceState(state - AL_INITIAL);
}

void AudioSource::Play() {
	alSourcePlay(m_ID);
}

void AudioSource::Pause() {
	alSourcePause(m_ID);
}

void AudioSource::Stop() {
	alSourceStop(m_ID);
}

}  // namespace audio
}  // namespace blitz