#include "hud.h"
#include "parsemsg.h"
#include "const.h"
#include "cl_util.h"

DECLARE_MESSAGE(m_SensuBean, SensuBean);

int CHudSensuBean::Init(void)
{	HOOK_MESSAGE(SensuBean);
	m_iSensuBeans = 0;	

	gHUD.AddHudElem(this);
	m_iFlags |= HUD_ACTIVE;
	return 1;
}

int CHudSensuBean::VidInit(void)
{	m_HUD_beanbar = gHUD.GetSpriteIndex( "beanbar" );
	return 1;
}

int CHudSensuBean::Draw(float flTime)
{	if (!(gHUD.m_iWeaponBits & (1<<(WEAPON_SUIT)) ))
		return 1;

	if ( (gHUD.m_iHideHUDDisplay & ( HIDEHUD_WEAPONS | HIDEHUD_ALL )) )
		return 1;

	//draw bar
	int r,g,b,x,y;
	UnpackRGB(r,g,b, RGB_WHITEISH);
	
	m_iWidth = gHUD.GetSpriteRect(m_HUD_beanbar).right - gHUD.GetSpriteRect(m_HUD_beanbar).left;
	int iHeight = gHUD.GetSpriteRect(m_HUD_beanbar).bottom - gHUD.GetSpriteRect(m_HUD_beanbar).top;
	
	//y = ScreenHeight - iHeight * 3; //assume KiHeigth==HealthHeigth==PowerLevelHeigth=Transformation==1/2 * this height
	y = ScreenHeight - iHeight * 4;
	x = gHUD.m_Transform.m_iWidth;

	SPR_Set(gHUD.GetSprite(m_HUD_beanbar), r, g, b);
	SPR_Draw(0, x, y, &gHUD.GetSpriteRect(m_HUD_beanbar));

	int iDigitWidth = gHUD.GetSpriteRect(gHUD.m_HUD_number_0).right - gHUD.GetSpriteRect(gHUD.m_HUD_number_0).left;
	int iNumberWidth = gHUD.GetNumWidth( m_iSensuBeans, DHN_2DIGITS ) * iDigitWidth;
	int iNumberHeight = gHUD.GetSpriteRect(gHUD.m_HUD_number_0).bottom - gHUD.GetSpriteRect(gHUD.m_HUD_number_0).top;

	x += m_iWidth - iNumberWidth - iDigitWidth * 0.5;
	y += iHeight * 0.5 - 0.5 * iNumberHeight;
	gHUD.DrawHudNumber(x, y, DHN_DRAWZERO | DHN_2DIGITS, m_iSensuBeans, r, g, b);
	
	return 1;
}

int CHudSensuBean::MsgFunc_SensuBean(const char *pszName,  int iSize, void *pbuf )
{	BEGIN_READ( pbuf, iSize );
	m_iSensuBeans = READ_BYTE();
	return 1;
}