#pragma once

#include <cstdint>

class LayoutManager
{
    std::string m_path;
    std::int64_t m_file_last_write_time = 0;
public:
    std::map<uint16_t, std::shared_ptr<class Node>> m_layouts;
    bool m_loaded = false;
    std::function<void(bool reloaded)> on_loaded;
    std::function<void()> on_reloading;
    void unload();
    void create();
    bool load(const char* path);
    bool parse(const std::string& xml_string) noexcept;
    bool reload();
    class Node* operator[](uint16_t id) 
    {
        auto i = m_layouts.find(id);
        return i == m_layouts.end() ? nullptr : i->second.get();
    }
    class Node* get(uint16_t id)
    {
        auto i = m_layouts.find(id);
        return i == m_layouts.end() ? nullptr : i->second.get();
    }
    template<class T = Node> std::shared_ptr<T> get_ref(const char* name)
    {
        auto i = m_layouts.find(const_hash(name));
        return i == m_layouts.end() ? nullptr : std::dynamic_pointer_cast<T>(i->second);
    }
    template<class T = Node> std::shared_ptr<T> instantiate(const char* name)
    {
        if (auto layout = get_ref(name))
            return layout->m_children[0]->clone<T>();
        return nullptr;
    }
    void restore_context();
    void clear_context();
    //Node& operator[](const char* ids) { return m_layouts[const_hash(ids)]; }
};
