#include <Display/WidgetDisplay/RegisterDisplay/RegisterDisplay.hpp>
#include <Widget/Label/Label.hpp>
#include <Widget/Layout/Center/Center.hpp>
#include <Widget/Layout/Column/Column.hpp>
#include <Widget/Button/Button.hpp>
#include <Display/WidgetDisplay/ServerSelection/ServerSelection.hpp>
#include <Widget/Layout/CompoundWidget/Form/Form.hpp>
#include <Widget/TextField/TextField.hpp>
#include <Display/WidgetDisplay/WorldDisplay/WorldDisplay.hpp>
#include <Display/WidgetDisplay/MessageDisplay/MessageDisplay.hpp>
#include <Utilities/ServerSessionTable.hpp>
#include <World/RemoteWorld/RemoteWorld.hpp>
#include <Display/WidgetDisplay/RegisteringDisplay/RegisteringDisplay.hpp>
#include <Widget/Panel/Panel.hpp>
#include <Widget/Layout/Row/Row.hpp>
#include <Widget/Builder/Builder.hpp>
#include <Widget/Layout/Column/HorizontalSeparator/HorizontalSeparator.hpp>
#include <Widget/Button/BackButton/BackButton.hpp>

void RegisterDisplay::onRegister()
{
    if (m_server.size() != 0 && m_userName.size() != 0 && m_password.size())
    {
        if (m_password != m_password2)
        {
            m_message = "Passwords did not match!";
            return;
        };

        Display::setCurrent<RegisteringDisplay>(m_userName, m_password, m_server);
    };
};

RegisterDisplay::RegisterDisplay(std::string message, std::string server, std::string userName) :
    m_message(message),
    m_server(server),
    m_userName(userName)
{
    setWidget(
        new Center(
            new Form(
                "Register",
                new Column(
                    new Builder<std::string>(m_message, [](std::string msg)
                    {
                        return new Label(msg);
                    }),

                    new Panel(glm::ivec2(100, 2)),

                    new Label("Server:"),
                    new TextField(m_server, [this]() { this->onRegister(); }),
                    new Label("User name:"),
                    new TextField(m_userName, [this]() { this->onRegister(); }),
                    new Label("Password:"),
                    new TextField(m_password, [this]() { this->onRegister(); }, true),
                    new Label("Re-type password:"),
                    new TextField(m_password2, [this]() { this->onRegister(); }, true)
                ),

                Layout::WithProportion<1>(),
                new Button("Register", [this]() { this->onRegister(); }),
                Layout::WithProportion<1>(),
                new BackButton("Cancel")
            )
        )
    );
};