/*********************************************************************//**
*	Message
*	Prida hud zpravu.
*	
*	author: Michal Jirous
*	date: 23.04.2009
*	file: ent_env_message.cpp
**********************************************************************/

#include "ent_environments.h"


EnvMessage::EnvMessage() : CBaseEnvironment()
{
	m_sClassName = "env_message";
	m_bPlayOnce = false;
	m_bIsOn = true;
}

void EnvMessage::passFlags( int flags )
{
	if(flags & PLAY_ONCE)
		m_bPlayOnce = true;
}

#include "HUD_messages.h"
using namespace HUDMessages;
void EnvMessage::onTarget( int target_type )
{
	if( m_bIsOn )
	{
		if( m_bPlayOnce )
			m_bIsOn = false;
	
		HUD_messages.insertMessage( m_dsMessage, x, y, m_uiDuration,m_Color );

	}
}

#include "sys_config.h"
#include "textLibrary.h"
void EnvMessage::setParameters( parameters_t &parametersMap )
{
	CBaseEnvironment::setParameters( parametersMap );
	
	int iTextMaxWidth = systemconf::getSystemInfo().m_iResolutionWidth;
	std::string sValue = getParameterValue( parametersMap, "xposition" );
	if( !sValue.empty() )
	{
		float i = (float)atof( sValue.c_str() );
		if( isInRange( i, 0.0f, 1.0f ) )
		{
			i = (float)(systemconf::getSystemInfo().m_iResolutionWidth * i);
		}
		else 
			i = (float)(systemconf::getSystemInfo().m_iResolutionWidth >> 1);
			
		x = i;
		iTextMaxWidth = (int)(systemconf::getSystemInfo().m_iResolutionWidth - x) << 1;
	}

	sValue = getParameterValue( parametersMap, "yposition" );
	if( !sValue.empty() )
	{
		float i = (float)atof( sValue.c_str() );
		if( isInRange( i, 0.0f, 1.0f ) )
		{
			i = (float)(systemconf::getSystemInfo().m_iResolutionHeight * i);
		}
		else 
			i = (float)((systemconf::getSystemInfo().m_iResolutionHeight >> 1) + 50);//50 je korekce vysky, aby text nebyl v oblasti zamerovace	

		y = i;
	}

	sValue = getParameterValue( parametersMap, "fadetime" );
	if( !sValue.empty() )
		m_uiDuration = atoi( sValue.c_str() );

	sValue = getParameterValue( parametersMap, "rendercolor" );
	if( !sValue.empty() )
	{
		Vector color(sValue);
		m_Color = Color4I( (GLuint)color.x, (GLuint)color.y, (GLuint)color.z, 255 );
	}

	sValue = getParameterValue( parametersMap, "message_key" );
	if( !sValue.empty() )
	{
		unsigned key = atoi( sValue.c_str() );
		if( key != -1 )
			createMessage( textLibrary::getLevelText( key ), iTextMaxWidth );
	}
	
	if( m_dsMessage.empty() )
	{
		sValue = getParameterValue( parametersMap, "message" );
		if( !sValue.empty() )
		{
			m_dsMessage.create( sValue, ENV_MESSAGE_FONT, FONT_ALIGN_CENTER, iTextMaxWidth );
		}
	}
}

#include "sstream"
#include "ctrl_keydatabase.h"
#include "ctrl_binding.h"
#include "parsing.h"
void EnvMessage::createMessage( std::string str, int maxWidth )
{
	std::istringstream in( str );

	std::string command;
	char c = 0;
	ostringstream out;
	while( (c = in.get() ) != -1 )
	{
		if( c == '#' )
		{
			command = parsing::readField( in, '#' );
			binding::Keys *pKey = binding::getKeyOfWatchedCommand( command );
			if( pKey )
				out << key_database::getKey( pKey->key_one );
		
		}
		else
			out << c;
	
	
	}
	m_dsMessage.create( out.str(), ENV_MESSAGE_FONT, FONT_ALIGN_CENTER, maxWidth );
}


