#pragma once

#include <toml.h>
#include <fstream>
#include <filesystem>
#include <optional>

namespace config {

constexpr auto DEFAULT_FILE = "config.toml";

/// Standalone options
template<typename O>
struct standalone_options {
public:
    /// Load from path
    standalone_options(const std::string &path): path(path) {
        auto config = toml::table({{"c", [&] {
            if(std::filesystem::exists(path))
                return toml::parse_file(path);

            LOG_E("Config file " << path << " not found. Creating default");
            return toml::table();
        }()}});
        val = new O(config["c"]);
    }
    ~standalone_options() {
        delete val;
    }
    /// Write to path
    void save() {
        std::ofstream out;
        out.open(path, std::ios::out | std::ios::trunc);
        out << val->save() << "\n\n";
        out.close();
    }

    O &get() { return *val; }

private:
    std::string path;
    O* val;
};
}