#include "CustomAudioVolumeJson.h"

#include <iostream>
#include <fstream>
#include <filesystem>
#include <iomanip>

namespace UkuiAudioFramwork
{
CustomAudioVolumeJson::CustomAudioVolumeJson()
{
    m_fileName = getenv("HOME");
    m_fileName += CUSTOM_AUDIO_VOLUME_JSON;
    loadJson();
}

bool CustomAudioVolumeJson::isContain(const AudioVolumeInfo& info) const
{
    const auto& card = m_json.find(info.cardName);
    if (card != m_json.end()) {
        const auto& port = (*card).find(info.portName);
        return port != (*card).end();
    }
    return false;
}

void CustomAudioVolumeJson::insert(const AudioVolumeInfo& info)
{
    nlohmann::json& ports = m_json[info.cardName];
    if (ports.find(info.portName) == ports.end()) {
        ports[info.portName] = {
            {"name", info.portName},
            {"volume", info.volume}
        };
    }

    writeToJson();
}
}
