/*===============================================================
	Client

	From sf194. Has problems with newer SDK. Not sure why.

	Last Updated Never
===============================================================*/


#include "cbase.h"

//#define SF194_CRAP 
#ifdef SF194_CRAP // This is not defined cause sf194 sucks

#include "hud.h"
#include "hud_freeze.h"
#include "hud_macros.h"
#include "c_basehlplayer.h"
#include "c_sdk_player.h"
#include "iclientmode.h"
#include <vgui_controls/AnimationController.h>
#include <vgui/ISurface.h>
#include <vgui/ILocalize.h>

#include "c_playerresource.h" // the good team colors

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

DECLARE_HUDELEMENT( CHudFreeze );
DECLARE_HUD_MESSAGE( CHudFreeze, Freeze );

#define SUITPOWER_INIT -1

//-----------------------------------------------------------------------------
// Purpose: Constructor
//-----------------------------------------------------------------------------
CHudFreeze::CHudFreeze( const char *pElementName ) : CHudElement( pElementName ),
	vgui::Panel(g_pClientMode->GetViewport(), "HudFreeze")
{
	SetHiddenBits( HIDEHUD_HEALTH | HIDEHUD_PLAYERDEAD | HIDEHUD_NEEDSUIT );
	SetVisible( false );
	SetEnabled( false );

	m_iBlockTeam = 0;
	m_iPlayerTeam = 0;
	m_bOn = false;
	m_bUnfreezing = false;
}

//-----------------------------------------------------------------------------
// Purpose: Initialization (copy of the construtor)
//-----------------------------------------------------------------------------
void CHudFreeze::Init( void )
{
	HOOK_HUD_MESSAGE( CHudFreeze, Freeze );
	m_flFreezePercent = SUITPOWER_INIT;

	SetVisible( false );
	SetEnabled( false );

	m_iBlockTeam = 0;
	m_iPlayerTeam = 0;
	m_bOn = false;
	m_bUnfreezing = false;
}

//-----------------------------------------------------------------------------
// Purpose: Initiailization (copy of the constructor)
//-----------------------------------------------------------------------------
void CHudFreeze::Reset( void )
{
	Init();
}

bool CHudFreeze::ShouldDraw()
{
	C_SDKPlayer *pPlayer = C_SDKPlayer::GetLocalSDKPlayer();
	if ( !pPlayer )
		return false;
	if ( pPlayer->State_Get() != STATE_ACTIVE )
		return false;

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: draws the freeze bar
//-----------------------------------------------------------------------------
void CHudFreeze::Paint()
{
	// JNighthawk 4-4-02 - I'm slowly rewriting this function to be less a direct copy of the HUD power suit
	// Terms: Disabled chunks = right side of progress bar, enabled chunks = left side of progress bar
	if ( !m_bOn )
		return;

	// get bar chunks
	static int chunkCount = m_flBarWidth / (m_flBarChunkWidth + m_flBarChunkGap);
	int enabledChunks = (int)((float)chunkCount * (m_flFreezePercent / 100.0f) + 0.5f );

	// draw the panel
	// Set the color of the enabled chunks
	vgui::surface()->DrawSetColor(GameResources()->GetTeamColor(m_iBlockTeam));

	// Draw the enabled chunks
	int xpos = m_flBarInsetX, ypos = m_flBarInsetY;
	for (int i = 0; i < enabledChunks; i++)
	{
		vgui::surface()->DrawFilledRect( xpos, ypos, xpos + m_flBarChunkWidth, ypos + m_flBarHeight );
		xpos += (m_flBarChunkWidth + m_flBarChunkGap);
	}

	// draw the exhausted portion of the bar.
	if (m_iBlockTeam == m_iPlayerTeam)
	{
		vgui::surface()->DrawSetColor(Color(GameResources()->GetTeamColor(m_iBlockTeam)[0], GameResources()->GetTeamColor(m_iBlockTeam)[1], GameResources()->GetTeamColor(m_iBlockTeam)[2], m_iAuxPowerDisabledAlpha));
	}
	else
	{
		vgui::surface()->DrawSetColor(GameResources()->GetTeamColor(m_iPlayerTeam));
	}

	// Draw the disabled chunks
	for (int i = enabledChunks; i < chunkCount; i++)
	{
		vgui::surface()->DrawFilledRect( xpos, ypos, xpos + m_flBarChunkWidth, ypos + m_flBarHeight );
		xpos += (m_flBarChunkWidth + m_flBarChunkGap);
	}
}

void CHudFreeze::MsgFunc_Freeze( bf_read &msg )
{
	m_bOn = msg.ReadByte();
	m_flFreezePercent = msg.ReadFloat();
	
	m_iBlockTeam = msg.ReadByte();
	m_iPlayerTeam = msg.ReadByte();
	m_bUnfreezing = msg.ReadByte();

	SetEnabled( m_bOn );
	SetPaintEnabled( m_bOn );
	SetPaintBackgroundEnabled( m_bOn );
}

#endif