#include "../H/sounds.h"

Sounds::Sounds(){
    m_BackgroundMusic.openFromFile("media/music/TheRiseoftheMechbeings.ogg");
    m_BackgroundMusic.setLoop(true);
    m_BackgroundMusic.play();
}
Sounds::~Sounds(){
    m_BackgroundMusic.stop();
}

void Sounds::ManageSounds(Engine* en){
    std::cout << "Sounds" << std::endl;
    while(en->IsWalking()){
        std::cout << "Is walking" << std::endl;
        Move(en);
        usleep(100);
    }
}

sf::SoundBuffer Sounds::SelectSound(sf::SoundBuffer buffer, char& sound){
    switch (sound)
    {
        case 0:
            std::cout << "LEFT" << std::endl;
            buffer.loadFromFile("media/sounds/Footstep_Tile_Left.wav");
            sleep(1);
            nextFootStep = 1;
            break;
        case 1:
            std::cout << "RIGHT" << std::endl;
            buffer.loadFromFile("media/sounds/Footstep_Tile_Right_2.wav");
            sleep(1);
            nextFootStep = 0;
            break;
        default:
            break;
    }
    return buffer;
}

void Sounds::Move(Engine* en){
    if (m_sound1.getStatus() != m_sound1.Playing){
        m_sound1.setBuffer(SelectSound(m_buffer, nextFootStep));
        if(en->m_walk){
            if (en->m_run)
                m_sound1.setPitch(1.2f);
            m_sound1.play();
        }
    }
}