#include "audio.h"

Audio::Audio(const std::string &background_path, std::array<std::string, N_SOUNDS> &&sounds_paths)
{
    SfMusicPtr music = std::make_unique<sf::Music>();
    if (music->openFromFile(background_path))
        music->setLoop(true);
    else
        music = nullptr;

    background_music = std::move(music);

    SoundEffectPtr effect;
    for (int i = 0; i < N_SOUNDS; ++i)
    {
        effect = std::make_unique<SoundEffect>();
        if (effect->buffer.loadFromFile(sounds_paths[i]))
        {
            effect->sound.setBuffer(effect->buffer);
            array_sounds[i] = std::move(effect);
        }
        else
        {
            array_sounds[i] = nullptr;
        }
    }
}

bool Audio::setSound(const SOUND_TYPE &type, const std::string &sound_file_path)
{
    SoundEffectPtr effect = std::make_unique<SoundEffect>();

    if (!effect->buffer.loadFromFile(sound_file_path))
        return false;

    effect->sound.setBuffer(effect->buffer);

    array_sounds[type] = std::move(effect);
    return true;
}

void Audio::playSound(const SOUND_TYPE &type)
{
    array_sounds[type]->sound.play();
}

bool Audio::setBackground(const std::string &music_file_path)
{
    SfMusicPtr music = std::make_unique<sf::Music>();

    if (!music->openFromFile(music_file_path))
        return false;

    background_music = std::move(music);
    return true;
}

void Audio::playBackground()
{
    background_music->play();
}

void Audio::stopBackground()
{
    background_music->stop();
}

void Audio::pauseBackground()
{
    background_music->pause();
}

void Audio::setBackgroundVolume(const float &volume)
{
    background_music->setVolume(volume);
}
