#include "Chat.h"

#include <DateiSystem.h>

#include "Game.h"
#include "Globals.h"
#include "ChatMessage.h"
#include <AsynchronCall.h>

Chat::Chat()
    : Fenster()
{
    setStyle(
        Fenster::Style::Erlaubt
        | Fenster::Style::Rahmen | Fenster::Style::BodyHAlpha
        | Fenster::Style::Titel
        | Fenster::Style::TitelHAlpha | Fenster::Style::Closable
        | Fenster::Style::ClosingHAlpha | Fenster::Style::ClosingKlickBuffer
        | Fenster::Style::TitelHintergrund | Fenster::Style::BodyHintergrund
        | Fenster::Style::ClosingHintergrund | Fenster::Style::MEIgnoreInside | Fenster::Style::HeightChangeable | Fenster::Style::BreiteChangeable | Fenster::Style::BodyMinBr | Fenster::Style::BodyMinHi | Fenster::Style::LeftPositionFixed | Fenster::Style::BottomPositionFixed);
    removeStyle(Fenster::Style::Sichtbar);
    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);
    setMausEreignis(Framework::_ret1ME);
    setTastaturEreignis(Framework::_ret1TE);
    setRBreite(1);
    setRFarbe(0xFF52525E);
    setKBgFarbe(0xA0000000);
    setTBgFarbe(0xA0000000);
    setSBgFarbe(0xA0000000);
    setTSchriftZ(
        dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()));
    zTTextFeld()->setSize(0, 20);
    zTTextFeld()->addStyle(TextFeld::Style::Center);
    setKMin(200, 100);

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

    commandLine = uiFactory.createTextFeld(uiFactory.initParam);
    commandLine->setText("");
    commandLine->setSize(getInnenBreite() - 40, 20);
    commandLine->setPosition(20, 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/bilder/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);

    optionsButton = uiFactory.createKnopf(uiFactory.initParam);
    optionsButton->setAlphaFeldFarbe(0x5F337AB7);
    optionsButton->setToolTipText(
        "Options", uiFactory.initParam.bildschirm, uiFactory.initParam.schrift);
    optionsButton->setSize(20, 20);
    optionsButton->setPosition(0, getInnenHeight() - 20);
    optionsButton->addStyle(Framework::Knopf::Style::HBild
                            | Framework::Knopf::Style::HAlpha
                            | Framework::Knopf::Style::Hintergrund);
    optionsButton->setHintergrundBildZ(
        iconsDat.laden(0, new Text("options.png")));
    optionsButton->setMausEreignis(
        [this](void* p, void* o, Framework::MausEreignis me) {
            if (me.id == ME_RLinks)
            {
                // TODO: open Options
            }
            return 1;
        });
    addMember(optionsButton);
}

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

bool Chat::tick(double time)
{
    history->setSize(getInnenBreite(), getInnenHeight() - 20);
    commandLine->setSize(getInnenBreite() - 40, 20);
    commandLine->setPosition(20, getInnenHeight() - 20);
    sendButton->setPosition(getInnenBreite() - 20, getInnenHeight() - 20);
    optionsButton->setPosition(0, getInnenHeight() - 20);
    return Fenster::tick(time);
}