#include "client/audio/AudioManager.h"

#include "client/Client.h"
#include "client/audio/AudioBuffer.h"

namespace blitz {
namespace audio {

AudioManager::AudioManager(Client* client) : m_Client(client) {
	m_Listener.SetPosition({});
	m_Listener.SetVelocity({});
	m_Listener.SetGain(1.0f);
	m_Listener.SetOrientation(blitz::Vec3f{0.0, 0.0, 0.0}, blitz::Vec3f{0.0, 1.0, 0.0});
	InitMainMenuMusic();
	m_MenuMusic->Play();
	m_Client->BindListener(this);
}

void AudioManager::InitMainMenuMusic() {
	AudioData audioData = AudioLoader::LoadAudioFile("sessionD.wav");
	AudioBufferPtr audioBuffer = std::make_unique<AudioBuffer>(std::move(audioData));
	m_MenuMusic = std::make_unique<AudioSource>();
	m_MenuMusic->SetBuffer(std::move(audioBuffer));
	m_MenuMusic->SetPosition({});
	m_MenuMusic->SetVelocity({});
	m_MenuMusic->SetPitch(1.0f);
	m_MenuMusic->SetLooping(true);
}

void AudioManager::OnGameJoin() {
	m_MenuMusic->Stop();
}

void AudioManager::OnGameLeave() {
	m_MenuMusic->Play();
}

AudioManager::~AudioManager() {
	m_Client->UnbindListener(this);
}

}  // namespace audio
}  // namespace blitz
