#ifndef IJSON_H
#define IJSON_H

#include <nlohmann/json.hpp>
#include <iostream>
#include <fstream>
#include <filesystem>
#include <iomanip>

namespace UkuiAudioFramwork
{
template <typename T>
class IJson
{
public:
    IJson() = default;

    virtual ~IJson() = default;

public:
    virtual bool isContain(const T&) const = 0;

    virtual void insert(const T&) = 0;

protected:
    void loadJson()
    {
        std::ifstream file(m_fileName);
        if (file.is_open()) {
            file >> m_json;
        } else {
            m_json = nlohmann::json::object();
        }
    }

    void writeToJson()
    {
        std::filesystem::create_directories(std::filesystem::path(m_fileName).parent_path());
        std::ofstream outFile(m_fileName);
        if (!outFile) {
            std::cerr << "Unable to open " << m_fileName << " for writing." << std::endl;
            return;
        }
        outFile << std::setw(4) << m_json << std::endl;
    }

protected:
    mutable std::string m_fileName;
    mutable nlohmann::json m_json;
};
}
#endif // IJSON_H
