#include "ScreenLog.h"
#include "Log.h"

ScreenLog* g_screenLog = NULL;

sf::Color slErrorColor( 255, 0, 0 );
//sf::Color slNarrativeColor( 192, 192, 255 );
sf::Color slNarrativeColor( 255, 255, 127 );
sf::Color slEventColor( 255, 255, 127 );
sf::Color slSpeechColor( 192, 255, 192 );

ScreenLog::ScreenLog()
{
    m_clock.Reset();
}

ScreenLog::~ScreenLog()
{
}

void ScreenLog::addMessage(string msgText, sf::Color color, float timeoutSeconds)
{
    ScreenLogMessage msg;
    msg.m_message = msgText;
    msg.m_color = color;
    msg.m_expiryTime = m_clock.GetElapsedTime() + timeoutSeconds * 1000;

    g_log.log(LL_INFO, "Screenlog: %s", msgText.c_str());

    m_messages.push_back(msg);
}

void ScreenLog::render(sf::RenderWindow& window, sf::Text& sfText)
{
    float characterSize = 20;
    sfText.SetCharacterSize( characterSize );

    float yPos = 5;

    float elapsedTime = m_clock.GetElapsedTime();

    for (size_t i = 0; i < m_messages.size(); i++) {
        ScreenLogMessage& msg = m_messages[i];
        if ( elapsedTime > msg.m_expiryTime ) {
            m_messages.erase( m_messages.begin() + i );
            i--;
            continue;
        }

        sf::Color color = msg.m_color;
        if ( msg.m_expiryTime - elapsedTime < 1000 )
            color.a = (msg.m_expiryTime - elapsedTime) * 0.001f * 255;

        sfText.SetColor( color );
        sfText.SetString( msg.m_message );
        sfText.SetPosition(5, yPos);
        window.Draw(sfText);

        yPos += characterSize * 1.f;
    }
}
