/**
* Melee Hud Text Printouts
* @version 2-18-04
* @author Joseph 'Pcjoe' Florencio, VGUI update by harSens
*/

#include "hud.h"
#include "parsemsg.h"
#include "const.h"
#include "cl_util.h"
#include "r_efx.h"
#include "pm_defs.h"
#include "pmtrace.h"
#include "event_api.h"
#include "triangleapi.h"
#include "math.h"
#include "vgui_int.h"
#include "vgui_TeamFortressViewport.h"
#include "vgui_meleetext.h"

using namespace std;

DECLARE_MESSAGE(m_MeleeHudText, MeleeTxtHud);

int CHudMeleeText::Init(void)
{	HOOK_MESSAGE(MeleeTxtHud);
	
	Reset();

	gHUD.AddHudElem(this);
	m_iFlags |= HUD_ACTIVE;

	return 1;
}

void CHudMeleeText::Reset(void)
{	if(gViewPort)
	{	gViewPort->RemoveAllMeleeText();
	}
}

/*
* Add Element
*/
void CHudMeleeText::AddElement( int iType, int iNum )
{	// Set element specific properties
	float flStartTime = gHUD.m_flTime;
	float flEndTime;
	int iStartX;
	int iStartY;
	int iEndX;
	int iEndY;
	int iStartR;
	int iStartG;
	int iStartB;
	int iEndR;
	int iEndG;
	int iEndB;

	string sText="";

	switch(iType)
	{
		case MELEETXT_HIT:
		case MELEETXT_HITS:
			{
				// Set Time
				flEndTime = gHUD.m_flTime + 2;

				char buffer[32];
				sprintf(buffer,"%d",iNum);
				sText = buffer;
				sText.append(" ");
				
				// Set Sprite
				if(iType == MELEETXT_HIT)
				{	sText.append(CHudTextMessage::BufferedLocaliseTextString("#Hit")); 
				}
				else
				{	sText.append(CHudTextMessage::BufferedLocaliseTextString("#Hits"));
				}
								
				// Set XY
				iStartX = ScreenWidth / 4;
				iStartY = ScreenHeight / 4;
				iEndX = iStartX;
				iEndY = 0;

				// Set RGB
				iStartR = 255;
				iStartG = 0;
				iStartB = 0;
				iEndR = 255;
				iEndG = 255;
				iEndB = 255;
				break;
			}			
		case MELEETXT_COMBO:
			{
				// Set Time
				flEndTime = gHUD.m_flTime + 1.0;

				// Set Sprite
				sText = CHudTextMessage::BufferedLocaliseTextString("#Combo");

				// Set XY
				iStartX = ScreenWidth/3;
				iStartY = ScreenHeight/2;
				iEndX = iStartX;
				iEndY = iStartY-ScreenHeight/20;

				// Set RGB
				iStartR = 0;
				iStartG = 0;
				iStartB = 255;
				iEndR = 255;
				iEndG = 255;
				iEndB = 255;
				
				break;
			}
		case MELEETXT_BLOCKBREAK:
			{
				// Set Time
				flEndTime = gHUD.m_flTime + 1.0;

				// Set Sprite
				sText = CHudTextMessage::BufferedLocaliseTextString("#BlockBreak");

				// Set XY
				iStartX = ScreenWidth/3;
				iStartY = ScreenHeight/2;
				iEndX = iStartX;
				iEndY = iStartY-ScreenHeight/20;

				// Set RGB
				iStartR = 255;
				iStartG = 155;
				iStartB = 0;
				iEndR = 255;
				iEndG = 255;
				iEndB = 255;
				
				break;
			}
		case MELEETXT_DAMAGE:
			{
				// Set Time
				flEndTime = gHUD.m_flTime + 2.0;

				char buffer[32];
				sprintf(buffer,"%d",iNum);
				sText = buffer;
				sText.append(" ");

				// Set Sprite
				sText.append(CHudTextMessage::BufferedLocaliseTextString("#Damage"));

				// Set XY
				iStartX = ScreenWidth/2;
				iStartY = ScreenHeight/2;
				iEndX = iStartX;
				iEndY = ScreenHeight/4;

				// Set RGB
				iStartR = 255;
				iStartG = 0;
				iStartB = 0;
				iEndR = 255;
				iEndG = 255;
				iEndB = 255;
				break;
			}
		case MELEETXT_SWORDKILL:
			{
				// Set Time
				flEndTime = gHUD.m_flTime + 1.0;

				// Set Sprite
				sText.append(CHudTextMessage::BufferedLocaliseTextString("#SwordKill"));

				// Set XY
				iStartX = ScreenWidth/3;
				iStartY = ScreenHeight/2;
				iEndX = iStartX;
				iEndY = iStartY-ScreenHeight/20;

				// Set RGB
				iStartR = 255;
				iStartG = 0;
				iStartB = 0;
				iEndR = 255;
				iEndG = 255;
				iEndB = 255;
				break;
			}
			/*
		case MELEETXT_CRITICAL:
		case MELEETXT_PERDAMAGE:
		case MELEETXT_LOWKI:
		case MELEETXT_LOWHP:
		case MELEETXT_DEEPSHIT:
			*/
		case MELEETXT_DODGED:
			{
				// Set Time
				flEndTime = gHUD.m_flTime + 2.0;

				// Set Sprite
				sText = CHudTextMessage::BufferedLocaliseTextString("#Dodged");

				// Set XY
				iStartX = ScreenWidth/2;
				iStartY = ScreenHeight/2;
				iEndX = iStartX;
				iEndY = ScreenHeight/4;

				// Set RGB
				iStartR = 255;
				iStartG = 155;
				iStartB = 0;
				iEndR = 255;
				iEndG = 255;
				iEndB = 255;				
				break;
			}
		case MELEETXT_WINNER:
			{	
				// Set Time
				flEndTime = gHUD.m_flTime + 4.0;

				// Set Sprite
				sText = CHudTextMessage::BufferedLocaliseTextString("#Winner");

				// Set XY
				iStartX = ScreenWidth / 4;
				iStartY = ScreenHeight / 4;
				iEndX = iStartX;
				iEndY = ScreenHeight / 4;

				// Set RGB
				iStartR = 0;
				iStartG = 0;
				iStartB = 255;
				iEndR = 255;
				iEndG = 255;
				iEndB = 255;				
				break;
			}
		case MELEETXT_LOOSER:
			{
				// Set Time
				flEndTime = gHUD.m_flTime + 4.0;

				// Set Sprite
				sText = CHudTextMessage::BufferedLocaliseTextString("#Loser");

				// Set XY
				iStartX = ScreenWidth / 4;
				iStartY = ScreenHeight / 4;
				iEndX = iStartX;
				iEndY = ScreenHeight / 4;

				// Set RGB
				iStartR = 255;
				iStartG = 0;
				iStartB = 0;
				iEndR = 255;
				iEndG = 255;
				iEndB = 255;				
				break;
			}
		case MELEETXT_TIE:
			{
				// Set Time
				flEndTime = gHUD.m_flTime + 4.0;

				// Set Sprite
				sText = CHudTextMessage::BufferedLocaliseTextString("#Tie");

				// Set XY
				iStartX = ScreenWidth / 4;
				iStartY = ScreenHeight / 4;
				iEndX = iStartX;
				iEndY = iStartY;

				// Set RGB
				iStartR = 255;
				iStartG = 155;
				iStartB = 0;
				iEndR = 255;
				iEndG = 255;
				iEndB = 255;				
				break;
			}
		case MELEETXT_HEADON:
			{
				// Set Time
				flEndTime = gHUD.m_flTime + 4.0;

				// Set Sprite
				sText = CHudTextMessage::BufferedLocaliseTextString("#Headon");

				// Set XY
				iStartX = ScreenWidth / 4;
				iStartY = ScreenHeight / 3;
				iEndX = iStartX;
				iEndY = iStartY;

				// Set RGB
				iStartR = 0;
				iStartG = 0;
				iStartB = 255;
				iEndR = 255;
				iEndG = 255;
				iEndB = 255;				
				break;
			}
		case MELEETXT_KNOCKBACK:
			{
				// Set Time
				flEndTime = gHUD.m_flTime + 4.0;

				// Set Sprite
				sText = CHudTextMessage::BufferedLocaliseTextString("#Knockback");

				// Set XY
				iStartX = ScreenWidth / 4;
				iStartY = ScreenHeight / 2;
				iEndX = iStartX;
				iEndY = iStartY;

				// Set RGB
				iStartR = 255;
				iStartG = 0;
				iStartB = 0;
				iEndR = 255;
				iEndG = 255;
				iEndB = 255;				
				break;
			}
		case MELEETXT_PREPUNCH:
			{
				// Set Time
				flEndTime = gHUD.m_flTime + 4.0;

				// Set Sprite
				sText = CHudTextMessage::BufferedLocaliseTextString("#Prepunch");

				// Set XY
				iStartX = ScreenWidth / 4;
				iStartY = ScreenHeight / 2;
				iEndX = iStartX;
				iEndY = iStartY;

				// Set RGB
				iStartR = 255;
				iStartG = 0;
				iStartB = 0;
				iEndR = 255;
				iEndG = 255;
				iEndB = 255;			
				break;
			}
		default:
			return;
			
	}
	gViewPort->AddMeleeText(sText,flStartTime, flEndTime,iStartX,iStartY,iEndX,iEndY,iStartR,iStartG,iStartB,iEndR,iEndG,iEndB);	
}

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

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

	gViewPort->UpdateMeleeText(flTime);

	return 1;
}

int CHudMeleeText::MsgFunc_MeleeTxtHud( const char *pszName, int iSize, void *pbuf )
{	BEGIN_READ( pbuf, iSize );

	int iType = READ_BYTE();
	int iNum = READ_BYTE();

	AddElement(iType, iNum);

	return 1;
}
