#pragma once

#include <RmlUi/Core.h>
#include <RmlUi/Core/Elements/ElementFormControlInput.h>
#include <RmlUi/Debugger.h>
#include <RmlUi_Backend.h>
#include <string>

using std::string;

using Rml::DataModelHandle;
using Rml::Event;
using Rml::VariantList;

namespace UI::Login {

struct ViewModel {
    string username = "";
    string password = "";
};

class Page {
  public:
    Rml::ElementDocument *document;

    ViewModel vm;

    void initialize(Rml::Context *context) {
        if (Rml::DataModelConstructor constructor = context->CreateDataModel("login")) {
            if (auto sh = constructor.RegisterStruct<ViewModel>()) {
                sh.RegisterMember("username", &ViewModel::username);
                sh.RegisterMember("password", &ViewModel::password);
            }

            constructor.BindEventCallback(
                "on_username_change",
                [this](DataModelHandle handle, Event &event, const VariantList &) {
                    auto value_str = event.GetParameter<Rml::String>("value", "");
                    vm.username = value_str;
                    handle.DirtyVariable("vm");
                });

            constructor.BindEventCallback(
                "on_password_change",
                [this](DataModelHandle handle, Event &event, const VariantList &) {
                    auto value_str = event.GetParameter<Rml::String>("value", "");
                    vm.password = value_str;
                    handle.DirtyVariable("vm");
                });

            constructor.Bind("vm", &vm);

            document = context->LoadDocument("build/assets/ui/pages/login/login.rml");
        }
    }

    void show() {
        document->Show();
    }

    void hide() {
        document->Hide();
    }
};

} // namespace UI::Login
