#include "AudioManager.h"

#ifdef __linux__
#include "AlsaInterface.h"
#endif

AudioManager::AudioManager()
    : mAudioDevices(),
      mAudioInterface()
{
#ifdef __linux__
    mAudioInterface = AlsaInterface::Create();
#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));
}

IAudioInterface* AudioManager::GetAudioInterface()
{
    return mAudioInterface.get();
}

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()
{
    if (mAudioDevices.size() == 0)
    {
        mAudioDevices.push_back(AudioDevice::Create());
    }
    mAudioInterface->OpenDevice(mAudioDevices[0]);
    mAudioInterface->Play(mAudioDevices[0]);
}
