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

#include "cbase.h"
#include "hud.h"
#include "hl2wars/hud_unitvisuals.h"
#include <vgui_controls/Panel.h>
#include "hl2wars/fx_quad2.h"
#include <vgui/ISurface.h>
#include <vgui/IScheme.h>
#include "hl2wars/c_hl2wars_player.h"
#include "c_ai_basenpc.h"
#include "iclientmode.h"
#include "hl2wars/c_rts_globalresource.h"
#include "hl2wars/c_rts_buildings.h"
#include "beam_shared.h"
//#include "hl2wars/hud_mousecursor.h"
//#include "fx_line.h"
#include "ClientEffectPrecacheSystem.h"

#include "view.h"

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------

DECLARE_HUDELEMENT( CHudUnitVisuals );

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

	SetHiddenBits( HIDEHUD_RTS | HIDEHUD_PLAYERDEAD );

	SetZPos( 1000 );


	SetPaintBackgroundEnabled( false );
	SetPaintBorderEnabled( false );

	m_pDrawSelectionB = NULL;
}

void CHudUnitVisuals::ApplySchemeSettings( vgui::IScheme *scheme )
{
	BaseClass::ApplySchemeSettings( scheme );

	SetPaintBackgroundEnabled( false );

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

/*void CHudUnitVisuals::LevelInit( void )
{
	for( int i = 1; i < MAXSELECTED; i++ )
	{
		if( m_quads[i] )
		{
			m_quads[i]->DeActivate();
			m_quads[i] = NULL;
		}
	}

	m_pDrawSelectionB = NULL;
}*/

void CHudUnitVisuals::Reset( void )
{
	/*for( int i = 0; i < MAXSELECTED; i++ )
	{
		if( m_quads[i] )
		{
			m_quads[i]->DeActivate();
			m_quads[i] = NULL;
		}
	}*/

	m_pDrawSelectionB = NULL;
}

void CHudUnitVisuals::Paint( void )
{
	C_HL2Wars_Player* pPlayer = C_HL2Wars_Player::GetLocalHL2WarsPlayer();
	if( !pPlayer )
		return;

	const Vector color = g_RTSR->GetColor( pPlayer->GetTeamRTSID() );
	switch( pPlayer->GetSelectionType() ) {
		case SELECTION_UNIT:
			/*if( m_pDrawSelectionB ) 
			{
				if( m_pDrawSelectionB->IsRTSBrush() )
				{
					C_BuildingBrush *clear = dynamic_cast<C_BuildingBrush *> (m_pDrawSelectionB.Get());
					if( clear )
						clear->SetDrawSelection( false );
					}
				else if( m_pDrawSelectionB->IsRTSModel() )
				{
					C_BaseBuildingModel *clearM = dynamic_cast<C_BaseBuildingModel *> (m_pDrawSelectionB.Get());
					if( clearM && clearM->IsAlive() )
						clearM->SetDrawSelection( false );
				}

				m_pDrawSelectionB = NULL;
			}	*/

			for( int i = 0; i < MAXSELECTED; i++) 
			{
				if( pPlayer->m_HLWLocal.m_hSelectedUnits[i] ) 
				{
					DrawHealthBar( pPlayer->m_HLWLocal.m_hSelectedUnits[i].Get());
				}
			}

			/*for( int i = 0; i < MAXSELECTED; i++) 
			{
				if( pPlayer->m_HLWLocal.m_hSelectedUnits[i] ) 
				{
					if( m_quads[i] )
					{
						if( m_quads[i]->GetFollowEntity() != (C_BaseEntity *)pPlayer->m_HLWLocal.m_hSelectedUnits[i].Get() ) 
						{
							m_quads[i]->DeActivate();
							m_quads[i] = NULL;
						}
					}

					DrawSelectionCircle( pPlayer->m_HLWLocal.m_hSelectedUnits[i].Get(), color, 
						pPlayer->m_HLWLocal.m_hSelectedUnits[i].Get()->GetAbsOrigin(), i, true );
					DrawHealthBar( pPlayer->m_HLWLocal.m_hSelectedUnits[i].Get());
				}		
				else 
				{
					if( m_quads[i] )
					{
						 m_quads[i]->DeActivate();
						 m_quads[i] = NULL;
					}
				}
			}*/
			break;
		case SELECTION_BUILDING:
			if( pPlayer->GetSelectedBuilding() && pPlayer->GetSelectedBuilding()->IsRTSModel() ) 
			{
				DrawHealthBar( pPlayer->GetSelectedBuilding() );
			}
			/*if( pPlayer->GetSelectedBuilding() ) 
			{
				if( pPlayer->GetSelectedBuilding()->IsRTSBrush() )
				{
					C_BuildingBrush *pBuilding = dynamic_cast<C_BuildingBrush *> (pPlayer->GetSelectedBuilding());
					if( pBuilding )
					{
						if( m_pDrawSelectionB && m_pDrawSelectionB != pBuilding )		// clean up old one, if any
						{
							C_BuildingBrush *clear = dynamic_cast<C_BuildingBrush *> (m_pDrawSelectionB.Get());
							if( clear )
								clear->SetDrawSelection( false );

							C_BaseBuildingModel *clearM = dynamic_cast<C_BaseBuildingModel *> (m_pDrawSelectionB.Get());
							if( clearM )
								clearM->SetDrawSelection( false );

							m_pDrawSelectionB = NULL;
						}

						pBuilding->SetDrawSelection( true );
						m_pDrawSelectionB = pBuilding;

						return;
					}
				}
				else if( pPlayer->GetSelectedBuilding()->IsRTSModel() )
				{
					C_BaseBuildingModel *pBuildingM = dynamic_cast<C_BaseBuildingModel *> (pPlayer->GetSelectedBuilding() );
					if( pBuildingM && pBuildingM->IsAlive() )
					{
						if( m_pDrawSelectionB && m_pDrawSelectionB != pBuildingM )		// clean up old one, if any
						{
							C_BuildingBrush *clear = dynamic_cast<C_BuildingBrush *> (m_pDrawSelectionB.Get());
							if( clear )
								clear->SetDrawSelection( false );

							C_BaseBuildingModel *clearM = dynamic_cast<C_BaseBuildingModel *> (m_pDrawSelectionB.Get());
							if( clearM )
								clearM->SetDrawSelection( false );

							m_pDrawSelectionB = NULL;
						}

						pBuildingM->SetDrawSelection( true );
						m_pDrawSelectionB = pBuildingM;
					}
				}
			}*/

			/*for( int i = 0; i < MAXSELECTED; i++ )
			{
				if( m_quads[i] )
				{
					m_quads[i]->DeActivate();
					m_quads[i] = NULL;
				}
			}*/

			break;
		default:
			DevMsg("Invalid selection type! \n");
			break;
	}

	// -------------- test particles instead of those quads
	/*switch( pPlayer->GetSelectionType() ) {
		case SELECTION_UNIT:

			for( int i = 0; i < MAXSELECTED; i++) 
			{
				if( pPlayer->m_HLWLocal.m_hSelectedUnits[i] ) 
				{
					if( m_hEffect[i] )
					{
						if( m_hEffect[i]->GetControlPointEntity(0) != (C_BaseEntity *)pPlayer->m_HLWLocal.m_hSelectedUnits[i].Get() ) 
						{
							m_hEffect[i]->StopEmission();
							m_hEffect[i] = NULL;		
						}
					}

					m_hEffect[i] = ParticleProp()->Create( "vortigaunt_hand_glow", PATTACH_ABSORIGIN_FOLLOW );
					m_hEffect[i]->SetControlPointEntity( 0, pPlayer->m_HLWLocal.m_hSelectedUnits[i] );

					//DrawSelectionCircle( pPlayer->m_HLWLocal.m_hSelectedUnits[i].Get(), color, 
						//pPlayer->m_HLWLocal.m_hSelectedUnits[i].Get()->GetAbsOrigin(), i, true );
					//DrawHealthBar( pPlayer->m_HLWLocal.m_hSelectedUnits[i].Get());
				}		
				else 
				{
					if( m_hEffect[i] )
					{
						m_hEffect[i]->StopEmission();
						m_hEffect[i] = NULL;		 
					}
				}
			}
			break;
	}*/

	// --------------- end test

	DrawMouseOverEntity();
}

void CHudUnitVisuals::DrawMouseOverEntity( void )
{
	/*CHudElement *pEl = gHUD.FindElement( "CHudMouseCursor" );
	if( pEl )
	{
		CHudMouseCursor *MC = dynamic_cast<CHudMouseCursor *>(pEl);
		if( MC )
		{
			if( MC->GetMouseOverEntity() )
			{
				if( MC->GetMouseOverEntity()->IsNPC() )
					DrawHealthBar( MC->GetMouseOverEntity()->MyNPCPointer() );
				else if( MC->GetMouseOverEntity()->IsRTSModel() )
				{
					C_BaseBuildingModel *pBuilding = dynamic_cast<C_BaseBuildingModel *>(MC->GetMouseOverEntity());
					if( pBuilding )
						DrawHealthBar(pBuilding);
				}
			}
		}
	}*/
}

//-----------------------------------------------------------------------------
// Purpose: Draw selection circle
//-----------------------------------------------------------------------------
/*void CHudUnitVisuals::DrawSelectionCircle( C_BaseEntity *pTarget, const Vector color, Vector origin, int i, bool moving ) 
{
	if( m_quads[i] )	// quads are updated internal
		return;

	// get size of quad
	Vector mins, maxs; 
	ICollideable* pCol = pTarget->GetCollideable(); 
	mins = pCol->OBBMins();
	maxs = pCol->OBBMaxs();
	float size = (maxs - mins).Length2D() + 10.0f; 

	// get position of quad	
	const QAngle normalAngle = pTarget->GetAbsAngles();	    
	Vector normal, stub, stub2;       
	AngleVectors(normalAngle, &stub, &stub2, &normal);	    
	origin[2] += 1.0f; // let it fly just a little bit above the ground		

	//IMaterial *pMaterial = materials->FindMaterial( QUAD_TO_SELECT_NEW, NULL, false );
	//materials->Bind( pMaterial );

	//Msg("%f %f %f \n", color[0], color[1], color[2]);
	m_quads[i] = FX_AddQuad2(origin, normal, size, 1.0f,    
		0.0f, 0.0f, color, pTarget, QUAD_TO_SELECT, 0, moving); 

}
*/

//-----------------------------------------------------------------------------
// Purpose: Draw health bar
//-----------------------------------------------------------------------------
void CHudUnitVisuals::DrawHealthBar( C_BaseEntity *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 )
		);
}
//-----------------------------------------------------------------------------
// Purpose: Draw health bar
//-----------------------------------------------------------------------------
/*void CHudUnitVisuals::DrawHealthBar( C_AI_BaseNPC *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 )
	);
}

//-----------------------------------------------------------------------------
// 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 )
		);
}*/