#include "tile/base/exposed_var.h"

#include "gtest/gtest.h"
#include "json/json.h"

namespace tile {
ExposedVarGroup *
GetFancyGroup()
{
    return ExposedVarGroup::FindOrCreate("/a/b");
}

Json::Value
GetTree()
{
    Json::Value jsv;
    jsv["dir"]["sub-dir"]["key"] = 5;
    jsv["key"]                   = "6";
    return jsv;
}

ExposedVar<int> v1("v1", 5);

ExposedVar<double> f1("f1", 6.2, ExposedVarGroup::FindOrCreate("/x/y/z"));

auto GreatGroup = ExposedVarGroup::FindOrCreate("/a/b");

// `/a/b/ds1`
ExposedVarDynamic<std::string> ds1("ds1", [] { return "test_str"; }, GetFancyGroup());

TEST(ExposedVar, Mutate)
{
    auto opt = ExposedVarGroup::TryGet("/");
    ASSERT_TRUE(opt);
    auto &&jsv = *opt;
    ASSERT_EQ(5, jsv["v1"].asInt());
    *v1 = 6;
    jsv = *ExposedVarGroup::TryGet("/");
    ASSERT_EQ(6, jsv["v1"].asInt());
    *v1 = 5;
}

}// namespace tile
