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

#define YOFFSET320 2
#define YOFFSET640 4
#define MIDRING640 2
#define MIDRING320 1
DECLARE_MESSAGE(m_PowerStruggle, PowerStrug);

using namespace std;
int CHudPowerStruggle::Init(void)
{	m_fPowerStruggle = false;
	HOOK_MESSAGE(PowerStrug);
	gHUD.AddHudElem(this);
	m_iFlags |= HUD_ACTIVE;
	return 1;
}

void CHudPowerStruggle::Reset(void)
{
	m_fPowerStruggle = false;
	m_fOverPowering=false;
	m_fOverPowered=false;
}

int CHudPowerStruggle::VidInit( void )
{	m_HUD_powerstrugglebar = gHUD.GetSpriteIndex( "powerstruggle" );
	m_HUD_powerstrugglering = gHUD.GetSpriteIndex( "psring" );
	m_HUD_powerstruggleline = gHUD.GetSpriteIndex( "psline" );	
	return 1;
}

int CHudPowerStruggle::Draw(float fltime)
{	if (!m_fPowerStruggle)
	{	return 1;
	}
	
	int iWidth = gHUD.GetSpriteRect(m_HUD_powerstrugglebar).right - gHUD.GetSpriteRect(m_HUD_powerstrugglebar).left;
	int iHeigth = gHUD.GetSpriteRect(m_HUD_powerstrugglebar).bottom - gHUD.GetSpriteRect(m_HUD_powerstrugglebar).top;
	
	//draw the bar in the down right corner
	int r,g,b,x,y;
	UnpackRGB(r,g,b, RGB_WHITEISH);
	x = ScreenWidth - iWidth;
	y = ScreenHeight - iHeigth;
	SPR_Set(gHUD.GetSprite(m_HUD_powerstrugglebar), r, g, b);
	SPR_Draw(0, x, y, &gHUD.GetSpriteRect(m_HUD_powerstrugglebar));	
	
	
	//calculate line position
	iHeigth *= 0.5;

	int iOffset = m_flPowerRatio * iHeigth;
	int fDrawLine = true;
	if (ScreenWidth<640)
	{	//don't draw the line over the ring in da middle
		if (abs(iOffset) <= MIDRING320)
		{	fDrawLine=false;
		}
		iHeigth-= YOFFSET320;
	}
	else
	{	//don't draw the line over the ring in da middle
		if (abs(iOffset) <= MIDRING640)
		{	fDrawLine=false;
		}		
		iHeigth-= YOFFSET640;
	}
	SPR_Set(gHUD.GetSprite(m_HUD_powerstrugglering), r, g, b);
	SPR_DrawHoles(0, x, y+1.4*iHeigth, &gHUD.GetSpriteRect(m_HUD_powerstrugglering));
	SPR_Set(gHUD.GetSprite(m_HUD_powerstrugglering), r, g, b);
	SPR_DrawHoles(0, x, y+0.6*iHeigth, &gHUD.GetSpriteRect(m_HUD_powerstrugglering));
	
	if(fDrawLine)
	{	y = y + iHeigth + iOffset;	
		SPR_Set(gHUD.GetSprite(m_HUD_powerstruggleline), r, g, b);
		SPR_DrawHoles(0, x, y, &gHUD.GetSpriteRect(m_HUD_powerstruggleline));
	}

	if(m_flPowerRatio>0.4)
	{
		if(!m_fOverPowering)
		{
			m_fOverPowering=true;
			int iStartX = ScreenWidth/2;
			int iStartY = ScreenWidth/2;
			int iEndX = ScreenWidth/2;
			int iEndY = ScreenWidth/2;
			string sText="";
			sText = CHudTextMessage::BufferedLocaliseTextString("#overpowering");
			gViewPort->AddMeleeText(sText,fltime, fltime+10,iStartX,iStartY,iEndX,iEndY,0,0,255,0,0,0);
		}
	}
	else if(m_flPowerRatio<-0.4)
	{
		if(!m_fOverPowered)
		{
			m_fOverPowered=true;
			int iStartX = ScreenWidth/2;
			int iStartY = ScreenWidth/2;
			int iEndX = ScreenWidth/2;
			int iEndY = ScreenWidth/2;
			string sText="";
			sText = CHudTextMessage::BufferedLocaliseTextString("#overpowered");
			gViewPort->AddMeleeText(sText,fltime, fltime+10,iStartX,iStartY,iEndX,iEndY,255,0,0,0,0,0);
		}
	}
	else
	{
		m_fOverPowering=false;
		m_fOverPowered=false;
	}
	return 1;
}

int CHudPowerStruggle:: MsgFunc_PowerStrug(const char *pszName,  int iSize, void *pbuf )
{	BEGIN_READ( pbuf, iSize );
	int n = READ_CHAR();
	if (n==127)
	{	m_fPowerStruggle = false;
	}
	else
	{	m_fPowerStruggle = true;
		m_flPowerRatio = (float)n/100.0;
	}
	return 1;
}