#include "AddServerMenu.h"

#include "Globals.h"
#include "Initialisierung.h"
#include "ServerSelection.h"

AddServerMenu::AddServerMenu(Bildschirm* zScreen)
    : Menu(zScreen)
{
    Punkt center = zScreen->getBackBufferSize() / 2;
    TextFeld* nameLabel = initTextFeld(center.x - 150,
        center.y - 50,
        100,
        20,
        TextFeld::Style::Text | TextFeld::Style::VCenter,
        "Name:");
    elements.add(nameLabel);
    name = initTextFeld(nameLabel->getPosition().x + nameLabel->getBreite(),
        nameLabel->getPosition().y,
        200,
        20,
        TextFeld::Style::TextFeld,
        "");
    name->setTastaturEreignis([this](void* p, void* o, TastaturEreignis te) {
        if (te.id == TE_Release && te.virtualKey == T_Tab)
        {
            name->removeStyle(Zeichnung::Style::Fokus);
            address->addStyle(Zeichnung::Style::Fokus);
        }
        return te.virtualKey != T_Tab;
    });
    elements.add(name);

    TextFeld* addressLabel = initTextFeld(center.x - 150,
        center.y - 25,
        100,
        20,
        TextFeld::Style::Text | TextFeld::Style::VCenter,
        "Server Address:");
    elements.add(addressLabel);
    address = initTextFeld(
        addressLabel->getPosition().x + addressLabel->getBreite(),
        addressLabel->getPosition().y,
        200,
        20,
        TextFeld::Style::TextFeld,
        "");
    address->setTastaturEreignis([this](void* p, void* o, TastaturEreignis te) {
        if (te.id == TE_Release && te.virtualKey == T_Tab)
        {
            address->removeStyle(Zeichnung::Style::Fokus);
            sslPort->addStyle(Zeichnung::Style::Fokus);
        }
        return te.virtualKey != T_Tab;
    });
    elements.add(address);

    TextFeld* sslPortLabel = initTextFeld(center.x - 150,
        center.y,
        100,
        20,
        TextFeld::Style::Text | TextFeld::Style::VCenter,
        "SSL Server Port:");
    elements.add(sslPortLabel);
    sslPort = initTextFeld(
        sslPortLabel->getPosition().x + sslPortLabel->getBreite(),
        sslPortLabel->getPosition().y,
        200,
        20,
        TextFeld::Style::TextFeld,
        "");
    sslPort->setTastaturEreignis([this](void* p, void* o, TastaturEreignis te) {
        if (te.id == TE_Release && te.virtualKey == T_Tab)
        {
            sslPort->removeStyle(Zeichnung::Style::Fokus);
            port->addStyle(Zeichnung::Style::Fokus);
        }
        return te.virtualKey != T_Tab && _nurNummernTE(p, o, te);
    });
    elements.add(sslPort);
    TextFeld* portLabel = initTextFeld(center.x - 150,
        center.y + 25,
        100,
        20,
        TextFeld::Style::Text | TextFeld::Style::VCenter,
        "Server Port:");
    elements.add(portLabel);
    port = initTextFeld(portLabel->getPosition().x + portLabel->getBreite(),
        portLabel->getPosition().y,
        200,
        20,
        TextFeld::Style::TextFeld,
        "");
    port->setTastaturEreignis(_nurNummernTE);
    elements.add(port);

    add = initKnopf(
        center.x + 50, center.y + 50, 100, 20, Knopf::Style::Normal, "Add");
    add->setMausEreignis([this](void* p, void* o, MausEreignis me) {
        if (me.id == ME_RLinks)
        {
            if (name->zText()->getLength() && address->zText()->getLength()
                && port->zText()->getLength())
            {
                if (!((ServerSelectionMenu*)(Menu*)menuRegister->get(
                          "serverSelection"))
                         ->hasServer(name->zText()->getText()))
                {
                    hide();
                    menuRegister->get("serverSelection")->show();
                    ((ServerSelectionMenu*)(Menu*)menuRegister->get(
                         "serverSelection"))
                        ->addServer(name->zText()->getText(),
                            address->zText()->getText(),
                            (unsigned short)(int)*sslPort->zText(),
                            (unsigned short)(int)*port->zText());
                    name->zText()->setText("");
                    address->zText()->setText("");
                    port->zText()->setText("");
                    sslPort->zText()->setText("");
                    name->setAlphaFeldFarbe(0xFF3d3d3d);
                    address->setAlphaFeldFarbe(0xFF3d3d3d);
                    port->setAlphaFeldFarbe(0xFF3d3d3d);
                    sslPort->setAlphaFeldFarbe(0xFF3d3d3d);
                }
                else
                    name->setAlphaFeldFarbe(0xFF3d0000);
            }
            else
            {
                if (!name->zText()->getLength())
                    name->setAlphaFeldFarbe(0xFF3d0000);
                if (!address->zText()->getLength())
                    address->setAlphaFeldFarbe(0xFF3d0000);
                if (!port->zText()->getLength())
                    port->setAlphaFeldFarbe(0xFF3d0000);
                if (!sslPort->zText()->getLength())
                    sslPort->setAlphaFeldFarbe(0xFF3d0000);
            }
        }
        return 1;
    });
    elements.add(add);

    abort = initKnopf(
        center.x - 150, center.y + 50, 100, 20, Knopf::Style::Normal, "Abort");
    abort->setMausEreignis([this](void* p, void* o, MausEreignis me) {
        if (me.id == ME_RLinks)
        {
            hide();
            menuRegister->get("serverSelection")->show();
            name->zText()->setText("");
            address->zText()->setText("");
            port->zText()->setText("");
            name->setAlphaFeldFarbe(0xFF3d3d3d);
            address->setAlphaFeldFarbe(0xFF3d3d3d);
            port->setAlphaFeldFarbe(0xFF3d3d3d);
            sslPort->setAlphaFeldFarbe(0xFF3d3d3d);
        }
        return 1;
    });
    elements.add(abort);
}