#include "ChatHistory.h"

#include <Bild.h>
#include <Scroll.h>

ChatHistory::ChatHistory()
    : Framework::ZeichnungHintergrund()
{
    setStyle(Style::Sichtbar | Style::Erlaubt | Style::VScroll);
    setVertikalScrollPos(0);
    setMausEreignis(Framework::_ret1ME);
}

void ChatHistory::addMessage(ChatMessage* message)
{
    messages.add(message);
    scrollToBottom = 1;
}

void ChatHistory::render(Framework::Bild& rObj)
{
    ZeichnungHintergrund::render(rObj);
    if (!rObj.setDrawOptions(innenPosition, innenSize)) return;
    rObj.addScrollOffset(
        horizontalScrollBar ? horizontalScrollBar->getScroll() : 0,
        vertikalScrollBar ? vertikalScrollBar->getScroll() : 0);
    for (ChatMessage* msg : messages)
        msg->render(rObj);
    rObj.releaseDrawOptions();
}

bool ChatHistory::tick(double tickVal)
{
    int y = 0;
    for (ChatMessage* msg : messages)
    {
        msg->setPosition(0, y);
        msg->setWidth(innenSize.x);
        rend |= msg->tick(tickVal);
        y += msg->getHeight();
    }
    vertikalScrollBar->update(y, innenSize.y);
    if (scrollToBottom)
    {
        setVertikalScrollPos(y);
        scrollToBottom = 0;
    }
    return ZeichnungHintergrund::tick(tickVal);
}