//========= Copyright © 1996-2005, Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================//

#include "cbase.h"
#include "hud.h"
#include "hudelement.h"
#include "hud_macros.h"
#include "iclientmode.h"
#include "vgui_controls/AnimationController.h"
#include "vgui_controls/Label.h"
#include "vgui_controls/TextEntry.h"
#include "vgui/ILocalize.h"
#include "vgui/ISurface.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

//-----------------------------------------------------------------------------
// Purpose: Displays current ammunition level
//-----------------------------------------------------------------------------
class CHudInfoDisplay : public vgui::Panel, public CHudElement
{
	DECLARE_CLASS_SIMPLE( CHudInfoDisplay, vgui::Panel );

public:
	CHudInfoDisplay( const char *pElementName );
	void Init();
	void Reset();
	void MsgFunc_InfoText( bf_read &msg );
	bool ShouldDraw();

	bool SetInfoText( const char *source, const char *text );

protected:
	virtual void ApplySchemeSettings( vgui::IScheme *pScheme );
	virtual void OnThink();

private:
	vgui::Label *m_pInfoSource;
	vgui::TextEntry *m_pInfoText;

	vgui::HFont m_hSmallFont, m_hLargeFont;

	float m_fExpireTime;
	bool m_bMessageVisible;

	CPanelAnimationVarAliasType( int, m_iSourceX, "source_xpos", "8", "proportional_int" );
	CPanelAnimationVarAliasType( int, m_iSourceY, "source_ypos", "8", "proportional_int" );
};

DECLARE_HUDELEMENT( CHudInfoDisplay );
DECLARE_HUD_MESSAGE( CHudInfoDisplay, InfoText );

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CHudInfoDisplay::CHudInfoDisplay( const char *pElementName ) : BaseClass(NULL, "HudInfoDisplay"), CHudElement( pElementName )
{
	vgui::Panel *pParent = g_pClientMode->GetViewport();
	SetParent( pParent );
	
	SetScheme( vgui::scheme()->LoadSchemeFromFile("resource/HL2Wars.res", "HL2Wars") );

	m_fExpireTime = 0.0f;
	m_bMessageVisible = false;

	// set up the a label
	m_pInfoSource = vgui::SETUP_PANEL(new vgui::Label(this, "source", ""));
	m_pInfoSource->SetPaintBackgroundEnabled( false );
	m_pInfoSource->SetPaintBorderEnabled( false );
	m_pInfoSource->SetContentAlignment( vgui::Label::a_west );
	m_pInfoSource->SetVisible( true );

	// set up a text entry.
	m_pInfoText = vgui::SETUP_PANEL(new vgui::TextEntry(this, "text")); 
	m_pInfoText->SetMultiline( true );
	m_pInfoText->SetPaintBorderEnabled( false );
	m_pInfoText->SetVisible( true );

	SetVisible( false );
	SetAlpha( 0 );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudInfoDisplay::Init()
{
	HOOK_HUD_MESSAGE( CHudInfoDisplay, InfoText );

	int width, height; 
	vgui::surface()->GetScreenSize( 
		width, 
		height );

	SetWide( width );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudInfoDisplay::Reset()
{
	SetInfoText( NULL, NULL );
	SetAlpha( 0 );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudInfoDisplay::ApplySchemeSettings( vgui::IScheme *pScheme )
{
	m_hSmallFont = pScheme->GetFont( "HudHintTextSmall", true );
	m_hLargeFont = pScheme->GetFont( "HudHintTextLarge", true );

	m_pInfoSource->SetPos( m_iSourceX, m_iSourceY );
	m_pInfoText->SetPos( m_iSourceX, m_iSourceY );
	m_pInfoSource->SetFont( m_hSmallFont );
	m_pInfoText->SetFont( m_hSmallFont );
	m_pInfoSource->SetFgColor( Color(255,255,255,255) );
	m_pInfoText->SetFgColor( Color(255,255,255,255) );

	m_pInfoText->SetBgColor( Color( 0, 0, 0, 0) );

	BaseClass::ApplySchemeSettings( pScheme );
}

//-----------------------------------------------------------------------------
// Purpose: Save CPU cycles by letting the HUD system early cull
// costly traversal.  Called per frame, return true if thinking and 
// painting need to occur.
//-----------------------------------------------------------------------------
bool CHudInfoDisplay::ShouldDraw( void )
{
	return ( ( GetAlpha() > 0 ) && CHudElement::ShouldDraw() );
}

//-----------------------------------------------------------------------------
// Purpose: Updates the label color each frame
//-----------------------------------------------------------------------------
void CHudInfoDisplay::OnThink()
{
	if( m_bMessageVisible )
	{
		if( m_fExpireTime < gpGlobals->curtime ) 
		{
			g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "InfoMessageHide" ); 
			m_bMessageVisible = false;
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: Sets the hint text, replacing variables as necessary
//-----------------------------------------------------------------------------
bool CHudInfoDisplay::SetInfoText( const char *source, const char *text )
{
	if( source == NULL || text == NULL )
		return false;

	// put a ':' behind the source
	char source_madeup[MAX_PATH];
	Q_snprintf( source_madeup, sizeof( source_madeup ), "%s:", source );

	// localize text if possible
	const wchar_t *pText;
	wchar_t textBuf[ 1024 ];

	pText = g_pVGuiLocalize->Find( text );
	if ( !pText )
	{
		g_pVGuiLocalize->ConvertANSIToUnicode(text, textBuf, sizeof( textBuf ) );
		pText = textBuf;
	}

	m_pInfoSource->SetText( source_madeup );
	m_pInfoText->SetText( pText );

	m_pInfoSource->SizeToContents();

	m_pInfoText->SetPos( m_iSourceX + 8 + m_pInfoSource->GetWide(), m_iSourceY );
	int wide, tall;
	GetSize( wide, tall );
	m_pInfoText->SetSize( wide - ( ( m_iSourceX * 2 ) + 8 + m_pInfoSource->GetWide() + 32 ),
		tall - 2 * m_iSourceY );
	m_pInfoText->GotoTextStart();

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: Activates the hint display
//-----------------------------------------------------------------------------
void CHudInfoDisplay::MsgFunc_InfoText( bf_read &msg )
{
	// how many strings do we receive ?
	int count = msg.ReadByte();

	// here we expect only one string
	if ( count != 2 )
	{
		DevMsg("CHudInfoDisplay::MsgFunc_InfoText: string count != 2.\n");
		return;
	}

	// read the source
	char szSource[256];
	msg.ReadString( szSource, sizeof(szSource) );

	// read the string
	char szString[2048];
	msg.ReadString( szString, sizeof(szString) );

	m_fExpireTime = gpGlobals->curtime + msg.ReadFloat();
	
	//Msg("We received two strings! \n source: %s, message: %s, expire: %f \n", szSource, szString, m_fExpireTime );

	if( SetInfoText( szSource, szString ) )
	{
		m_bMessageVisible = true;
		SetVisible( true );
		g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "InfoMessageShow" ); 
	}
	else
	{
		g_pClientMode->GetViewportAnimationController()->StartAnimationSequence( "InfoMessageHide" ); 
	}
}
