#pragma once

#include "RmlUi/Core/Element.h"
#include "RmlUi/Core/Factory.h"
#include "RmlUi/Core/XMLParser.h"

namespace UI::FieldText {

class Component : public Rml::Element {

  public:
    Component(const Rml::String &tag) : Rml::Element(tag) {}

  protected:
    void OnAttributeChange(const Rml::ElementAttributes &changed) override {
        Rml::Element::OnAttributeChange(changed);

        if (!built) {
            Build();
            built = true;
        }
    }

  private:
    bool built = false;

    void Build() {
        Rml::String type = GetAttribute<Rml::String>("type", "text");
        Rml::String data_attr_value = GetAttribute<Rml::String>("data-attr-value", "");
        Rml::String data_event_change = GetAttribute<Rml::String>("data-event-change", "");

        Rml::XMLAttributes input_attrs;
        input_attrs["type"] = type;
        input_attrs["data-attr-value"] = data_attr_value;
        input_attrs["data-event-change"] = data_event_change;
        AppendChild(Rml::Factory::InstanceElement(this, "input", "input", input_attrs));
    }
};

} // namespace UI::FieldText
