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

ChatMessage::ChatMessage(char* data)
    : TextFeld(),
      lastWidth(0)
{
    setStyle(Style::VCenter | Style::Sichtbar | Style::Mehrzeilig | Style::AutoLineBreak);
    setSchriftZ(dynamic_cast<Schrift*>(uiFactory.initParam.schrift->getThis()));
    setSchriftSize(12);
    setAutoLineBreakSpacing("    ");
    
    __int64 time = *(__int64*)data;
    short mLen = *(short*)(data + 8);
    char* message = new char[mLen + 1];
    memcpy(message, data + 10, mLen);
    message[mLen] = 0;
    char channelLen = *(data + 10 + mLen);
    char* channel = new char[channelLen + 1];
    memcpy(channel, data + 11 + mLen, channelLen);
    channel[channelLen] = 0;
    Framework::Text channelT = channel;
    delete[] channel;

    Zeit converter(time);
    Text* timeT = converter.getZeit("d.m.y h:i:s");
    
    setSchriftFarbe(0xFFFFFFFF);
    if (channelT.istGleich("system:INFO"))
    {
        setSchriftFarbe(0xFFA0A0A0);
        channelT = "[INFO]";
    }
    else if (channelT.istGleich("system:WARNING"))
    {
        setSchriftFarbe(0xFFFF7700);
        channelT = "[WARNING]";
    }
    else if (channelT.istGleich("system:ERROR"))
    {
        setSchriftFarbe(0xFFFF0000);
        channelT = "[ERROR]";
    }
    else if (channelT.hatAt(0, "player:"))
    {
        Text* tmp = channelT.getTeilText(7);
        channelT = *tmp;
        tmp->release();
    }
    Text tmp = *timeT + " " + channelT + " " + message;
    tmp.ersetzen("\n", "\n    ");
    setText(tmp);
    timeT->release();
    delete[] message;
}

bool ChatMessage::tick(double tickVal)
{
    if (getBreite() != lastWidth)
    {
        lastWidth = getBreite();
        setHeight(getNeededHeight());
    }
    return ZeichnungHintergrund::tick(tickVal);
}