#include "Chat.h"

#include <AsynchronCall.h>
#include <FileSystem.h>

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

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

    options = new ChatOptions();

    history = new ChatHistory();
    history->setSize(getInnerWidth(), getInnerHeight() - 20);
    addMember(history);

    commandLine = uiFactory.createTextField(uiFactory.initParam);
    commandLine->setText("");
    commandLine->setSize(getInnerWidth() - 20, 20);
    commandLine->setPosition(0, getInnerHeight() - 20);
    commandLine->setStyle(Framework::TextField::Style::TextField);
    commandLine->setKeyboardEvent(
        [this](void* p, void* o, Framework::KeyboardEvent 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);

    LTDBFile iconsDat;
    iconsDat.setFile(new Text("data/images/gui_icons.ltdb"));
    iconsDat.readData(0);

    sendButton = uiFactory.createButton(uiFactory.initParam);
    sendButton->setAlphaFieldColor(0x5F337AB7);
    sendButton->setToolTipText(
        "Send", uiFactory.initParam.bildschirm, uiFactory.initParam.font);
    sendButton->setSize(20, 20);
    sendButton->setPosition(getInnerWidth() - 20, getInnerHeight() - 20);
    sendButton->addStyle(Framework::Button::Style::BImage
                         | Framework::Button::Style::BAlpha
                         | Framework::Button::Style::Background);
    sendButton->setBackgroundImageZ(iconsDat.load(0, new Text("send.png")));
    sendButton->setMouseEvent(
        [this](void* p, void* o, Framework::MouseEvent me) {
            if (me.id == ME_RLeft)
            {
                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<Drawable*>(options->getThis()));
    }
    return OptionsWindow::tick(time);
}

void Chat::render(Framework::Image& rObj)
{
    history->setSize(getInnerWidth(), getInnerHeight() - 20);
    commandLine->setSize(getInnerWidth() - 20, 20);
    commandLine->setPosition(0, getInnerHeight() - 20);
    sendButton->setPosition(getInnerWidth() - 20, getInnerHeight() - 20);
    OptionsWindow::render(rObj);
}