#include "AudioManager.h"

namespace UkuiAudioFramwork
{
AudioManager::AudioManager()
{
    if (checkAudioService("pipewire"))
        m_type = BackendType::PIPEWIRE;
    else if (checkAudioService("pulseaudio"))
        m_type = BackendType::PULSEAUDIO;
    else
        m_type = BackendType::UNKNOWN;
}

std::string AudioManager::getBackend() const
{
    return getBackendName(m_type);
}

void AudioManager::setBackend(BackendType type)
{

}

bool AudioManager::checkAudioService(const std::string& name)
{
//    std::ifstream psOutput("/bin/ps");
    std::ostringstream cmd;
    cmd << "/bin/ps -e | grep " << name;
    std::string command = cmd.str();

    FILE* pipe = popen(command.c_str(), "r");
    if (!pipe) return false;

    char buffer[128];
    std::string result = "";
    while (!feof(pipe)) {
        if (fgets(buffer, 128, pipe) != nullptr) result += buffer;
    }
    pclose(pipe);

    return !result.empty();
}
}
