#include "app.h"
#include <algorithm>
#include <string>
#include <regex>
#include <curl/curl.h>
#include <libxml2/libxml/parser.h>
#include <libxml2/libxml/tree.h>
#include <libxml2/libxml/xpath.h>
#include <Magick++.h>
#include <Wt/WMemoryResource.h>
#include <Wt/WContainerWidget.h>
#include <Wt/WTemplate.h>
#include <Wt/WVBoxLayout.h>
#include <Wt/WHBoxLayout.h>
#include <Wt/WGridLayout.h>
#include <Wt/WCssDecorationStyle.h>
#include <Wt/WText.h>
#include <Wt/WPushButton.h>
#include <Wt/WLineEdit.h>
#include <Wt/WMessageBox.h>
#include <Wt/WComboBox.h>
#include <Wt/WEvent.h>
#include <Wt/WAny.h>
#include <Wt/WSpinBox.h>
#include <Wt/Json/Array.h>
#include <Wt/Json/Object.h>
#include <Wt/Json/Serializer.h>
#include <Wt/WLink.h>
#include <Wt/WFileResource.h>
#include <Wt/WTable.h>
#include <Wt/WPopupMenu.h>
#include <Wt/WDialog.h>
#include <Wt/WFileUpload.h>
#include <Wt/Utils.h>
#include <Wt/WSound.h>
#include <Wt/WSlider.h>
#include <Wt/WGridLayout.h>
#include <Wt/WCheckBox.h>

App::App(const Wt::WEnvironment &env, Broadcast &server):
        Wt::WApplication(env),
        env_(env),
        server_(server),
        updateLocationSignal_(this, "updateLocationSignal") {
    initApp();
    updateLocation();
    enableUpdates(true);
    userName = server.userNameForSessionId(sessionId());
    auto verticalContainer = createVerticalLayout();
    createHeadContainer(verticalContainer);
    createMenuContainer(verticalContainer);
    auto horizontalContainer = createActionLayout(verticalContainer);
    createUserListContainer(horizontalContainer);
    createContentContainer(horizontalContainer);
    createImprintContainer(verticalContainer);
    reSetUser();
    if (userName == "") {
        showLogin();
//        Wt::WMessageBox::show("Cookie information", "By technical reasons we set a cookie. This cookie isn't tracking anything or will be saved in a database.", Wt::StandardButton::Ok);
    } else {
        startChat();
    }
    messageReceived_ = std::make_unique<Wt::WSound>("newmessage.mp3");
}

App::~App() {
    server_.disconnect(this);
}

void App::initApp() {
    setTitle("YP Direct Chat");
    setCssTheme("");
    useStyleSheet("style.css");
}

void App::reSetUser() {
    if (env_.cookies().contains("wtd")) {
        auto userData = server_.reSetUser(env_.cookies().find("wtd")->second, sessionId());
        Wt::Json::Object emptyObject = {};
        if (userData != emptyObject) {
            userName = (std::string)userData["username"];
            gender = (std::string)userData["gender"];
            country = (std::string)userData["country"];
            isoCountryCode = (std::string)userData["iso-country-code"];
            age = (int)userData["age"];
        }
    }
}

Wt::WVBoxLayout *App::createVerticalLayout() {
    auto verticalBox = root()->addNew<Wt::WContainerWidget>();
    verticalBox->setHeight(Wt::WLength(100, Wt::LengthUnit::Percentage));
    verticalBox->setWidth(Wt::WLength(100, Wt::LengthUnit::Percentage));
    auto verticalContainer = verticalBox->setLayout(std::make_unique<Wt::WVBoxLayout>());
    verticalContainer->setContentsMargins(0, 0, 0, 0);
    verticalContainer->setSpacing(0);
    return verticalContainer;
}

Wt::WHBoxLayout *App::createActionLayout(Wt::WVBoxLayout *verticalContainer) {
    auto horizontalBox = verticalContainer->addNew<Wt::WContainerWidget>();
    horizontalBox->setStyleClass("horizontal-box");
    auto horizontalContainer = horizontalBox->setLayout(std::make_unique<Wt::WHBoxLayout>());
    horizontalContainer->setContentsMargins(0, 0, 0, 0);
    horizontalContainer->setSpacing(0);
    return horizontalContainer;
}

void App::createHeadContainer(Wt::WVBoxLayout *layout) {
    auto header = layout->addNew<Wt::WContainerWidget>();
    header->addNew<Wt::WText>("<h1>ypChat</h1>");
    header->setStyleClass("header");
}

void App::createMenuContainer(Wt::WVBoxLayout *layout) {
    menuContainer_ = layout->addNew<Wt::WContainerWidget>();
    menuContainer_->setStyleClass("menu");
}

void App::showLogin() {
    contentContainer_->clear();
    createLoginContainer();
}

void App::createLoginContainer() {
    auto loginContainer = contentContainer_->addNew<Wt::WContainerWidget>();
    loginContainer->resize(Wt::WLength(40, Wt::LengthUnit::FontEm), Wt::WLength::Auto);
    auto contentGrid = loginContainer->setLayout(std::make_unique<Wt::WGridLayout>());
    auto userName = addUsernameInput(contentGrid);
    auto gender = addGenderSelection(contentGrid);
    auto age = addAgeInput(contentGrid);
    auto countrySelection = addCountrySelection(contentGrid);
    addStartChatButton(contentGrid, userName, countrySelection, age, gender);
}

Wt::WLineEdit *App::addUsernameInput(Wt::WGridLayout* contentGrid) {
    contentGrid->addWidget(std::make_unique<Wt::WText>("Please type in your nick for the chat: "), 0, 0);
    auto userName = contentGrid->addWidget(std::make_unique<Wt::WLineEdit>(), 0, 1);
    userName->setTextSize(20);
    userName->setMaxLength(30);
    return userName;
}

Wt::WComboBox *App::addGenderSelection(Wt::WGridLayout* contentGrid) {
    contentGrid->addWidget(std::make_unique<Wt::WText>("Gender: "), 1, 0);
    auto genderWidget = contentGrid->addWidget(std::make_unique<Wt::WComboBox>(), 1, 1);
    populateGenderComboBox(genderWidget);
    return genderWidget;
}

Wt::WSpinBox *App::addAgeInput(Wt::WGridLayout* contentGrid) {
    contentGrid->addWidget(std::make_unique<Wt::WText>("Age: "), 2, 0);
    auto ageWidget = contentGrid->addWidget(std::make_unique<Wt::WSpinBox>(), 2, 1);
    ageWidget->setRange(18, 150);
    ageWidget->setValue(18);
    return ageWidget;
}

Wt::WComboBox *App::addCountrySelection(Wt::WGridLayout* contentGrid) {
    contentGrid->addWidget(std::make_unique<Wt::WText>("Country: "), 3, 0);
    auto countryWidget = contentGrid->addWidget(std::make_unique<Wt::WComboBox>(), 3, 1);
    populateCountryComboBox(countryWidget);
    return countryWidget;
}

void App::addStartChatButton(Wt::WGridLayout* contentGrid, Wt::WLineEdit *userName, Wt::WComboBox *country, Wt::WSpinBox *age, Wt::WComboBox *gender) {
    auto doLogin = contentGrid->addWidget(std::make_unique<Wt::WPushButton>("Start chat"), 4, 1);
    doLogin->clicked().connect([=, this]() {
        handleLogin(userName, country, age, gender);
    });
}

void App::populateCountryComboBox(Wt::WComboBox* countryWidget) {
    auto countries = server_.countries();
    int countryIndex{-1};
    auto isoCode = isoCountryCode;
    std::transform(isoCode.begin(), isoCode.end(), isoCode.begin(), ::tolower);
    for (const auto &countryItem: countries) {
        countryWidget->addItem(countryItem.first);
        std::string countryCode = Wt::asString(countryItem.second).toUTF8();
        if (countryCode == isoCode) {
            countryIndex = countryWidget->count();
            break;
        }
    }
    countryWidget->setCurrentIndex(countryIndex);
}

void App::handleLogin(Wt::WLineEdit* userName, Wt::WComboBox* countryWidget, Wt::WSpinBox *ageWidget, Wt::WComboBox *genderWidget) {
    try {
        std::string nick = extractTrimmedUserName(userName);
        validateName(nick);
        validateGender(genderWidget);
        validateAge(ageWidget);

        setUserData(nick, countryWidget, ageWidget, genderWidget);
        connectToServer();
        startChat();
        setCookie("wtd", sessionId(), 21000);
    } catch (const std::exception& e) {
        Wt::WMessageBox::show("Attention", e.what(), Wt::StandardButton::Ok);
    }
}

std::string App::extractTrimmedUserName(Wt::WLineEdit* userName) {
    return userName->text().trim().toUTF8();
}

void App::validateName(const std::string& nick) {
    if (isInvalidName(nick) || isNameAlreadyInUse(nick) || !isNickAllowed(nick)) {
        throw std::runtime_error("The name you wish is already in use or not allowed. Please use another one.");
    }
}

bool App::isInvalidName(const std::string& nick) {
    return nick.empty();
}

bool App::isNameAlreadyInUse(const std::string& nick) {
    return !server_.nameIsFree(nick);
}

void App::validateGender(Wt::WComboBox* genderWidget) {
    if (!isGenderSelected(genderWidget)) {
        throw std::runtime_error("Please select a gender.");
    }
}

bool App::isGenderSelected(Wt::WComboBox* genderWidget) {
    return genderWidget->currentIndex() >= 0;
}

void App::validateAge(Wt::WSpinBox *ageWidget) {
    int ageValue = ageWidget->value();
    if (ageValue < 18 || ageValue > 150) {
        throw std::runtime_error("This age isn't allowed.");
    }
}

void App::setUserData(const std::string& nick, Wt::WComboBox* countryWidget, Wt::WSpinBox *ageWidget, Wt::WComboBox *genderWidget) {
    this->userName = nick;
    country = countryWidget->currentText().toUTF8();
    isoCountryCode = server_.getCountryIsoCodeByCountry(country).toUTF8();
    age = ageWidget->value();
    gender = getGenderShortByGender(genderWidget->currentText().toUTF8());
}

void App::connectToServer() {
    server_.connect(this, std::bind(&App::incomingBroadcast, this));
}

void App::populateGenderComboBox(Wt::WComboBox *genderWidget) {
    int markIndex{-1};
    for (const auto &genderItem: genders_) {
        genderWidget->addItem(genderItem.second);
        if (genderItem.first == "F") {
            markIndex = genderWidget->count();
        }
    }
    genderWidget->setCurrentIndex(markIndex);
}

std::string App::getGenderShortByGender(std::string gender) {
    for (const auto &genderItem: genders_) {
        if (gender == genderItem.second) {
            return genderItem.first.toUTF8();
        }
    }
    return "";
}

void App::updateUserlist(Wt::Json::Array unsortedUserList) {
    userListContainer_->clear();
    userListContainer_->resize(Wt::WLength(15, Wt::LengthUnit::FontEm), Wt::WLength::Auto);
    auto layout = userListContainer_->setLayout(std::make_unique<Wt::WVBoxLayout>());
    layout->setSpacing(1);
    layout->setContentsMargins(0, 0, 0, 0);
    userListContainer_->setOverflow(Wt::Overflow::Auto, Wt::Orientation::Vertical);
    userListContainer_->setOverflow(Wt::Overflow::Hidden, Wt::Orientation::Horizontal);
    auto sortedUserList = sortUserList(unsortedUserList);
    for (Wt::Json::Object &user: sortedUserList) {
        addUserItemToLayout(layout, user);
    }
    layout->addWidget(std::make_unique<Wt::WText>(), 1)->setStyleClass("height-spacer userlist");
    triggerUpdate();
}

std::vector<Wt::Json::Object> App::sortUserList(Wt::Json::Array unsortedUserList) {
    std::vector<Wt::Json::Object> sortedUserList;
    for (const Wt::Json::Object &item : unsortedUserList) {
        sortedUserList.push_back(item);
    }
    std::sort(sortedUserList.begin(), sortedUserList.end(), [this](const Wt::Json::Object& obj1, const Wt::Json::Object& obj2) {
        auto compareResult = compareJsonObjects(obj1, obj2);
        return compareResult;
    });
    return sortedUserList;
}

bool App::compareJsonObjects(const Wt::Json::Object& obj1, const Wt::Json::Object& obj2) {
    auto standardCountry = country;
    auto country1 = (std::string)obj1.get("country");
    auto country2 = (std::string)obj2.get("country");
    auto name1 = (std::string)obj1.get("name");
    auto name2 = (std::string)obj2.get("name");
    if (country1 == standardCountry && country2 == standardCountry) {
        return name1 < name2;
    }
    if (country1 == standardCountry) {
        return true;
    } else if (country2 == standardCountry) {
        return false;
    }
    if (country1 != country2) {
        return country1 < country2;
    }
    return name1 < name2;
}

void App::requestConversation(std::string conversationWith) {
    if (conversationWith == userName) {
        return;
    }
    currentConversationWith_ = conversationWith;
    server_.requestConversation(sessionId(), conversationWith, userName);
}

void App::showConversation(Wt::Json::Object data) {
    try {
        Wt::Json::Object userData = extractUserData(data);
        if (!shouldShowConversation(userData)) {
            return;
        }
        setupConversationUI(userData);
    } catch (const std::exception &e) {
        std::cout << __LINE__ << e.what() << std::endl;
    }
    triggerUpdate();
}

Wt::Json::Object App::extractUserData(Wt::Json::Object data) {
    return (Wt::Json::Object)data["data"];
}

bool App::shouldShowConversation(Wt::Json::Object userData) {
    return currentConversationWith_ == (std::string)userData["name"];
}

void App::setupConversationUI(Wt::Json::Object userData) {
    inboxOpen_ = false;
    searchFields.outputContainer = nullptr;
    contentContainer_->clear();
    auto layout = contentContainer_->setLayout(std::make_unique<Wt::WVBoxLayout>());
    createInfoWidget(layout, userData);
    layout->addWidget(std::make_unique<Wt::WContainerWidget>(), 1)->setOverflow(Wt::Overflow::Auto);
    createInputContainer(layout);
}

Wt::WContainerWidget* App::createInfoWidget(Wt::WVBoxLayout *layout, Wt::Json::Object userData) {
    auto infoWidget = layout->addNew<Wt::WContainerWidget>();
    auto infoLayout = infoWidget->setLayout(std::make_unique<Wt::WHBoxLayout>());
    infoWidget->setStyleClass(Wt::WString("user-conversation-info userlist-gender-{1}").arg((std::string)userData["gender"]));
    infoLayout->addWidget(createInfoText(userData), 1);
    auto blockButton = createBlockButton(userData);
    infoLayout->addWidget(std::move(blockButton));
    return infoWidget;
}

std::unique_ptr<Wt::WText> App::createInfoText(Wt::Json::Object userData) {
    return std::make_unique<Wt::WText>(Wt::WString("{1} ({2}) - {3}, {4}").arg((std::string)userData["name"])
        .arg((int)userData["age"]).arg((std::string)userData["country"]).arg(genders_[(std::string)userData["gender"]]));
}

std::unique_ptr<Wt::WPushButton> App::createBlockButton(Wt::Json::Object userData) {
    auto blockButton = std::make_unique<Wt::WPushButton>((bool)userData["blocked"] ? "Unblock user" : "Block user");
    blockButton->clicked().connect([=, this, &userData]() {
        server_.toggleBlockUser(userName, (std::string)userData["name"], userName);
    });
    return blockButton;
}

Wt::WContainerWidget* App::createInputContainer(Wt::WVBoxLayout* layout) {
    auto inputContainer = layout->addNew<Wt::WContainerWidget>();
    auto inputLayout = inputContainer->setLayout(std::make_unique<Wt::WHBoxLayout>());
    auto inputLine = createInputLine(inputLayout);
    inputLine->setFocus();
    createSendImageButton(inputLayout);
    auto cursorPosition = std::make_shared<int>(0);
    createSmileyButton(inputLayout, inputLine, cursorPosition);
    createSmileyBar(inputContainer, inputLine, cursorPosition);
    createSendButton(inputLayout, inputLine);
    return inputContainer;
}

Wt::WLineEdit* App::createInputLine(Wt::WHBoxLayout* inputLayout) {
    auto inputLine = inputLayout->addWidget(std::make_unique<Wt::WLineEdit>(), 1);
    inputLine->setMaxLength(250);
    auto cursorPosition = std::make_shared<int>(0);
    auto updateCursorPosition = [=]() mutable {
        *cursorPosition = inputLine->cursorPosition();
    };
    inputLine->keyPressed().connect(updateCursorPosition);
    inputLine->clicked().connect(updateCursorPosition);
    return inputLine;
}

Wt::WImage* App::createSendImageButton(Wt::WHBoxLayout* inputLayout) {
    auto sendImageButton = inputLayout->addNew<Wt::WImage>(Wt::WLink("/image.png"));
    sendImageButton->setToolTip("Send an image");
    sendImageButton->clicked().connect([=, this]() {
        auto fileDialog = root()->addNew<Wt::WDialog>("Send Image to User");
        auto layout = fileDialog->contents()->setLayout(std::make_unique<Wt::WVBoxLayout>());
        layout->addNew<Wt::WText>("Please select an immage");
        auto fileWidget = layout->addNew<Wt::WFileUpload>();
        fileWidget->setFilters("image/*");
        auto image = layout->addNew<Wt::WImage>();
        image->setMaximumSize(Wt::WLength(100, Wt::LengthUnit::Pixel), Wt::WLength(100, Wt::LengthUnit::Pixel));
        image->setHeight(Wt::WLength::Auto);
        auto localImage = std::make_shared<Magick::Image>();
        auto buttonsContainer = layout->addNew<Wt::WContainerWidget>();
        auto okButton = buttonsContainer->addNew<Wt::WPushButton>("Send image");
        fileWidget->uploaded().connect([=, this]() mutable {
            try {
                Magick::Image originalImage;
                originalImage.quiet(true);
                originalImage.read(fileWidget->spoolFileName());
                *localImage = scaleImage(originalImage, 500);
                auto smallImage = scaleImage(originalImage, 100);
                Magick::Blob previewBlob;
                smallImage.write(&previewBlob);
                auto previewBase64 = previewBlob.base64();
                auto previewImage = Wt::Utils::base64Decode(previewBase64);
                auto imageResource = std::make_shared<Wt::WMemoryResource>(
                    "image/png",
                    std::vector<unsigned char>(previewImage.begin(), previewImage.end()));
                image->setImageLink(Wt::WLink(imageResource));
                image->resize(Wt::WLength(smallImage.columns(), Wt::LengthUnit::Pixel), Wt::WLength(smallImage.rows(), Wt::LengthUnit::Pixel));
                okButton->setEnabled(true);
                triggerUpdate();
            } catch (const std::exception& e) {
                std::cerr << "Error processing uploaded image: " << e.what() << std::endl;
            }
        });
        fileWidget->changed().connect([=]() { fileWidget->upload(); });
        fileWidget->fileTooLarge().connect([](){ std::cout << "file too big" << std::endl; });
        okButton->setDisabled(true);
        okButton->clicked().connect([=, this]() {
            server_.addImage(sessionId(), currentConversationWith_, localImage);
            fileDialog->accept();
        });
        auto cancelButton = buttonsContainer->addNew<Wt::WPushButton>("Cancel");
        cancelButton->clicked().connect([=](){ fileDialog->reject(); });
        fileDialog->setClosable(true);
        fileDialog->setModal(true);
        fileDialog->show();
    });
    return sendImageButton;
}

Magick::Image App::scaleImage(const Magick::Image& originalImage, int maxSize) const {
    int scale = 100;
    int maxDimension = std::max(originalImage.size().width(), originalImage.size().height());
    if (maxDimension > maxSize) {
        scale = (maxSize * 100) / maxDimension;
    }
    scale = (scale < 100) ? scale : 100;
    Magick::Image scaledImage(originalImage);
    scaledImage.scale(Magick::Geometry(originalImage.size().width() * scale / 100, originalImage.size().height() * scale / 100));
    return scaledImage;
}

Wt::WContainerWidget* App::createSmileyButton(Wt::WHBoxLayout* inputLayout, Wt::WLineEdit* inputLine, std::shared_ptr<int> cursorPosition) {
    auto smileyButton = inputLayout->addNew<Wt::WContainerWidget>();
    smileyButton->addNew<Wt::WImage>(Wt::WLink("/smileys.png"));
    smileyButton->setStyleClass("no-style");
    smileyButton->setToolTip("Add a smiley");
    smileyButton->setInline(true);
    auto smileyBar = createSmileyBar(smileyButton, inputLine, cursorPosition);
    smileyBar->setHidden(true);
    smileyButton->clicked().connect([=]() {
        smileyBar->setHidden(!smileyBar->isHidden());
    });
    return smileyButton;
}

Wt::WContainerWidget* App::createSmileyBar(Wt::WContainerWidget* parent, Wt::WLineEdit* inputLine, std::shared_ptr<int> cursorPosition) {
    auto smileyBar = parent->addWidget(createSmileysBar(inputLine, cursorPosition));
    smileyBar->setHidden(true);
    return smileyBar;
}

Wt::WPushButton* App::createSendButton(Wt::WHBoxLayout* inputLayout, Wt::WLineEdit* inputLine) {
    auto sendButton = inputLayout->addNew<Wt::WPushButton>("Send");
    auto sendMessageFunction = [=, this]() {
        sendMessage(inputLine);
    };
    inputLine->enterPressed().connect(sendMessageFunction);
    sendButton->clicked().connect(sendMessageFunction);
    inputLine->setFocus();
    return sendButton;
}

void App::sendMessage(Wt::WLineEdit *inputLine) {
    auto utf8String = inputLine->valueText().toUTF8();
    size_t pos = 0;
    while ((pos = utf8String.find("<", pos)) != std::string::npos) {
        utf8String.replace(pos, 1, "&lt;");
        pos += 4;
    }
    inputLine->setValueText("");
    auto sendString = Wt::WString(utf8String.substr(0, 250));
    server_.addMessage(sessionId(), currentConversationWith_, Broadcast::Message(sessionId(), sendString));
}

void App::renderConversation(Wt::Json::Object conversation) {
    if (currentConversationWith_ != (std::string)conversation["user1"] && currentConversationWith_ != (std::string)conversation["user2"]) {
        return;
    }
    updateOutputContainer(conversation);
    server_.setConversationRead(sessionId(), currentConversationWith_);
    triggerUpdate();
}

void App::updateOutputContainer(Wt::Json::Object conversation) {
    auto containerLayout = (Wt::WVBoxLayout*)contentContainer_->layout();
    auto outputLayoutItem = containerLayout->itemAt(1);
    auto outputContainer = (Wt::WContainerWidget*)outputLayoutItem->widget();
    outputContainer->clear();
    outputContainer->setOverflow(Wt::Overflow::Auto);
    renderChatLines(conversation, outputContainer);
}

void App::renderChatLines(Wt::Json::Object conversation, Wt::WContainerWidget* outputContainer) {
    Wt::Json::Array chat = conversation["data"];
    for (Wt::Json::Object &line : chat) {
        try {
            renderChatLine(line, conversation, outputContainer);
        } catch (const std::exception &e) {
            std::cout << e.what() << std::endl;
        }
    }
}

void App::renderChatLine(Wt::Json::Object &line, Wt::Json::Object conversation, Wt::WContainerWidget* outputContainer) {
    std::string writer = getChatLineWriter(line, conversation);
    Wt::WWebWidget* item;
    if ((std::string)line["type"] == "text") {
        item = createTextElement(writer, (std::string)line["string"], outputContainer);
    } else if ((std::string)line["type"] == "image") {
        item = createImageElement(line, writer, outputContainer);
    } else {
        item = new Wt::WText("");
    }
    item->setInline(false);
}

Wt::WWebWidget* App::createTextElement(const std::string& writer, const std::string& text, Wt::WContainerWidget* outputContainer) {
    std::string outputText = replaceSmileys(text);
    Wt::WString output = Wt::WString("<b>{1}</b>: {2}").arg(writer).arg(outputText);
    return outputContainer->addNew<Wt::WText>(output);
}

void App::createImprintContainer(Wt::WVBoxLayout *containerLayout) {
    auto imprintContainer = containerLayout->addNew<Wt::WContainerWidget>();
    imprintContainer->setContentAlignment(Wt::AlignmentFlag::Right);
    imprintContainer->setPadding(Wt::WLength(0.5, Wt::LengthUnit::FontEm));
    auto imprintButton = imprintContainer->addNew<Wt::WText>("Imprint");
    Wt::WCssDecorationStyle imprintButtonDecorationStyle;
    imprintButtonDecorationStyle.setForegroundColor(Wt::StandardColor::White);
    imprintButtonDecorationStyle.setBackgroundColor(Wt::WColor(0x42, 0x90, 0x43));
    imprintButtonDecorationStyle.setCursor(Wt::Cursor::PointingHand);
    imprintContainer->setDecorationStyle(imprintButtonDecorationStyle);
    imprintButton->clicked().connect([=, this]() {
        auto imprintDialog = root()->addNew<Wt::WMessageBox>("Imprint", "", Wt::Icon::None, Wt::StandardButton::Ok);
        imprintDialog->contents()->addNew<Wt::WText>("<h1>Imprint</h1>");
        imprintDialog->contents()->addNew<Wt::WText>("<p>Information according to § 5 TMG</p>");
        imprintDialog->contents()->addNew<Wt::WText>("<p>Torsten Schulz <br> "
                                                     "Friedrich-Stampfer-Str. 21<br>  "
                                                     "60437 Frankfurt <br>  "
                                                     "</p>", Wt::TextFormat::UnsafeXHTML);
        imprintDialog->contents()->addNew<Wt::WText>("<p> <strong>Represented by: </strong><br> "
                                                     "Torsten Schulz<br> "
                                                     "</p>", Wt::TextFormat::UnsafeXHTML);
        imprintDialog->contents()->addNew<Wt::WText>("<p><strong>Contact:</strong> <br> "
                                                     "Phone: 069-95 64 17 10<br> "
                                                     "Email: <a href='mailto:tsschulz@tsschulz.de'>tsschulz@tsschulz.de</a></br> "
                                                     "</p>", Wt::TextFormat::UnsafeXHTML);
        imprintDialog->contents()->addNew<Wt::WText>("<p><strong>Disclaimer: </strong><br><br><strong>Liability for Contents</strong><br><br> "
                                                     "The contents of our pages were created with the greatest care. However, we cannot guarantee the correctness, completeness, and topicality of the contents. As a service provider, we are responsible for our own content on these pages in accordance with § 7 para.1 TMG (German Telemedia Act) and general laws. According to §§ 8 to 10 TMG, however, we are not obliged as service providers to monitor transmitted or stored third-party information or to investigate circumstances that indicate illegal activity. Obligations to remove or block the use of information under general law remain unaffected. However, liability in this regard is only possible from the time of knowledge of a concrete infringement. If we become aware of any such legal infringements, we will remove the content immediately.<br><br><strong>Liability for Links</strong><br><br> "
                                                     "Our offer contains links to external websites of third parties, on whose contents we have no influence. Therefore, we cannot assume any liability for these external contents. The respective provider or operator of the pages is always responsible for the contents of the linked pages. The linked pages were checked for possible legal violations at the time of linking. Illegal contents were not recognizable at the time of linking. However, permanent monitoring of the content of the linked pages is not reasonable without concrete evidence of a violation of the law. If we become aware of any infringements, we will remove such links immediately.<br><br><strong>Data Protection</strong><br><br> "
                                                     "The use of our website is usually possible without providing personal data. As far as personal data (e.g., name, address, or email addresses) is collected on our website, this is always done on a voluntary basis as far as possible. This data will not be passed on to third parties without your express consent. <br> "
                                                     "We would like to point out that data transmission over the Internet (e.g., communication by email) can have security gaps. A complete protection of data against access by third parties is not possible. <br> "
                                                     "The use of contact data published within the scope of the imprint obligation by third parties for sending unsolicited advertising and information materials is hereby expressly prohibited. The operators of these pages expressly reserve the right to take legal action in the event of unsolicited sending of advertising information, such as spam emails.<br> "
                                                     "</p>", Wt::TextFormat::UnsafeXHTML);
        imprintDialog->contents()->addNew<Wt::WText>("<br>  "
                                                     "Imprint from <a href=\"https://www.impressum-generator.de\">Imprint Generator</a> of <a href=\"https://www.kanzlei-hasselbach.de/\">Kanzlei Hasselbach, Lawyers for Labor Law and Family Law</a> ", Wt::TextFormat::UnsafeXHTML);
        imprintDialog->contents()->setMaximumSize(Wt::WLength(60, Wt::LengthUnit::FontEm), Wt::WLength(40, Wt::LengthUnit::FontEm));
        imprintDialog->contents()->setOverflow(Wt::Overflow::Auto);
        imprintDialog->buttonClicked().connect([=]() { imprintDialog->accept(); });
        imprintDialog->show();
    });
}

Wt::WWebWidget* App::createImageElement(Wt::Json::Object& line, const std::string& writer, Wt::WContainerWidget* outputContainer) {
    Wt::Json::Object imageDescription = line["image"];
    auto imageLineItem = outputContainer->addNew<Wt::WContainerWidget>();
    auto outputText = Wt::WString("<b>{1}:</b>&nbsp;").arg(writer);
    imageLineItem->addNew<Wt::WText>(outputText)->setStyleClass("output-line");
    auto image = imageLineItem->addNew<Wt::WImage>();
    auto imageBlob = Wt::Utils::base64Decode((std::string)imageDescription["imageblobbase64"]);
    auto imageResource = std::make_shared<Wt::WMemoryResource>(
        "image/png",
        std::vector<unsigned char>(imageBlob.begin(), imageBlob.end()));
    image->setImageLink(Wt::WLink(imageResource));
    image->resize(Wt::WLength((int)imageDescription["width"], Wt::LengthUnit::Pixel), Wt::WLength((int)imageDescription["height"], Wt::LengthUnit::Pixel));
    return imageLineItem;
}

std::string App::getChatLineWriter(Wt::Json::Object &line, Wt::Json::Object conversation) {
    std::string sender = (std::string)line["sender"];
    if (sender == sessionId()) {
        return "you";
    } else if (sender == (std::string)conversation["sessionid1"]) {
        return (std::string)conversation["user1"];
    } else {
        return (std::string)conversation["user2"];
    }
}

std::string App::replaceSmileys(std::string outputText) {
    for (const auto &smiley : smileys_) {
        std::size_t pos = outputText.find(smiley.first);
        while (pos != std::string::npos) {
            outputText.replace(pos, smiley.first.length(), "&#x" + smiley.second.code + ";");
            pos = outputText.find(smiley.first, pos + smiley.second.code.length());
        }
    }
    return outputText;
}

void App::showUnreadMessages(Wt::Json::Object data) {
    Wt::WString buttonText = (int)data["data"] == 0 ? "Inbox" : Wt::WString("Inbox ({1})").arg((int)data["data"]);
    auto currentText = inbox_->text();
    inbox_->setText(buttonText);
    std::string pattern{"Inbox\\(\\s*(\\d+)\\)"};
    std::regex regex(pattern);
    std::string text = currentText.toUTF8();
    std::smatch match;
    auto doPlay{false};
    if (std::regex_search(text, match, regex)) {
        std::string numberStr = match[2].str();
        auto oldValue = std::stoi(numberStr);
        if (oldValue < (int)data["data"] && (int)data["data"] > 0) {
            doPlay = true;
        }
    } else if (text == "Inbox" && buttonText != "Inbox") {
        doPlay = true;
    }
    if (doPlay) {
        messageReceived_->play();
    }
    triggerUpdate();
    if (inboxOpen_) {
        server_.sendOpenConversations(sessionId());
    }
}

void App::showOpenInbox(Wt::Json::Object data) {
    searchFields.outputContainer = nullptr;
    contentContainer_->clear();
    contentContainer_->setPadding(Wt::WLength(1, Wt::LengthUnit::FontEm), Wt::Side::Top | Wt::Side::Bottom);
    contentContainer_->setPadding(Wt::WLength(2, Wt::LengthUnit::FontEm), Wt::Side::Left | Wt::Side::Right);
    contentContainer_->setOverflow(Wt::Overflow::Auto);
    auto headline = contentContainer_->addNew<Wt::WText>("<h2>Inbox</h2>");
    headline->setInline(false);
    auto conversationsTable = contentContainer_->addNew<Wt::WTable>();
    conversationsTable->resize(Wt::WLength(15, Wt::LengthUnit::FontEm), Wt::WLength::Auto);
    for (Wt::Json::Object &user: (Wt::Json::Array)data["data"]) {
        Wt::WString partnerName = (std::string)user["name"];
        auto userItem = conversationsTable->elementAt(conversationsTable->rowCount(), 0)->addNew<Wt::WText>(Wt::WString("{1} ({2})").arg(partnerName).arg((int)user["age"]));
        userItem->setStyleClass(Wt::WString("userlist-item userlist-gender-{1}").arg((std::string)user["gender"]));
        userItem->setHeight(Wt::WLength(2, Wt::LengthUnit::FontEm));
        userItem->setPadding(Wt::WLength(3, Wt::LengthUnit::Pixel));
        userItem->setInline(false);
        userItem->clicked().connect([=, this]() {
            requestConversation(partnerName.toUTF8());
        });
    }
    triggerUpdate();
}

void App::updateUserinfo(Wt::Json::Object data) {
    Wt::Json::Object userData = data["data"];
    if ((std::string)userData["name"] != currentConversationWith_) {
        return;
    }
    auto containerLayout = (Wt::WVBoxLayout*)contentContainer_->layout();
    auto infoLayoutItem = containerLayout->itemAt(0);
    auto infoWidget = (Wt::WContainerWidget*)(infoLayoutItem->widget());
    auto infoWidgetLayout = (Wt::WHBoxLayout*)infoWidget->layout();
    auto infoTextWidget = dynamic_cast<Wt::WText*>(infoWidgetLayout->itemAt(0)->widget());
    auto blockButton = dynamic_cast<Wt::WPushButton*>(infoWidgetLayout->itemAt(1)->widget());
    try {
        infoWidget->setStyleClass(Wt::WString("user-conversation-info userlist-gender-{1}").arg((std::string)userData["gender"]));
        if (infoTextWidget) {
            infoTextWidget->setText(Wt::WString("{1} ({2}) - {3}, {4}").arg((std::string)userData["name"])
                .arg((int)userData["age"]).arg((std::string)userData["country"]).arg(genders_[(std::string)userData["gender"]]));
        }
        blockButton->setText((bool)userData["blocked"] ? "Unblock" : "Block");
    } catch (const std::exception &e) {
        std::cout << e.what() << std::endl << Wt::Json::serialize(userData) << std::endl;
    }
    triggerUpdate();
}

std::unique_ptr<Wt::WContainerWidget> App::createSmileysBar(Wt::WLineEdit *inputLine, std::shared_ptr<int> cursorPosition) {
    auto smileyBar = std::make_unique<Wt::WContainerWidget>();
    smileyBar->setHidden(true);
    for (auto &smiley: smileys_) {
        auto code = smileyPlaceholder_;
        code = code.arg(smiley.second.code);
        auto item = smileyBar->addNew<Wt::WText>(code, Wt::TextFormat::UnsafeXHTML);
        item->setToolTip(smiley.second.tooltip);
        item->clicked().connect([=]() {
            auto currentText = inputLine->text().toUTF8();
            currentText.insert(*cursorPosition, smiley.first);
            inputLine->setText(currentText);
            inputLine->setFocus();
            *cursorPosition += currentText.length() - 1;
        });
    }
    smileyBar->setStyleClass("smiley-bar");
    return smileyBar;
}

void App::toggleSmileysBar(Wt::WContainerWidget *smileyBar) {
    smileyBar->setHidden(!smileyBar->isHidden());
}

void App::showSystemMessage(Wt::Json::Object broadcast) {
    if ((std::string)broadcast["relatedUser"] != currentConversationWith_) {
        return;
    }
    auto containerLayout = (Wt::WVBoxLayout*)contentContainer_->layout();
    auto outputLayoutItem = containerLayout->itemAt(1);
    auto outputContainer = (Wt::WContainerWidget*)outputLayoutItem->widget();
    auto outputLine = outputContainer->addNew<Wt::WText>((std::string)broadcast["data"]);
    outputLine->setStyleClass("system-message");
}


void App::createMenu() {
    menuContainer_->clear();
    menuContainer_->setStyleClass("menu");
    addLeaveButton();
    addIdentifier();
    addSearchButton();
    addInboxButton();
    addHistoryButton();
}

void App::addLeaveButton() {
    auto leaveButton = menuContainer_->addNew<Wt::WPushButton>("Leave");
    leaveButton->clicked().connect(this, &App::logout);
}

void App::logout() {
    server_.disconnect(this);
    userListContainer_->clear();
    menuContainer_->clear();
    showLogin();
    inboxOpen_ = false;
    searchFields.outputContainer = nullptr;
}

void App::addHistoryButton() {
    auto history = menuContainer_->addNew<Wt::WPushButton>("History");
    history->clicked().connect(this, &App::requestHistory);
}

void App::addIdentifier() {
    auto identifier = menuContainer_->addNew<Wt::WText>(Wt::WString("{1} ({2}), {3}").arg(userName).arg(isoCountryCode).arg(age));
    identifier->setMargin(Wt::WLength(0.3, Wt::LengthUnit::FontEm), Wt::Side::Bottom | Wt::Side::Top);
    Wt::WCssDecorationStyle backgroundStyle;
    backgroundStyle.setBackgroundColor(Wt::StandardColor::White);
    identifier->setStyleClass("inline-block");
    identifier->setPadding(Wt::WLength(0.3, Wt::LengthUnit::FontEm), Wt::Side::Left | Wt::Side::Right);
    identifier->setDecorationStyle(backgroundStyle);
    identifier->setWidth(Wt::WLength(25, Wt::LengthUnit::FontEm));
}

void App::addSearchButton() {
    auto searchButton = menuContainer_->addNew<Wt::WPushButton>("Search");
    searchButton->clicked().connect(this, &App::showSearchWindow);
}

void App::addInboxButton() {
    inbox_ = menuContainer_->addNew<Wt::WPushButton>("Inbox");
    inbox_->clicked().connect(this, &App::openInbox);
}

void App::showSearchWindow() {
    auto contentLayout = resetSearchFields();
    auto userNameField = setupNameSearchField(contentLayout);
    auto ageSearchFields = setupSearchFields(contentLayout);
    auto countryFields = setupCountryDropDown(contentLayout);
    auto gendersFields = setupGendersDropDown(contentLayout);
    searchFields.outputContainer = setupSearchButton(contentLayout);
    restoreSearchFields(searchFields.outputContainer, userNameField, ageSearchFields.first, ageSearchFields.second,
                        countryFields.second, gendersFields.second, countryFields.first, gendersFields.first);
}

Wt::WVBoxLayout *App::resetSearchFields() {
    currentConversationWith_ = "";
    contentContainer_->clear();
    inboxOpen_ = false;
    auto contentLayout = contentContainer_->setLayout(std::make_unique<Wt::WVBoxLayout>());
    contentLayout->addNew<Wt::WText>("<h2>Search</h2>");
    return contentLayout;
}

std::pair<Wt::WSpinBox*, Wt::WSpinBox*> App::setupSearchFields(Wt::WVBoxLayout *contentLayout) {
    auto minAgeEdit = addSearchItemLine<Wt::WSpinBox>(contentLayout, "From age");
    minAgeEdit->setRange(18, 150);
    minAgeEdit->setValue(18);
    minAgeEdit->changed().connect([=, this] { searchFields.minAge = minAgeEdit->value(); });
    auto maxAgeEdit = addSearchItemLine<Wt::WSpinBox>(contentLayout, "To age");
    maxAgeEdit->setRange(18, 150);
    maxAgeEdit->setValue(150);
    maxAgeEdit->changed().connect([=, this] { searchFields.maxAge = maxAgeEdit->value(); });
    return {minAgeEdit, maxAgeEdit};
}

std::pair<Wt::WPushButton*, Wt::WContainerWidget*> App::setupCountryDropDown(Wt::WVBoxLayout *contentLayout) {
    auto countryDropDown = std::make_unique<Wt::WContainerWidget>();
    countryDropDown->setInline(true);
    countryDropDown->setStyleClass("countries-drop-down");
    auto countryDropDownContainer = countryDropDown.get();
    auto countryOpenList = addSearchItemLine<Wt::WPushButton>(contentLayout, "Country", std::move(countryDropDown));
    countryDropDownContainer->hide();
    countryOpenList->clicked().connect([=]() { if (countryDropDownContainer->isHidden()) { countryDropDownContainer->show();} else { countryDropDownContainer->hide(); } });
    countryOpenList->setText("All");
    std::map<Wt::WString, Wt::WString> countries = server_.countries();
    addItem("All", countryDropDownContainer, countryOpenList, &searchFields.countries, true);
    addItem(country, countryDropDownContainer, countryOpenList, &searchFields.countries);
    for (const auto &itemCountry: countries) {
        if (itemCountry.first.toUTF8() != country) {
            addItem(itemCountry.first.toUTF8(), countryDropDownContainer, countryOpenList, &searchFields.countries);
        }
    }
    return {countryOpenList, countryDropDownContainer};
}

std::pair<Wt::WPushButton*, Wt::WContainerWidget*> App::setupGendersDropDown(Wt::WVBoxLayout *contentLayout) {
    auto gendersDropDown = std::make_unique<Wt::WContainerWidget>();
    gendersDropDown->setInline(true);
    gendersDropDown->setStyleClass("countries-drop-down");
    auto gendersDropDownContainer = gendersDropDown.get();
    auto gendersOpenList = addSearchItemLine<Wt::WPushButton>(contentLayout, "Genders", std::move(gendersDropDown));
    gendersDropDownContainer->hide();
    gendersOpenList->clicked().connect([=]() { if (gendersDropDownContainer->isHidden()) { gendersDropDownContainer->show();} else { gendersDropDownContainer->hide(); } });
    gendersOpenList->setText("All");
    addItem("All", gendersDropDownContainer, gendersOpenList, &searchFields.gender, true);
    std::map<Wt::WString, Wt::WString> swappedGenders;
    for (const auto& pair : genders_) {
        swappedGenders[pair.second] = pair.first;
    }
    for (const auto &itemGender: swappedGenders) {
        addItem(itemGender.first.toUTF8(), gendersDropDownContainer, gendersOpenList, &searchFields.gender);
    }
    return {gendersOpenList, gendersDropDownContainer};
}

void App::addItem(const std::string& country, Wt::WContainerWidget *dropDownContainer, Wt::WPushButton *openListButton, std::unordered_set<std::string> *saveItems, bool isSelected) {
    auto menuItem = dropDownContainer->addNew<Wt::WCheckBox>(country);
    menuItem->changed().connect([=, this]() mutable { itemChanged(menuItem, dropDownContainer, openListButton, saveItems); });
    menuItem->setInline(false);
    if (isSelected) {
        menuItem->setChecked();
    }
}

void App::addUserItemToLayout(Wt::WVBoxLayout *layout, Wt::Json::Object userObject) {
    auto userName = (std::string)userObject["name"];
    auto userItem = layout->addNew<Wt::WText>(Wt::WString("{1} ({2})").arg(userName).arg((int)userObject["age"]));
    userItem->setStyleClass(Wt::WString("userlist-item userlist-gender-{1}").arg((std::string)userObject["gender"]));
    userItem->setHeight(Wt::WLength(2, Wt::LengthUnit::FontEm));
    userItem->setPadding(Wt::WLength(3, Wt::LengthUnit::Pixel));
    userItem->clicked().connect([=, this]() {
        requestConversation(userName);
    });

}

std::unordered_set<std::string> App::gendersListToShortGendersList(std::unordered_set<std::string> gendersList) {
    std::unordered_set<std::string> result;
    for (const auto &gender: gendersList) {
        result.insert(genderShortOfGender(gender));
    }
    return result;
}

std::string App::genderShortOfGender(const std::string incomingGender) {
    for (const auto &genderItem: genders_) {
        if (incomingGender == genderItem.second) {
            return genderItem.first.toUTF8();
        }
    }
    return incomingGender;
}

void App::extendSearchResultIfNeeded(Wt::Json::Object broadcast) {
    if (searchFields.outputContainer == nullptr) {
        return;
    }
    auto user = (Wt::Json::Object)broadcast["data"];
    auto age = (int)user["age"];
    auto country = (std::string)user["country"];
    auto gender = (std::string)user["gender"];
    if (
            (searchFields.userName.toUTF8() == "" || ((std::string)user["name"]).find(searchFields.userName.toUTF8()) != std::string::npos)
            && (searchFields.minAge <= age)
            && (searchFields.maxAge >= age)
            && (searchFields.countries.contains("All") || searchFields.countries.contains(country) || searchFields.countries.size() == 0)
            && (searchFields.gender.contains("All") || searchFields.gender.contains(gender) || searchFields.gender.size() == 0)
    ) {
        startSearch();
    }
}

void App::removeUserFromSearch(Wt::Json::Object) {
    if (searchFields.outputContainer == nullptr) {
        return;
    }
    startSearch();
}

void App::requestHistory() {
    server_.sendHistory(sessionId());
}

void App::showHistory(Wt::Json::Object broadcast) {
    try {
        contentContainer_->clear();
        auto headerWidget = contentContainer_->addNew<Wt::WText>("<h2>Conversations with already logged in users</h2>");
        headerWidget->setInline(false);
        auto listWidget = contentContainer_->addNew<Wt::WTable>();
        for (Wt::Json::Object user: (Wt::Json::Array)broadcast["data"]) {
            auto userName = std::make_shared<std::string>((std::string)user["name"]);
            auto tableCell = listWidget->elementAt(listWidget->rowCount(), 0);
            tableCell->addNew<Wt::WText>(Wt::WString("{1} ({2})").arg(*userName).arg((int)user["age"]));
            tableCell->setStyleClass(Wt::WString("user-conversation-info userlist-item userlist-gender-{1}").arg((std::string)user["gender"]));
            tableCell->clicked().connect([=, this]() {
                requestConversation(*userName);
            });
        }
    } catch(std::exception &e) {
        std::cout << e.what() << std::endl;
    }

    triggerUpdate();
}

void App::itemChanged(Wt::WCheckBox *item, Wt::WContainerWidget *dropDownContainer, Wt::WPushButton *openButton, std::unordered_set<std::string> *saveItems) {
    saveItems->clear();
    bool unselect = (item->text() == "All" && item->isChecked());
    for (auto &widgetItem: dropDownContainer->children()) {
        auto widgetCheckBox = static_cast<Wt::WCheckBox*>(widgetItem);
        if (unselect && widgetCheckBox->text() != "All" && widgetCheckBox->isChecked()) {
            widgetCheckBox->setChecked(false);
        }
        if (widgetCheckBox->isChecked()) {
            saveItems->insert(widgetCheckBox->text().toUTF8());
        }
    }
    if (saveItems->size() > 1 && saveItems->find("All") != saveItems->end()) {
        saveItems->erase(saveItems->find("All"));
        static_cast<Wt::WCheckBox*>(dropDownContainer->children().at(0))->setChecked(false);
    }
    if (saveItems->empty()) {
        saveItems->insert("All");
    }
    std::string result;
    for (const auto &selected: *saveItems) {
        result += (result.empty() ? "" : ", ") + selected;
    }
    openButton->setText(result);
}

void App::restoreSearchFields(Wt::WContainerWidget *searchResultContainer, Wt::WLineEdit *userNameEdit, Wt::WSpinBox *minAgeEdit,
                              Wt::WSpinBox *maxAgeEdit, Wt::WContainerWidget *countryDropDownContainer, Wt::WContainerWidget *gendersDropDownContainer,
                              Wt::WPushButton *countryOpenList, Wt::WPushButton *gendersOpenList) {
    if (!searchFields.set) {
        searchFields = Search(searchResultContainer);
    } else {
        userNameEdit->setValueText(searchFields.userName);
        minAgeEdit->setValue(searchFields.minAge);
        maxAgeEdit->setValue(searchFields.maxAge);
        for (auto countryWidget: countryDropDownContainer->children()) {
            auto countryCheckBox = (Wt::WCheckBox*)countryWidget;
            countryCheckBox->setChecked(searchFields.countries.find(countryCheckBox->text().toUTF8()) != searchFields.countries.end());
        }
        itemChanged((Wt::WCheckBox*)*countryDropDownContainer->children().begin(), countryDropDownContainer, countryOpenList, &searchFields.countries);
        for (auto genderWidget: gendersDropDownContainer->children()) {
            auto genderCheckBox = (Wt::WCheckBox*)genderWidget;
            genderCheckBox->setChecked(searchFields.gender.find(genderCheckBox->text().toUTF8()) != searchFields.gender.end());
        }
        itemChanged((Wt::WCheckBox*)*gendersDropDownContainer->children().begin(), gendersDropDownContainer, gendersOpenList, &searchFields.gender);
        startSearch();
    }
}

Wt::WLineEdit *App::setupNameSearchField(Wt::WVBoxLayout *contentLayout) {
    auto userNameEdit = addSearchItemLine<Wt::WLineEdit>(contentLayout, "Username includes");
    userNameEdit->changed().connect([=, this] { searchFields.userName = userNameEdit->text().trim(); });
    return userNameEdit;
}

Wt::WContainerWidget *App::setupSearchButton(Wt::WVBoxLayout *contentLayout) {
    auto searchButton = addSearchItemLine<Wt::WPushButton>(contentLayout, "");
    searchButton->setText("Search");
    auto searchResultContainer = contentLayout->addWidget(std::make_unique<Wt::WContainerWidget>(), 1);
    searchResultContainer->addNew<Wt::WText>("No results");
    searchButton->clicked().connect(this, &App::startSearch);
    return searchResultContainer;
}

void App::startSearch() {
    if (searchFields.minAge > searchFields.maxAge) {
        searchFields.outputContainer->clear();
        searchFields.outputContainer->addNew<Wt::WText>("Minimum age must be at least as large as or greater than the maximum age.");
        return;
    }
    server_.userSearch(sessionId(), searchFields.userName.toUTF8(), searchFields.minAge, searchFields.maxAge,
                       searchFields.countries, gendersListToShortGendersList(searchFields.gender), userName);
}

void App::showSearch(Wt::Json::Object broadcast) {
    if (!searchFields.outputContainer) {
        return;
    }
    searchFields.outputContainer->clear();
    auto searchResult = (Wt::Json::Array)broadcast["data"];
    if (searchResult.size() == 0) {
        searchFields.outputContainer->addNew<Wt::WText>("No results.");
    }
    auto searchListContainer = searchFields.outputContainer->addNew<Wt::WContainerWidget>();
    auto searchList = searchListContainer->setLayout(std::make_unique<Wt::WVBoxLayout>());
    searchListContainer->setOverflow(Wt::Overflow::Auto);
    for (const Wt::Json::Object &searchItem: searchResult) {
        addUserItemToLayout(searchList, searchItem);
    }
    triggerUpdate();
}

void App::openInbox() {
    currentConversationWith_ = "";
    contentContainer_->clear();
    contentContainer_->addNew<Wt::WText>("<h2>Inbox</h2>");
    inboxOpen_ = true;
    searchFields.outputContainer = nullptr;
    server_.sendOpenConversations(sessionId());
}

void App::incomingBroadcast() {
    auto broadcasts = server_.getBroadcastsForSession(sessionId());
    for (Wt::Json::Object &broadcast: broadcasts) {
        if (broadcast["type"] == "userlist") {
            updateUserlist(broadcast["data"]);
        } else if (broadcast["type"] == "logout") {
            server_.disconnect(this);
        } else if (broadcast["type"] == "messagequeue") {
            renderConversation(broadcast);
        } else if (broadcast["type"] == "unread-chats") {
            showUnreadMessages(broadcast);
        } else if (broadcast["type"] == "openconversations" && inboxOpen_) {
            showOpenInbox(broadcast);
        } else if (broadcast["type"] == "userinfo") {
            updateUserinfo(broadcast);
        } else if (broadcast["type"] == "system") {
            showSystemMessage(broadcast);
        } else if (broadcast["type"] == "conversation-start") {
            showConversation(broadcast);
        } else if (broadcast["type"] == "search-result") {
            showSearch(broadcast);
        } else if (broadcast["type"] == "newuser") {
            extendSearchResultIfNeeded(broadcast);
        } else if (broadcast["type"] == "userleft") {
            removeUserFromSearch(broadcast);
        } else if (broadcast["type"] == "history") {
            showHistory(broadcast);
        }
    }
}

void App::startChat() {
    createMenu();
    contentContainer_->clear();
    contentContainer_->addNew<Wt::WText>("A warm welcome. We wish you lots of fun.<br /><br />"
                                         "Please note the following:<br/><ul>"
                                         "<li>Be honest</li>"
                                         "<li>Be friendly</li>"
                                         "<li>Do not insult</li>"
                                         "<li>Do not be sexually assaultive</li>"
                                         "<li>Do not send unauthorized content</li>"
                                         "<li>Do not harass anyone</li></ul>", Wt::TextFormat::UnsafeXHTML);
}

void App::createUserListContainer(Wt::WHBoxLayout *layout) {
    userListContainer_ = layout->addNew<Wt::WContainerWidget>();
    userListContainer_->setStyleClass("userlist");
}

void App::createContentContainer(Wt::WHBoxLayout *layout) {
    contentContainer_ = layout->addNew<Wt::WContainerWidget>();
    contentContainer_->setStyleClass("content");
}

void App::updateLocation() {
    CURL *curl;
    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    try {
        if (curl) {
            std::string userIP = getUserIP();
            std::string apiUrl = buildApiUrl(userIP);
            setCurlOptions(curl, apiUrl);
            performCurlRequest(curl);
            processCurlResponse();
            curl_easy_cleanup(curl);
        }
    } catch (std::exception &e) {
        handleException(e);
    }
    curl_global_cleanup();
}

std::string App::getUserIP() {
    std::string userIP = env_.clientAddress();
    return (userIP == "127.0.0.1") ? "77.189.91.196" : userIP;
}

std::string App::buildApiUrl(const std::string &userIP) {
    return "http://ip-api.com/xml/" + userIP;
}

void App::setCurlOptions(CURL *curl, const std::string &apiUrl) {
    curl_easy_setopt(curl, CURLOPT_URL, apiUrl.c_str());
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &App::WriteCallback);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseData_);
}

void App::performCurlRequest(CURL *curl) {
    CURLcode res = curl_easy_perform(curl);
    if (res != CURLE_OK) {
        throw std::runtime_error("Curl request failed");
    }
}

void App::processCurlResponse() {
    xmlDocPtr doc = xmlReadMemory(responseData_.c_str(), responseData_.size(), NULL, NULL, 0);
    if (doc != NULL) {
        parseXmlDocument(doc);
        xmlFreeDoc(doc);
    }
}

void App::parseXmlDocument(xmlDocPtr doc) {
    xmlNodePtr countryCodeNode = xmlDocGetRootElement(doc)->children;
    while (countryCodeNode != nullptr) {
        processXmlNode(countryCodeNode);
        countryCodeNode = countryCodeNode->next;
    }
}

void App::processXmlNode(xmlNodePtr node) {
    if (xmlStrEqual(node->name, BAD_CAST "countryCode")) {
        isoCountryCode = reinterpret_cast<const char *>(xmlNodeGetContent(node));
    } else if (xmlStrEqual(node->name, BAD_CAST "country")) {
        country = reinterpret_cast<const char *>(xmlNodeGetContent(node));
    }
}

void App::handleException(const std::exception &e) {
    std::cerr << "Error: " << e.what() << std::endl;
}

bool App::isNickAllowed(const std::string& nick) {
    std::string lowercaseNick = nick;
    std::transform(lowercaseNick.begin(), lowercaseNick.end(), lowercaseNick.begin(), ::tolower);
    return (lowercaseNick != "self") &&
            (lowercaseNick != "system") &&
            (lowercaseNick != "you") &&
            std::all_of(notAllowedNickPhrases_.begin(), notAllowedNickPhrases_.end(),
                       [&](const std::string& phrase) {
                           std::string lowercasePhrase = phrase;
                           std::transform(lowercasePhrase.begin(), lowercasePhrase.end(), lowercasePhrase.begin(), ::tolower);
                           return lowercaseNick.find(lowercasePhrase) == std::string::npos;
                       });
}

template<class Class>
Class *App::addSearchItemLine(Wt::WVBoxLayout *layout, std::string label, std::unique_ptr<Wt::WContainerWidget> additionalItem) {
    auto lineContainer = layout->addNew<Wt::WContainerWidget>();
    lineContainer->setStyleClass("search-line");
    lineContainer->setPositionScheme(Wt::PositionScheme::Relative);
    auto lineLayout = lineContainer->setLayout(std::make_unique<Wt::WHBoxLayout>());
    lineContainer->setPadding(Wt::WLength("0"));
    lineContainer->setMargin(Wt::WLength("0"));
    lineLayout->addNew<Wt::WText>(label)->setWidth(Wt::WLength(9, Wt::LengthUnit::FontEm));
    lineLayout->setContentsMargins(0, 0, 0, 0);
    lineLayout->setSpacing(0);
    auto input = std::make_unique<Class>();
    auto returnInput = input.get();
    lineLayout->addWidget(std::move(input), 1);
    if (additionalItem) {
        lineLayout->addWidget(std::move(additionalItem));
        lineContainer->setOverflow(Wt::Overflow::Visible);
    }
    return returnInput;
}
