//========= Copyright © 1996-2005, Sander Corporation, All rights reserved. ============//
//
// Purpose: hud_rts_unitpanel.cpp
//
// $NoKeywords: $
//=============================================================================//

#include "cbase.h"
#include "hud.h"
#include "hl2wars/hud_rts_infopanel.h"
#include <KeyValues.h>
#include <filesystem.h>
#include <vgui/ISurface.h>
#include <vgui_controls/Label.h>
#include <vgui_controls/RichText.h>
#include <vgui/IScheme.h>
//#include "hl2wars/vgui_bitmapbuttonrts.h"
#include "c_rts_globalresource.h"
#include "hl2wars/rts_parsedata.h"
#include <vgui/IVGui.h>
#include "vgui/ILocalize.h"
#include <vgui_controls/TextEntry.h>

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

const int NumSegments = 7;
static int coord[NumSegments+1] = {
	0,
	1,
	2,
	3,
	4,
	6,
	9,
	10
};

using namespace vgui;

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
Description::Description(Panel *parent, const char *panelName) : TextEntry(parent, panelName)
{
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void Description::ApplySchemeSettings(IScheme *pScheme)
{
	BaseClass::ApplySchemeSettings(pScheme);
	
	SetFgColor(Color(200,200,200,255));
	SetBgColor(Color(200,200,200,0));

	SetFont( pScheme->GetFont( "DebugFixedSmall" ) );

	SetBorder(NULL);
}

DECLARE_HUDELEMENT( CHudRTSInfoPanel );
//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CHudRTSInfoPanel::CHudRTSInfoPanel( const char *pElementName ) :
  CHudElement( pElementName ), BaseClass( NULL, "HudInfoPanel" )
{
	vgui::Panel *pParent = g_pClientMode->GetViewport();
	SetParent( pParent );	//Our parent is the screen itself.

	SetProportional( true );
	SetPaintBackgroundEnabled( true );
	//SetPaintBorderEnabled( true );
	SetKeyBoardInputEnabled( false );	
	SetMouseInputEnabled( false );	
	//SetScheme("ClientScheme.res");
	SetScheme( vgui::scheme()->LoadSchemeFromFile("resource/HL2Wars.res", "HL2Wars") );
	SetVisible( false );
	SetCursor( GetParent()->GetCursor() ); 

	//m_hButton = NULL;

	//SetBgColor( Color(150, 150, 150, 150) );
	//SetBorder( pScheme->GetBorder( "BaseBorder" ) );

	// setting up headers for some Label's
	m_pTimeHeader = vgui::SETUP_PANEL(new vgui::Label(this,"InfoTimeHeader","Time:"));		
	m_pTimeHeader->SetScheme("ClientScheme.res");	//Set it's scheme...
	m_pTimeHeader->SetSize(vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 50 ),
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 15 ) );
	m_pTimeHeader->SetPos(vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 24 ) ,
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 19 ));	
	m_pTimeHeader->SetPaintBackgroundEnabled( false );
	m_pTimeHeader->SetPaintBorderEnabled( false );	
	m_pTimeHeader->SetContentAlignment( vgui::Label::a_west );
	m_pTimeHeader->SetVisible( true );
	//m_pTimeHeader->SizeToContents();
	int time_wide = m_pTimeHeader->GetWide();

	m_pCostHeader = vgui::SETUP_PANEL(new vgui::Label(this,"InfoTimeHeader","Cost:"));		
	m_pCostHeader->SetScheme("ClientScheme.res");	//Set it's scheme...
	m_pCostHeader->SetSize( vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 50 ), 
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 15 ) );
	m_pCostHeader->SetPos(vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 24 ) ,
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 12 ));		
	m_pCostHeader->SetPaintBackgroundEnabled( false );
	m_pCostHeader->SetPaintBorderEnabled( false );	
	m_pCostHeader->SetContentAlignment( vgui::Label::a_west );
	m_pCostHeader->SetVisible( true );
	//m_pCostHeader->SizeToContents();
	int cost_wide = m_pCostHeader->GetWide();

	//---------- setting up labels
	m_pTitle = vgui::SETUP_PANEL(new vgui::Label(this,"InfoTitle",""));		
	m_pTitle->SetScheme("ClientScheme.res");	//Set it's scheme...
	m_pTitle->SetSize( vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 100 ), 
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 15 ) );
	m_pTitle->SetPos( vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 12 ) ,
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 6 ));	
	m_pTitle->SetPaintBackgroundEnabled( false );
	m_pTitle->SetPaintBorderEnabled( false );	
	m_pTitle->SetContentAlignment( vgui::Label::a_west );
	m_pTitle->SetVisible( true );
	
	//m_pDescription = vgui::SETUP_PANEL(new vgui::RichText(this,"InfoDescription"));
	//m_pDescription = vgui::SETUP_PANEL(new vgui::RichText(this,"InfoDescription"));
	//m_pDescription = new vgui::RichText(this,"InfoDescription");
	m_pDescription = new vgui::Description(this,"InfoDescription");
	//m_pDescription->SetScheme( vgui::scheme()->LoadSchemeFromFile("resource/HL2Wars.res", "HL2Wars") );
	//m_pDescription->SetPaintBackgroundEnabled( false );
	//m_pDescription->SetPaintBorderEnabled( false );	
	//m_pDescription->SetContentAlignment( vgui::Label::a_west );
	m_pDescription->SetPaintBackgroundEnabled( true );
	m_pDescription->SetPaintBorderEnabled( true );
	m_pDescription->SetVisible( true );
	m_pDescription->SetVerticalScrollbar( false );
	m_pDescription->SetMultiline(true);
	m_pDescription->SetEditable(false);
	m_pDescription->SetEnabled( false );

	m_pCost = vgui::SETUP_PANEL(new vgui::Label(this,"InfoCost",""));		
	m_pCost->SetScheme("ClientScheme.res");	//Set it's scheme...
	m_pCost->SetSize( vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 100 ), 
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 15 ) );
	m_pCost->SetPos( vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 24 ) + cost_wide ,
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 12 ));		
	m_pCost->SetPaintBackgroundEnabled( false );
	m_pCost->SetPaintBorderEnabled( false );	
	m_pCost->SetContentAlignment( vgui::Label::a_west );
	m_pCost->SetVisible( true );

	m_pTime = vgui::SETUP_PANEL(new vgui::Label(this,"InfoTime",""));		
	m_pTime->SetScheme("ClientScheme.res");	//Set it's scheme...
	m_pTime->SetSize( vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 100 ), 
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 15 ) );
	m_pTime->SetPos(vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 24 ) + time_wide ,
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 19 ));	
	m_pTime->SetPaintBackgroundEnabled( false );
	m_pTime->SetPaintBorderEnabled( false );	
	m_pTime->SetContentAlignment( vgui::Label::a_west );
	m_pTime->SetVisible( true );

	// load icons
	m_iCostIcon = vgui::surface()->CreateNewTextureID(); 
	vgui::surface()->DrawSetTextureFile( m_iCostIcon, "vgui/icons/icon_cost" , true, false); 
	m_iTimeIcon = vgui::surface()->CreateNewTextureID(); 
	vgui::surface()->DrawSetTextureFile( m_iTimeIcon, "vgui/icons/icon_reChargetime" , true, false); 

	m_utlDescriptionBuildings.SetLessFunc( DefLessFunc( int ) );
	m_ddDescriptionAbilities.SetLessFunc( DefLessFunc( int ) );

	vgui::ivgui()->AddTickSignal( GetVPanel() );
}

//-----------------------------------------------------------------------------
// Purpose: Deconstructor
//-----------------------------------------------------------------------------
CHudRTSInfoPanel::~CHudRTSInfoPanel()
{

}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudRTSInfoPanel::Init( void )
{
}

//-----------------------------------------------------------------------------
// Purpose: 
// Input  : *newmap - 
//-----------------------------------------------------------------------------
void CHudRTSInfoPanel::LevelInit( const char *newmap )
{
	m_fTimeOut = 0.0f;
	SetVisible( false );	// make sure we ain't visible at start of another map
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudRTSInfoPanel::LevelShutdown( void )
{
	m_fTimeOut = 0.0f;
	SetVisible( false );	// make sure we ain't visible at start of another map

	m_utlDescriptionBuildings.RemoveAll();
	m_ddDescriptionAbilities.RemoveAll();
}

//-----------------------------------------------------------------------------
// Purpose: sets up screen
//-----------------------------------------------------------------------------
void CHudRTSInfoPanel::ApplySchemeSettings( IScheme *pScheme )
{
	BaseClass::ApplySchemeSettings( pScheme );
	m_bgColor = GetSchemeColor("ObjElement.BgColor", GetBgColor(), pScheme);
	m_borderColor = pScheme->GetColor( "ObjElement.BrColor", Color( 0, 0, 0, 0 ) );

	//GetSize( m_iWidth, m_iTall );
	//SetBgColor( m_bgColor );
	//SetBorder( pScheme->GetBorder( "BaseBorder" ) );

	//SetBgColor(GetSchemeColor("WizardSubPanel.BgColor",pScheme));

	// save the screensize
	int m_iScreenWidth, m_iScreenHeight;
	surface()->GetScreenSize( 
		m_iScreenWidth, 
		m_iScreenHeight );

	KeyValues *pInterfaceMainData = new KeyValues( "data" );
	if ( !pInterfaceMainData->LoadFromFile( filesystem, "resource/ui/HudInterface.res", "GAME" ) )
	{
		Warning("Error! CHudRTSInfoPanel::Init: couldn't load file resource/ui/HudInterface.res.\n" );
		return;
	}

	// retrieve the right values for the screensize
	KeyValues *pData;
	if( ( (float)m_iScreenWidth / (float)m_iScreenHeight ) < 1.45f )
		pData = pInterfaceMainData->FindKey("normal");
	else
		pData = pInterfaceMainData->FindKey("wide");

	if( !pData )
	{
		Warning("Failed to load the screen data settings! \n");
		return;
	}
	
	// load the right background
	CHudTexture *m_pHudPanel = new CHudTexture();
	m_pHudPanel = gHUD.GetIcon(  pData->GetString("texture") );

	float tex_width, tex_height;
	tex_width = (float)m_pHudPanel->Width();
	tex_height = (float)m_pHudPanel->Height();

	float size_fix = 1.0f;
	if( tex_width != 0 )
		size_fix = m_iScreenWidth / tex_width;
	int size_y = size_fix * tex_height;

	// now apply our own pos y
	int x, y;
	GetPos( x, y );
	SetPos( x, m_iScreenHeight - size_y - GetTall() + 4 );

	GetSize( m_iWidth, m_iTall );

	// set up labels
	//vgui::IScheme *pScheme = vgui::scheme()->GetIScheme(GetScheme());
	vgui::HFont hFontNormal = pScheme->GetFont( "Default" );
	vgui::HFont hFontSmall = pScheme->GetFont( "DefaultVerySmall" );
	vgui::HFont hFontSmallest = pScheme->GetFont( "DebugFixedSmall" );

	m_pTimeHeader->SetFont(hFontSmall);
	m_pTimeHeader->SetBgColor(Color(0,0,0,0));	
	m_pTimeHeader->SetFgColor( Color(200,200,200,255) );

	m_pCostHeader->SetFont(hFontSmall);  
	m_pCostHeader->SetBgColor(Color(0,0,0,0));	
	m_pCostHeader->SetFgColor( Color(200,200,200,255) );

	m_pTitle->SetFont(hFontNormal);  
	m_pTitle->SetBgColor(Color(0,0,0,0));		
	m_pTitle->SetFgColor( Color(185,181,68,255) );

	m_pDescription->SetFont(hFontSmallest);  
	//m_pDescription->SetBgColor(Color(200,200,200,255));	
	//m_pDescription->SetFgColor( Color(200,200,200,255) );
	m_pDescription->SetPos( vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 12 ) ,
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 32 ));	
	m_pDescription->SetSize( vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 140 ), 
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 50 ) );
	//m_pDescription->SetMaximumCharCount(-1);
	m_pDescription->SetEnabled( true );

	m_pCost->SetFont(hFontSmall);  
	m_pCost->SetBgColor(Color(0,0,0,0));
	m_pCost->SetFgColor( Color(200,200,200,255) );

	m_pTime->SetFont(hFontSmall); 
	m_pTime->SetBgColor(Color(0,0,0,0));
	m_pTime->SetFgColor( Color(200,200,200,255) );
}

//-----------------------------------------------------------------------------
// Purpose: Paint background for rounded corners
//-----------------------------------------------------------------------------
void CHudRTSInfoPanel::PaintBackground()
{
	int x1, x2, y1, y2;
	surface()->DrawSetColor(m_bgColor);
	surface()->DrawSetTextColor(m_bgColor);

	int wide, tall;
	GetSize( wide, tall );

	int i;

	// top-left corner --------------------------------------------------------
	int xDir = 1;
	int yDir = -1;
	int xIndex = 0;
	int yIndex = NumSegments - 1;
	int xMult = 1;
	int yMult = 1;
	int x = 0;
	int y = 0;
	for ( i=0; i<NumSegments; ++i )
	{
		x1 = min( x + coord[xIndex]*xMult, x + coord[xIndex+1]*xMult );
		x2 = max( x + coord[xIndex]*xMult, x + coord[xIndex+1]*xMult );
		y1 = max( y + coord[yIndex]*yMult, y + coord[yIndex+1]*yMult );
		y2 = y + coord[NumSegments];
		surface()->DrawFilledRect( x1, y1, x2, y2 );

		xIndex += xDir;
		yIndex += yDir;
	}

	// paint between top left and bottom left ---------------------------------
	x1 = 0;
	x2 = wide;
	y1 = coord[NumSegments];
	y2 = tall;
	surface()->DrawFilledRect( x1, y1, x2, y2 );

	x1 = coord[NumSegments];
	x2 = wide;
	y1 = 0;
	y2 = coord[NumSegments];
	surface()->DrawFilledRect( x1, y1, x2, y2 );
}

//-----------------------------------------------------------------------------
// Purpose: Paint
//-----------------------------------------------------------------------------
void CHudRTSInfoPanel::Paint( void )
{
	//BaseClass::Paint();

	//vgui::surface()->DrawSetColor( m_bgColor );
	//vgui::surface()->DrawFilledRect(0, 0, m_iWidth, m_iTall); 

	// get right data
	description_data data;
	switch( m_iInfoType ) {
		case INFOTYPE_BUILDING:
			data = GetInfoBuilding();
			break;
		case INFOTYPE_ABILITY:
			//data = m_ddDescriptionAbilities[m_iType];
			data = GetInfoAbility();
			break;
		default:
			return;		// abort paint
			break;
	}

	// draw icons
	vgui::surface()->DrawSetColor( 255, 255, 255, 255 );
	vgui::surface()->DrawSetTexture( m_iCostIcon );
	vgui::surface()->DrawTexturedRect( 
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 12 ),
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 15 ), 
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 21 ),
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 24 ) 
	);	

	vgui::surface()->DrawSetColor( 255, 255, 255, 255 );
	vgui::surface()->DrawSetTexture( m_iTimeIcon );
	vgui::surface()->DrawTexturedRect( 
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 12 ),
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 23 ), 
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 20 ),
		vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 31 ) 
	);	

	// set right text
	m_pTitle->SetText( data.m_sTitle );
	m_pCost->SetText( data.m_sCost );
	m_pTime->SetText( data.m_sTime );

	m_pDescription->SetText( "" );

	m_pDescription->InsertString( data.m_sDescription );

	m_pDescription->SetToFullHeight();
	m_pDescription->GotoTextStart();
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CHudRTSInfoPanel::description_data CHudRTSInfoPanel::GetInfoBuilding()
{
	int index = m_utlDescriptionBuildings.Find( m_iType );
	if( index < (int)m_utlDescriptionBuildings.Count() ) {
		return m_utlDescriptionBuildings.Element( index );
	}
	else {
		FileUnitInfo_t *pFUI = GetFileUnitInfoFromHandle( LookupUnitInfoSlot( STRING( g_RTSR->GetUnitType( m_iType ) ) ) );
		if( !pFUI )
			return m_utlDescriptionBuildings.Element( 0 );

		char buf[5];

		description_data data;
		Q_strncpy( data.m_sTitle, pFUI->m_szName, MAX_INFOSTRING );
		Q_strncpy( data.m_sCost, itoa( pFUI->m_iCost, buf, 10), MAX_INFOSTRING );
		Q_strncpy( data.m_sTime, itoa( pFUI->m_iRechargeTime, buf, 10), MAX_INFOSTRING );
		Q_strncpy( data.m_sDescription, pFUI->m_szDescription, MAX_INFOSTRING );
		index = m_utlDescriptionBuildings.Insert( m_iType, data);

		return m_utlDescriptionBuildings.Element( index );
	}
}

//-----------------------------------------------------------------------------
// Purpose: FIXME
//-----------------------------------------------------------------------------
CHudRTSInfoPanel::description_data CHudRTSInfoPanel::GetInfoAbility()
{
	int index = m_ddDescriptionAbilities.Find( m_iType );
	if( index < (int)m_ddDescriptionAbilities.Count() ) {
		return m_ddDescriptionAbilities.Element( index );
	}
	else {
		FileAbilityInfo_t *pFAI = GetFileAbilityInfoFromHandle( m_iType );
		if( !pFAI )
			return m_ddDescriptionAbilities.Element( 0 );

		char buf[5];

		description_data data;
		Q_strncpy( data.m_sTitle, pFAI->m_szName, MAX_INFOSTRING );
		Q_strncpy( data.m_sCost, itoa( pFAI->m_iCost, buf, 10), MAX_INFOSTRING );
		Q_strncpy( data.m_sTime, itoa( pFAI->m_iRechargeTime, buf, 10), MAX_INFOSTRING );
		Q_strncpy( data.m_sDescription, pFAI->m_szDescription, MAX_INFOSTRING );
		index = m_ddDescriptionAbilities.Insert( m_iType, data);

		return m_ddDescriptionAbilities.Element( index );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudRTSInfoPanel::OnTick(void)
{
	if( m_fTimeOut < gpGlobals->curtime ) {
		SetVisible( false );
	}
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudRTSInfoPanel::SetType( int type )
{
	m_iType = type;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudRTSInfoPanel::SetInfoType( int info_type )
{
	m_iInfoType = info_type;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudRTSInfoPanel::SetTimeOut( float time )
{
	m_fTimeOut = time;
}
