//-----------------------------------------------------------------------------
// Set the appropriate texture...
//-----------------------------------------------------------------------------
void CObjectBuildAlphaProxy::OnBind( C_BaseEntity *pEntity )
{
	if( !m_pAlphaVar )
		return;
	
	// It needs to be a TF2 C_BaseObject to have this proxy applied
	C_BaseObject *pObject = dynamic_cast< C_BaseObject * >( pEntity );
	if ( !pObject )
		return;

	float build_amount = pObject->m_flCycle; //pObject->GetPercentageConstructed();
	float frac;

	if ( build_amount <= buildstart )
	{
		frac = 0.0f;
	}
	else if ( build_amount >= buildend )
	{
		frac = 1.0f;
	}
	else
	{
		// Avoid div by zero
		if ( buildend == buildstart )
		{
			frac = 1.0f;
		}
		else
		{
			frac = ( build_amount - buildstart ) / ( buildend - buildstart );
			frac = clamp( frac, 0.0f, 1.0f );
		}
	}

	if ( !pObject->IsBuilding() )
	{
		frac = 1.0f;
	}

	m_pAlphaVar->SetFloatValue( frac );
}
//-----------------------------------------------------------------------------
// Purpose: Calc visibility of subpanels
//-----------------------------------------------------------------------------
void CBuildingStatusItem::PerformLayout( void )
{
	BaseClass::PerformLayout();

	C_BaseObject *pObj = m_pObject.Get();

	m_bActive = ( pObj != NULL );

	m_pHealthBar->SetVisible( m_bActive );

	m_pNotBuiltPanel->SetVisible( !m_bActive );
	m_pBuiltPanel->SetVisible( m_bActive );

	if ( pObj )
	{
		// redo the background
		m_pBackground->SetIcon( GetBackgroundImage() );

		if ( pObj->IsBuilding() )
		{
			m_pBuildingPanel->SetVisible( true );
			m_pRunningPanel->SetVisible( false );
		}
		else
		{
			m_pBuildingPanel->SetVisible( false );
			m_pRunningPanel->SetVisible( true );
		}
	}
	else
	{
		// redo the background
		m_pBackground->SetIcon( GetInactiveBackgroundImage() );

		if ( m_pAlertTray->IsTrayOut() )
		{
			m_pAlertTray->HideTray();
			m_pWrenchIcon->SetVisible( false );
			m_pSapperIcon->SetVisible( false );
		}
	}
}
//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CBuildingStatusItem::OnTick()
{
	// We only tick while active and with a valid built object
	C_BaseObject *pObj = GetRepresentativeObject();

	if ( !pObj )	// implies not active
	{
		if ( m_bActive )
		{
			// we lost our object. force relayout to inactive mode
			InvalidateLayout();

			// tell our parent that we're gone
			IGameEvent *event = gameeventmanager->CreateEvent( "building_info_changed" );
			if ( event )
			{
				event->SetInt( "building_type", GetRepresentativeObjectType() );
				gameeventmanager->FireEventClientSide( event );
			}
		}

		// We don't want to tick while inactive regardless
		return;
	}

	float flHealth = (float)pObj->GetHealth() / (float)pObj->GetMaxHealth();

	m_pHealthBar->SetProgress( flHealth );

	if ( pObj->IsBuilding() )
	{
		m_pBuildingPanel->SetVisible( true );
		m_pRunningPanel->SetVisible( false );

		m_pBuildingProgress->SetProgress( pObj->GetPercentageConstructed() );
	}
	else
	{
		m_pBuildingPanel->SetVisible( false );
		m_pRunningPanel->SetVisible( true );
	}

	// what is our current alert state?
	BuildingHudAlert_t alertLevel = pObj->GetBuildingAlertLevel();

	if ( alertLevel <= BUILDING_HUD_ALERT_NONE )
	{
		// if the tray is out, hide it
		if ( m_pAlertTray->IsTrayOut() )
		{
			m_pAlertTray->HideTray();
			m_pWrenchIcon->SetVisible( false );
			m_pSapperIcon->SetVisible( false );
		}
	}
	else
	{
		if ( !m_pAlertTray->IsTrayOut() )
		{
			m_pAlertTray->ShowTray();
		}

		m_pAlertTray->SetAlertType( alertLevel );

		m_pWrenchIcon->SetVisible( false );
		m_pSapperIcon->SetVisible( false );

		if ( m_pAlertTray->GetAlertType() != BUILDING_HUD_ALERT_NONE &&
			m_pAlertTray->GetPercentDeployed() >= 1.0f )
		{
			switch( m_pAlertTray->GetAlertType() )
			{
			case BUILDING_HUD_ALERT_LOW_AMMO:
			case BUILDING_HUD_ALERT_LOW_HEALTH:
			case BUILDING_HUD_ALERT_VERY_LOW_AMMO:
			case BUILDING_HUD_ALERT_VERY_LOW_HEALTH:
				m_pWrenchIcon->SetVisible( true );
				break;

			case BUILDING_HUD_ALERT_SAPPER:
				m_pSapperIcon->SetVisible( true );
				break;

			case BUILDING_HUD_ALERT_NONE:
			default:
				break;
			}
		}
	}	
}