#include "Chat.h"

#include <AsynchronCall.h>
#include <DateiSystem.h>

#include "ChatMessage.h"
#include "Game.h"
#include "Globals.h"

Chat::Chat()
    : OptionsWindow([this]() { options->addStyle(Fenster::Style::Sichtbar); }),
      optionsAdded(0)
{
    addStyle( Fenster::Style::LeftPositionFixed
        | Fenster::Style::BottomPositionFixed);
    removeStyle(Fenster::Style::Beweglich);
    setTitel("Chat");
    setClosingMe([this](void* p, void* o, Framework::MausEreignis me) {
        if (me.id == Framework::ME_RLinks)
        {
            removeStyle(Fenster::Style::Sichtbar);
            ((Game*)(Menu*)menuRegister->get("game"))->makeChatButtonVisible();
        }
        return 1;
    });
    setSize(500, 300);
    setPosition(5, uiFactory.initParam.bildschirm->getBackBufferSize().y - 305);
    setKMin(200, 100);

    options = new ChatOptions();

    history = new ChatHistory();
    history->setSize(getInnenBreite(), getInnenHeight() - 20);
    addMember(history);

    commandLine = uiFactory.createTextFeld(uiFactory.initParam);
    commandLine->setText("");
    commandLine->setSize(getInnenBreite() - 20, 20);
    commandLine->setPosition(0, getInnenHeight() - 20);
    commandLine->setStyle(Framework::TextFeld::Style::TextFeld);
    commandLine->setTastaturEreignis(
        [this](void* p, void* o, Framework::TastaturEreignis te) {
            if (te.id == Framework::TE_Release
                && te.virtualKey == Framework::T_Enter)
            {
                if (commandLine->zText()->getLength() > 0)
                {
                    Text* msg = new Text(*commandLine->zText());
                    commandLine->setText("");
                    new AsynchronCall([msg]() {
                        World::INSTANCE->zClient()->sendChatMessage(*msg);
                        msg->release();
                    });
                }
            }
            return 1;
        });
    addMember(commandLine);

    LTDBDatei iconsDat;
    iconsDat.setDatei(new Text("data/images/gui_icons.ltdb"));
    iconsDat.leseDaten(0);

    sendButton = uiFactory.createKnopf(uiFactory.initParam);
    sendButton->setAlphaFeldFarbe(0x5F337AB7);
    sendButton->setToolTipText(
        "Send", uiFactory.initParam.bildschirm, uiFactory.initParam.schrift);
    sendButton->setSize(20, 20);
    sendButton->setPosition(getInnenBreite() - 20, getInnenHeight() - 20);
    sendButton->addStyle(Framework::Knopf::Style::HBild
                         | Framework::Knopf::Style::HAlpha
                         | Framework::Knopf::Style::Hintergrund);
    sendButton->setHintergrundBildZ(iconsDat.laden(0, new Text("send.png")));
    sendButton->setMausEreignis(
        [this](void* p, void* o, Framework::MausEreignis me) {
            if (me.id == ME_RLinks)
            {
                if (commandLine->zText()->getLength() > 0)
                {
                    Text* msg = new Text(*commandLine->zText());
                    commandLine->setText("");
                    new AsynchronCall([msg]() {
                        World::INSTANCE->zClient()->sendChatMessage(*msg);
                        msg->release();
                    });
                }
            }
            return 1;
        });
    addMember(sendButton);
}

Chat ::~Chat()
{
    options->release();
}

void Chat::addMessage(char* data)
{
    history->addMessage(new ChatMessage(data));
}

void Chat::initOptions(char* data)
{
    options->init(data);
}

bool Chat::tick(double time)
{
    if (!optionsAdded)
    {
        optionsAdded = 1;
        uiFactory.initParam.bildschirm->addMember(
            dynamic_cast<Zeichnung*>(options->getThis()));
    }
    return OptionsWindow::tick(time);
}

void Chat::render(Framework::Bild& rObj)
{
    history->setSize(getInnenBreite(), getInnenHeight() - 20);
    commandLine->setSize(getInnenBreite() - 20, 20);
    commandLine->setPosition(0, getInnenHeight() - 20);
    sendButton->setPosition(getInnenBreite() - 20, getInnenHeight() - 20);
    OptionsWindow::render(rObj);
}