
#include "sound.h"
#include <map>
using namespace std;

#include "Log.h"

vector<sf::SoundBuffer*> m_buffers;
vector<sf::Sound*> m_sounds;

void loadSounds()
{
    map<_soundId, string> soundFiles;
    soundFiles[SID_TEST] = "test.wav";
    soundFiles[SID_ITWORKS] = "itworks.wav";
    soundFiles[SID_ITWORKS2] = "itworks.wav";

    for (int i = 0; i < SID_MAX; i++) {
        string soundFile = soundFiles[(_soundId)i];
        sf::SoundBuffer* buffer = new sf::SoundBuffer();
        if ( !buffer->LoadFromFile(soundFile) ) {
            g_log.log(LL_ERROR, "Could not load sound %s", soundFile.c_str());
            delete buffer;
            m_buffers.push_back(NULL);
            m_sounds.push_back(NULL);
            continue;
        }
        g_log.log(LL_INFO, "Loaded sound %s", soundFile.c_str());
        m_buffers.push_back(buffer);

        sf::Sound* sound = new sf::Sound(*buffer);
        m_sounds.push_back(sound);
    }
}

void clearSounds()
{
    g_log.log(LL_INFO, "Clearing sounds");
    for (int i = 0; i < SID_MAX; i++) {
        delete m_sounds[i];
        delete m_buffers[i];
    }
}

void playSound(int soundId)
{
    sf::Sound* sound = m_sounds[soundId];
    if ( sound )
        sound->Play();
}
