#pragma once

#include "nlohmann/json.hpp"

namespace nsDebug {
    using stHardware = struct _stHardware {
        bool Motor{ false };
        bool Input{ false };
        bool Output{ false };
        // custom
        bool ETEL{ false };
    };

    using stDebug = struct _stDebug {
        stHardware Hardware;
        stHardware InitController;
        stHardware MmfLog;
    };
}

namespace nsDebug {
    using namespace nlohmann;

    template <typename T>
    inline void getValue(const nlohmann::json& j, const std::string& fieldname, T& value)
    {
        if (j.contains(fieldname))
        {
            j.at(fieldname).get_to(value);
        }
    }

    void from_json(const json& j, stHardware& x);
    void to_json(json& j, const stHardware& x);

    void from_json(const json& j, stDebug& x);
    void to_json(json& j, const stDebug& x);

    inline void from_json(const json& j, stHardware& x)
    {
        getValue(j, "Motor", x.Motor);
        getValue(j, "Input", x.Input);
        getValue(j, "Output", x.Output);
        //
        getValue(j, "ETEL", x.ETEL);
    }

    inline void to_json(json& j, const stHardware& x)
    {
        j = nlohmann::json{
            { "Motor", x.Motor },
            { "Input", x.Input },
            { "Output", x.Output },
            //
            { "ETEL", x.ETEL }
        };
    }

    inline void from_json(const json& j, stDebug& x)
    {
        getValue(j, "Hardware", x.Hardware);
        getValue(j, "InitController", x.InitController);
        getValue(j, "MmfLog", x.MmfLog);
    }

    inline void to_json(json& j, const stDebug& x)
    {
        j = nlohmann::json{
            { "Hardware", x.Hardware },
            { "InitController", x.InitController },
            { "MmfLog", x.MmfLog }
        };
    }
}