#include "AudioManager.h"

#ifdef __linux__
#ifdef HAS_ALSA
#include "AlsaInterface.h"
#else
#include "NullAudioInterface.h"
#endif
#endif

AudioManager::AudioManager()
    : mAudioDevices(),
      mAudioInterface()
{
#ifdef __linux__
#ifdef HAS_ALSA
    mAudioInterface = AlsaInterface::Create();
#else
    mAudioInterface = NullAudioInterface::Create();
#endif
#endif
}

AudioManager::~AudioManager()
{

}

std::unique_ptr<AudioManager> AudioManager::Create()
{
    return std::make_unique<AudioManager>();
}

void AudioManager::addAudioDevice(AudioDevicePtr device)
{
    mAudioDevices.push_back(std::move(device));
}

std::size_t AudioManager::getNumAudioDevices() const
{
    return mAudioDevices.size();
}

AudioDevice* AudioManager::getAudioDevice(unsigned idx) const
{
    if (idx < mAudioDevices.size())
    {
        return mAudioDevices[idx].get();
    }
    return nullptr;
}

void AudioManager::play(AudioSample* sample, unsigned duration)
{
    if (mAudioDevices.size() == 0)
    {
        mAudioDevices.push_back(AudioDevice::Create());
    }
    mAudioInterface->play(mAudioDevices[0].get(), sample, duration);
}
