#ifndef _SYSELEMENTS_HPP_
#define _SYSELEMENTS_HPP_

#include <string>
#include <vector>
#include <unordered_map>
#include <stdexcept>

using namespace std;

class SystemElement
{
public:
    string name;
    SystemElement(string name) : name(name) {};
};

template <typename T>
class SystemElementContainer : public SystemElement
{
private:
    //static_assert(is_base_of<SystemElement, T>::value, "T shall be a system element descendant !");
    unsigned int iter_count;
    unordered_map<string, T*> content;

    void add(unordered_map<string, T*> el_content)
    {
        for (const auto &[key, value] : el_content)
        {
            add(value);
        }
    }

public:
    SystemElementContainer(string name) : SystemElement(name) {};
    SystemElementContainer(string name, vector<T *> elements) : SystemElement(name)
    {
        add(elements);
    }
    bool exists(string name)
    {
        try
        {
            get(name);
            return true;
        }
        catch (const exception &e)
        {
            return false;
        }
    }
    void add(T* element)
    {
        if ("" == element->name)
        {
            throw runtime_error("System elements with empty names are forbidden");
        }
        if (exists(element->name))
        {
            throw runtime_error("System elements of same names are forbidden");
        }
        content.insert({element->name, element});
    }
    void add(SystemElementContainer<T> *elements)
    {
        add(elements->content);
    }
    void add(vector<T*> elements)
    {
        for (auto &element : elements)
        {
            add(element);
        }
    }
    T *get(string name)
    {
        auto it = content.find(name);
        if (it != content.end())
        {
            return it->second;
        }
        else
        {
            throw runtime_error("System elements not found");
        }
    }
};

#endif