#include "AddServerMenu.h"

#include "Globals.h"
#include "Initialization.h"
#include "ServerSelection.h"

AddServerMenu::AddServerMenu(Screen* zScreen)
    : Menu(zScreen)
{
    Point center = zScreen->getBackBufferSize() / 2;
    TextField* nameLabel = initTextField(center.x - 150,
        center.y - 50,
        100,
        20,
        TextField::Style::Text | TextField::Style::VCenter,
        "Name:");
    elements.add(nameLabel);
    name = initTextField(nameLabel->getPosition().x + nameLabel->getWidth(),
        nameLabel->getPosition().y,
        200,
        20,
        TextField::Style::TextField,
        "");
    name->setKeyboardEvent([this](void* p, void* o, KeyboardEvent te) {
        if (te.id == TE_Release && te.virtualKey == T_Tab)
        {
            name->removeStyle(Drawable::Style::Focus);
            address->addStyle(Drawable::Style::Focus);
        }
        return te.virtualKey != T_Tab;
    });
    elements.add(name);

    TextField* addressLabel = initTextField(center.x - 150,
        center.y - 25,
        100,
        20,
        TextField::Style::Text | TextField::Style::VCenter,
        "Server Address:");
    elements.add(addressLabel);
    address = initTextField(
        addressLabel->getPosition().x + addressLabel->getWidth(),
        addressLabel->getPosition().y,
        200,
        20,
        TextField::Style::TextField,
        "");
    address->setKeyboardEvent([this](void* p, void* o, KeyboardEvent te) {
        if (te.id == TE_Release && te.virtualKey == T_Tab)
        {
            address->removeStyle(Drawable::Style::Focus);
            sslPort->addStyle(Drawable::Style::Focus);
        }
        return te.virtualKey != T_Tab;
    });
    elements.add(address);

    TextField* sslPortLabel = initTextField(center.x - 150,
        center.y,
        100,
        20,
        TextField::Style::Text | TextField::Style::VCenter,
        "SSL Server Port:");
    elements.add(sslPortLabel);
    sslPort = initTextField(
        sslPortLabel->getPosition().x + sslPortLabel->getWidth(),
        sslPortLabel->getPosition().y,
        200,
        20,
        TextField::Style::TextField,
        "");
    sslPort->setKeyboardEvent([this](void* p, void* o, KeyboardEvent te) {
        if (te.id == TE_Release && te.virtualKey == T_Tab)
        {
            sslPort->removeStyle(Drawable::Style::Focus);
            port->addStyle(Drawable::Style::Focus);
        }
        return te.virtualKey != T_Tab && _onlyNumbersTE(p, o, te);
    });
    elements.add(sslPort);
    TextField* portLabel = initTextField(center.x - 150,
        center.y + 25,
        100,
        20,
        TextField::Style::Text | TextField::Style::VCenter,
        "Server Port:");
    elements.add(portLabel);
    port = initTextField(portLabel->getPosition().x + portLabel->getWidth(),
        portLabel->getPosition().y,
        200,
        20,
        TextField::Style::TextField,
        "");
    port->setKeyboardEvent(_onlyNumbersTE);
    elements.add(port);

    add = initButton(
        center.x + 50, center.y + 50, 100, 20, Button::Style::Normal, "Add");
    add->setMouseEvent([this](void* p, void* o, MouseEvent me) {
        if (me.id == ME_RLeft)
        {
            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->setAlphaFieldColor(0xFF3d3d3d);
                    address->setAlphaFieldColor(0xFF3d3d3d);
                    port->setAlphaFieldColor(0xFF3d3d3d);
                    sslPort->setAlphaFieldColor(0xFF3d3d3d);
                }
                else
                    name->setAlphaFieldColor(0xFF3d0000);
            }
            else
            {
                if (!name->zText()->getLength())
                    name->setAlphaFieldColor(0xFF3d0000);
                if (!address->zText()->getLength())
                    address->setAlphaFieldColor(0xFF3d0000);
                if (!port->zText()->getLength())
                    port->setAlphaFieldColor(0xFF3d0000);
                if (!sslPort->zText()->getLength())
                    sslPort->setAlphaFieldColor(0xFF3d0000);
            }
        }
        return 1;
    });
    elements.add(add);

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