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

#include "cbase.h"
#include "hud.h"
#include "hl2wars/hud_iconsvisuals.h"
#include "iclientmode.h"
#include "view.h"
#include "vgui_controls/controls.h"
#include "vgui/ISurface.h"
#include "IVRenderView.h"
#include "input.h"

#include "hl2wars/c_hl2wars_player.h"
#include "c_ai_basenpc.h"
#include "hl2wars/c_rts_buildings.h"
#include "hl2wars/c_rts_globalresource.h"

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

using namespace vgui;

// ##################################################################################
//	>> objective visual class
//
// ##################################################################################
// link data table DT_Myentity to client class and map variables (RecvProps)
IMPLEMENT_CLIENTCLASS_DT( C_EnvRTSObjective, DT_EnvRTSObjective, CEnvRTSObjective )
	RecvPropInt( RECVINFO( m_iObjective ) ),
END_RECV_TABLE()

// a list of objective env to search quickly
C_EntityClassList<C_EnvRTSObjective> g_EnvRTSObjectiveList;
C_EnvRTSObjective *C_EntityClassList<C_EnvRTSObjective>::m_pClassList = NULL;
C_EnvRTSObjective *GetEnvRTSObjectiveList()
{
	return g_EnvRTSObjectiveList.m_pClassList;
}

// ##################################################################################
//	>> capture point visual class
//
// ##################################################################################
// link data table DT_Myentity to client class and map variables (RecvProps)
IMPLEMENT_CLIENTCLASS_DT( C_CapturePoint, DT_CapturePoint, CCapturePoint )
	RecvPropInt( RECVINFO( m_iTeamOwnership ) ),
	RecvPropInt( RECVINFO( m_iTeamCapturing ) ),
	//RecvPropFloat( RECVINFO( m_flRemainingCaptureTime ) ),
	RecvPropFloat( RECVINFO( m_flNextCaptureTime ) ),
	RecvPropVector	(RECVINFO(m_vIconTarget)),
END_RECV_TABLE()

// a list of capture points
C_EntityClassList<C_CapturePoint> g_CapturePointList;
C_CapturePoint *C_EntityClassList<C_CapturePoint>::m_pClassList = NULL;
C_CapturePoint *GetCapturePointList()
{
	return g_CapturePointList.m_pClassList;
}

// ##################################################################################
//	>> The hud we are drawing the icons on
//
// ##################################################################################

DECLARE_HUDELEMENT( CHudIconsVisuals );

CHudIconsVisuals::CHudIconsVisuals( const char *pElementName ) :
  CHudElement( pElementName ), BaseClass( NULL, "HudIconsVisuals" )
{
	vgui::Panel *pParent = g_pClientMode->GetViewport();
	SetParent( pParent );

	SetHiddenBits( HIDEHUD_RTS | HIDEHUD_PLAYERDEAD );

	SetZPos( -1 );

	SetPaintBackgroundEnabled( false );
	SetPaintBorderEnabled( false );
}

void CHudIconsVisuals::ApplySchemeSettings( IScheme *scheme )
{
	BaseClass::ApplySchemeSettings( scheme );

    SetSize( ScreenWidth(), ScreenHeight() );
}

//-----------------------------------------------------------------------------
// 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 CHudIconsVisuals::ShouldDraw( void )
{
	C_BasePlayer* pPlayer = C_BasePlayer::GetLocalPlayer();
	if ( !pPlayer )
		return false;

	return true;
}*/

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudIconsVisuals::OnThink()
{

}

void CHudIconsVisuals::Paint( void )
{
	DrawCapturePoints();	
	//DrawHealthBuildings();
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CHudIconsVisuals::DrawCapturePoints( void )
{
	for( C_CapturePoint *pEnt = GetCapturePointList(); pEnt != NULL; pEnt = pEnt->m_pNext )
	{
		Vector color;

		int xpos, ypos;
		GetVectorInScreenSpace( pEnt->GetIconTarget(), xpos, ypos);

		if( pEnt->GetCapturingTeam() != -1 ) {
			color = g_RTSR->GetColor255( pEnt->GetCapturingTeam() );

			// draw time left to capture and capture icon
			float weigth = (pEnt->GetCaptureTime() - gpGlobals->curtime) / CAPTURE_TIME;
			int fill_bar = (int)(weigth * vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 120 ) );

			// draw the outlined rect
			vgui::surface()->DrawSetColor( Color( 255,255,255,200 ) );
			vgui::surface()->DrawOutlinedRect(
				xpos - vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 60 ), 
				ypos - vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 32 ), 
				xpos + vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 60 ), 
				ypos - vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 25 )
			);

			// draw how much time we still got
			vgui::surface()->DrawSetColor( Color( color.x, color.y, color.z, 150 ) );
			vgui::surface()->DrawFilledRect(
				xpos - vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 60 ), 
				ypos - vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 32 ), 
				( xpos - vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 60 ) ) + fill_bar, 
				ypos - vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 25 )
			);
		}
		else {
			/*color = g_RTSR->GetColor255( pEnt->GetOwnerShip() );

			// draw ownership icon
			vgui::surface()->DrawSetColor( color.x, color.y, color.z, 255 );
			vgui::surface()->DrawSetTexture( m_iCaptureIcon );
			vgui::surface()->DrawTexturedRect( xpos, ypos, 
				xpos + vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 25 ),
				ypos + vgui::scheme()->GetProportionalScaledValueEx( GetScheme(), 25 ) 
			);	*/
		}
	}
}

/*void CHudIconsVisuals::DrawHealthBuildings( void )
{
	for( C_BaseBuildingModel *pEnt = GetBuildingModelList(); pEnt != NULL; pEnt = pEnt->m_pNext )
	{
		DrawHealthBar(pEnt);
	}
}

//-----------------------------------------------------------------------------
// Purpose: Draw health bar
//-----------------------------------------------------------------------------
void CHudIconsVisuals::DrawHealthBar( C_BaseBuildingModel *pTarget )
{
	// get the percentage health
	float weigth = (float)pTarget->GetHealth() / (float)pTarget->GetMaxHealth();
	int healthSize = (int)(weigth * vgui::scheme()->GetProportionalScaledValue( 30 ) );
	
	// get the position on the screen of the npc
	int xpos = 0, ypos = 0;
	GetVectorInScreenSpace(pTarget->GetAbsOrigin(), xpos, ypos);
	//Msg("Draw health, x: %d, y: %d \n", xpos, ypos );
	
	// calculate the colors
	int red = 255 - (int)(weigth * 255.0f);
	int green = (int)(weigth * 255.0f);

	// draw the outlined rect
	vgui::surface()->DrawSetColor( Color( 255,255,255,200 ) );
	vgui::surface()->DrawOutlinedRect(
		xpos - vgui::scheme()->GetProportionalScaledValue( 15 ), 
		ypos + vgui::scheme()->GetProportionalScaledValue( 15 ), 
		xpos + vgui::scheme()->GetProportionalScaledValue( 15 ), 
		ypos + vgui::scheme()->GetProportionalScaledValue( 18 )
	);

	// draw how much health we still got
	vgui::surface()->DrawSetColor( Color( red, green, 0, 125 ) );
	vgui::surface()->DrawFilledRect(
		xpos - vgui::scheme()->GetProportionalScaledValue( 15 ), 
		ypos + vgui::scheme()->GetProportionalScaledValue( 15 ), 
		( xpos - vgui::scheme()->GetProportionalScaledValue( 15 ) ) + healthSize, 
		ypos + vgui::scheme()->GetProportionalScaledValue( 18 )
	);
}*/

//---------------------------- objective visual functions
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
C_EnvRTSObjective::C_EnvRTSObjective( void )
{
	SetVisible( false );
	gameeventmanager->AddListener( this, "objective_updated", false );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
C_EnvRTSObjective::~C_EnvRTSObjective( void )
{
	gameeventmanager->RemoveListener(this);
	SetVisible( false );
}

//-----------------------------------------------------------------------------
// Purpose: Game event
//-----------------------------------------------------------------------------
void C_EnvRTSObjective::FireGameEvent( IGameEvent *event )
{
	const char * type = event->GetName();

	if( Q_strcmp(type, "objective_updated") == 0 )
	{
		int objective = event->GetInt("objectiveID");
		if( objective != m_iObjective )
			return;

		switch( event->GetInt("type")  )
		{
		case OBJECTIVE_COMPLETED:
		case OBJECTIVE_FAILED:
		case OBJECTIVE_HIDE:
			SetVisible( false );
			break;
		case OBJECTIVE_SETVISIBLE:
			SetVisible( true );
			break;
		default:
			break;
		}
	}
}

void C_EnvRTSObjective::SetVisible( bool visible )
{
	if( m_bVisible == visible )
		return;

	if( visible ) 
	{
		g_EnvRTSObjectiveList.Insert( this );
		m_bVisible = true;
	}
	else
	{
		g_EnvRTSObjectiveList.Remove( this );
		m_bVisible = false;
	}
}

//---------------------------- capture point visual functions
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
C_CapturePoint::C_CapturePoint( void )
{
	g_CapturePointList.Insert( this );
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
C_CapturePoint::~C_CapturePoint( void )
{
	g_CapturePointList.Remove( this );
}

int C_CapturePoint::GetTeamNumber() const
{
	return m_iTeamOwnership;
}


