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


#define BAR_OFFSET640 4
#define BAR_OFFSET320 2

//DECLARE_MESSAGE(m_Ki, Ki);

extern int DrawBar(int x,int y,int width, int height, float f);

int CHudKi::Init(void)
{	//HOOK_MESSAGE(Ki);
	gHUD.AddHudElem(this);
	
	m_iFlags |= HUD_ACTIVE;
	return 1;
}

int CHudKi::VidInit(void)
{	m_HUD_mainbar = gHUD.GetSpriteIndex( "mainbar" );
	m_HUD_kiline = gHUD.GetSpriteIndex( "kiline" );
	m_HUD_kifront = gHUD.GetSpriteIndex("kifront");
	return 1;
}

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

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

	
	int r,g,b,x,y;
	UnpackRGB(r,g,b, RGB_WHITEISH);

	int iWidth = gHUD.GetSpriteRect(m_HUD_kifront).right - gHUD.GetSpriteRect(m_HUD_kifront).left;
	int iHeigth = gHUD.GetSpriteRect(m_HUD_kifront).bottom - gHUD.GetSpriteRect(m_HUD_kifront).top;
	
	y = ScreenHeight - iHeigth * 2; //assume KiHeigt==HealthHeight==PowerLevelHeight
	x = 0;
	
	SPR_Set(gHUD.GetSprite(m_HUD_kifront), r, g, b);
	SPR_Draw(0, x, y, &gHUD.GetSpriteRect(m_HUD_kifront));

	x += iWidth;	
	iWidth = gHUD.GetSpriteRect(m_HUD_mainbar).right - gHUD.GetSpriteRect(m_HUD_mainbar).left;
				
	SPR_Set(gHUD.GetSprite(m_HUD_mainbar), r, g, b);
	SPR_Draw(0, x, y, &gHUD.GetSpriteRect(m_HUD_mainbar));

	// draw line
	if (ScreenWidth<640)
	{	iWidth = iWidth - 2 * BAR_OFFSET320;
		x += BAR_OFFSET320;
	}
	else
	{	iWidth = iWidth - 2 * BAR_OFFSET640;
		x += BAR_OFFSET640;
	}
	
	int iBarFill = iWidth * g_fUser4 / 1000;
	for (int i = 0;i<iBarFill;i++)
	{	SPR_Set(gHUD.GetSprite(m_HUD_kiline), r, g, b);
		SPR_DrawHoles(0, x + i, y, &gHUD.GetSpriteRect(m_HUD_kiline));
	}

	// draw powerlevel in ki bar
	int iNumberHeigth = gHUD.GetSpriteRect(gHUD.m_HUD_comma).bottom - gHUD.GetSpriteRect(gHUD.m_HUD_comma).top;
	int iNumberWidth = gHUD.GetNumWidth( gHUD.m_PowerLevel.m_iPowerLevel, DHN_KI );
	
	//check if number fits in bar		
	if (iNumberHeigth<iHeigth && iNumberWidth < iWidth)
	{	int iNumberX = x + (iWidth - iNumberWidth) / 2;		
		int iNumberY = y + (iHeigth - iNumberHeigth) / 2;
		gHUD.DrawHudNumber(iNumberX, iNumberY, DHN_KI, gHUD.m_PowerLevel.m_iPowerLevel, r, g, b);
	}

	return 1;
}

/*
int CHudKi::MsgFunc_Ki(const char *pszName, int iSize, void *pbuf)
{	BEGIN_READ( pbuf, iSize );
	m_iKi = READ_LONG();
	return 1;
}
*/
