#include "MultiAudioCombineJson.h"

#include "AudioContext.h"
namespace UkuiAudioFramwork
{
MultiAudioCombineJson::MultiAudioCombineJson()
{
    m_fileName = getenv("HOME");
    m_fileName += MULTI_AUDIO_COMBINE_JSON;
    loadJson();
    initJson();
}

bool MultiAudioCombineJson::isContain(const MultiAudioCombineInfo&) const
{

}

void MultiAudioCombineJson::insert(const MultiAudioCombineInfo& info)
{
    auto str = info.type ? "output-combine" : "input-combine";


    if (m_json[str]["status"] == true) {
        m_json[str]["devices"] = info.deviceList; // 直接赋值设备列表
    } else {
        m_json[str]["devices"].clear(); // 当status为false时，清空devices数组
    }
}

std::optional<bool> MultiAudioCombineJson::getValue(const std::string& key1, const std::string& key2) const
{
    try {
        if (m_json.contains(key1)) {
            const nlohmann::json& combine = m_json[key1];

            if (combine.contains(key2) && combine[key2].is_boolean()) {
                return combine[key2];
            } else {
                AUDIO_ERROR("The key value {} does not exist in {}.", key2, key1);
            }
        } else {
            AUDIO_ERROR("{} does not exist.", key1);
        }
    } catch (nlohmann::json::parse_error& e) {
        AUDIO_ERROR("JSON parsing error, error: {}.", e.what());
    } catch (nlohmann::json::out_of_range& e) {
        AUDIO_ERROR("JSON key out of range, error: {}.", e.what());
    } catch (nlohmann::json::type_error& e) {
        AUDIO_ERROR("JSON type error, error: {}.", e.what());
    } catch (std::exception& e) {
        AUDIO_ERROR("Other abnormalities, error: {}.", e.what());
    }
}

void MultiAudioCombineJson::setValue(const std::string& key1, const std::string& key2, bool status)
{
    try {
        if (m_json.contains(key1)) {
            nlohmann::json& combine = m_json[key1];

            if (combine.contains(key2) && combine[key2].is_boolean()) {
                combine[key2] = status;
            } else {
                AUDIO_ERROR("The key value {} does not exist in {}.", key2, key1);
            }
        } else {
            AUDIO_ERROR("{} does not exist.", key1);
        }
    } catch (nlohmann::json::parse_error& e) {
        AUDIO_ERROR("JSON parsing error, error: {}.", e.what());
    } catch (nlohmann::json::out_of_range& e) {
        AUDIO_ERROR("JSON key out of range, error: {}.", e.what());
    } catch (nlohmann::json::type_error& e) {
        AUDIO_ERROR("JSON type error, error: {}.", e.what());
    } catch (std::exception& e) {
        AUDIO_ERROR("Other abnormalities, error: {}.", e.what());
    }

    writeToJson();
}

void MultiAudioCombineJson::initJson()
{
    auto input = m_json.find("input-combine");
    if (input == m_json.end()) {
        m_json["input-combine"]["status"] = false;
        m_json["input-combine"]["devices"] = nlohmann::json::array();
    }

    auto output = m_json.find("output-combine");
    if (output == m_json.end()) {
        m_json["output-combine"]["status"] = false;
        m_json["output-combine"]["devices"] = nlohmann::json::array();
    }

    writeToJson();
}
}
