#include "hud.h"
#include "cl_util.h"
#include "const.h"
#include "entity_state.h"
#include "cl_entity.h"
#include "vgui_TeamFortressViewport.h"
#include "vgui_scouterstats.h"

#define FONT_HEIGHT640 16
#define FONT_HEIGHT320  8
#define ARROW_LENGTH 128
#define ARROW_HEIGHT 16
#define IMAGE_HEIGHT 64
#define IMAGE_WIDTH 32
#define TEXT_WIDTH 128
#define WIDTH TEXT_WIDTH+IMAGE_WIDTH
#define HEIGHT IMAGE_HEIGHT+2*ARROW_HEIGHT
#define SCOUTER_WIDTH 256
#define HUD_HEIGHT 256

extern buddy_info_t gBuddyInfo[MAX_PLAYERS];
CScouterStats :: CScouterStats(int iTrans, int iRemoveMe, int x, int y, int wide, int tall) : CMenuPanel(iTrans, iRemoveMe, x,y,wide,tall)
{	//schemes
	CSchemeManager *pSchemes = gViewPort->GetSchemeManager();
	SchemeHandle_t hStatsScheme = pSchemes->getSchemeHandle( "Scouterstats Text" );
	m_bIsBuddy=false;
	m_iStatsEnt=0;
	m_flLastStats=0;
	m_flPlayerStatsTime=4;

	int r,g,b,a;
	
	int iFontHeight;
	if (ScreenWidth<640)
	{	iFontHeight = FONT_HEIGHT320;
	}
	else
	{	iFontHeight = FONT_HEIGHT640;
	}
	
	//arrows
	char sz[255];
	for(int i=0;i<4;i++)
	{
		sprintf( sz, "scouter_%d",i+1);
		int x=0;
		int y=0;
		switch(i)
		{
		case 0:	x=0;y=IMAGE_HEIGHT+ARROW_HEIGHT;break;
		case 1: x=0;y=0;break;
		case 2: x=WIDTH-ARROW_LENGTH;y=0;break;
		case 3: x=WIDTH-ARROW_LENGTH;y=IMAGE_HEIGHT+ARROW_HEIGHT;break;
		}
		m_pLineImage[i] = new CImageLabel( sz, x, y, ARROW_LENGTH, ARROW_HEIGHT );
		m_pLineImage[i]->setParent(this);
	}
	m_pClassImage[0]=new CImageLabel( "scouter_a18", 0, ARROW_HEIGHT, IMAGE_WIDTH, IMAGE_HEIGHT );
	m_pClassImage[0]->setParent(this);

	//name
	m_pLabelName = new Label( "", IMAGE_WIDTH, ARROW_HEIGHT);
	m_pLabelName->setParent(this);
	m_pLabelName->setFont( pSchemes->getFont(hStatsScheme) );
	pSchemes->getFgColor( hStatsScheme, r, g, b, a );
	m_pLabelName->setFgColor( r, g, b, a );
	pSchemes->getBgColor( hStatsScheme, r, g, b, a );
	m_pLabelName->setBgColor( r, g, b, a );
	m_pLabelName->setContentAlignment( vgui::Label::a_west );
	
	//class
	m_pLabelClass = new Label( "", IMAGE_WIDTH, ARROW_HEIGHT+iFontHeight);
	m_pLabelClass->setParent( this );
	m_pLabelClass->setFont( pSchemes->getFont(hStatsScheme) );
	pSchemes->getFgColor( hStatsScheme, r, g, b, a );
	m_pLabelClass->setFgColor( r, g, b, a );
	pSchemes->getBgColor( hStatsScheme, r, g, b, a );
	m_pLabelClass->setBgColor( r, g, b, a );
	m_pLabelClass->setContentAlignment( vgui::Label::a_west );
	
	
	//powerlevel
	m_pLabelPowerlevel = new Label( "", IMAGE_WIDTH, ARROW_HEIGHT+iFontHeight * 2);
	m_pLabelPowerlevel->setParent( this );
	m_pLabelPowerlevel->setFont( pSchemes->getFont(hStatsScheme) );
	pSchemes->getFgColor( hStatsScheme, r, g, b, a );
	m_pLabelPowerlevel->setFgColor( r, g, b, a );
	pSchemes->getBgColor( hStatsScheme, r, g, b, a );
	m_pLabelPowerlevel->setBgColor( r, g, b, a );
	m_pLabelPowerlevel->setContentAlignment( vgui::Label::a_west );	
}

void CScouterStats::SetNameLabel(char *name)
{	char label[255];
	sprintf(label,"Name: %s",name);
	m_pLabelName->setText(label);
}

void CScouterStats :: paint()
{
	//char buffer[255];
	//sprintf(buffer,"Stats ent: %d\n",m_iStatsEnt);
	//ConsolePrint(buffer);
	float flTime = gEngfuncs.GetClientTime();
	if (gHUD.m_Crosshair.m_iAimedAtPlayer)
	{	m_iStatsEnt = gHUD.m_Crosshair.m_iAimedAtPlayer;
		m_bIsBuddy = gHUD.m_Crosshair.m_bAimedAtBuddy;
		m_flLastStats = flTime;
	}
	else
	{	if(flTime - m_flLastStats>m_flPlayerStatsTime)
		{	m_iStatsEnt = 0;
			m_bIsBuddy = false;
		}
	}
	
	int m_fHide = false;
	if ((((m_iStatsEnt>=0)&&(m_iStatsEnt<=MAX_PLAYERS)) && (m_iStatsEnt&&g_PlayerExtraInfo[m_iStatsEnt].playerclass!=PC_A18))   || m_iStatsEnt > MAX_PLAYERS)
	{	
		cl_entity_t *pClient=gEngfuncs.GetEntityByIndex(m_iStatsEnt);
		vec3_t screen;
		/*
		char buffer[255];
		sprintf(buffer,"Clip: %d\n",gEngfuncs.pTriAPI->WorldToScreen(pClient->origin,screen));
		ConsolePrint(buffer);

		sprintf(buffer,"x: %d\n y: %d\n",(int)XPROJECT(screen.x),(int)YPROJECT(screen.y));
		ConsolePrint(buffer);
		*/
		gEngfuncs.pTriAPI->WorldToScreen(pClient->origin,screen);
		int x = XPROJECT(screen.x);
		int y = YPROJECT(screen.y);
		
		if(x>0&&x<ScreenWidth-SCOUTER_WIDTH&&y>0&&y<ScreenHeight-HUD_HEIGHT)
		{
			DrawPlayerStats(x,y);		
			m_pLabelName->setVisible(true);
			m_pLabelClass->setVisible(true);
			m_pLabelPowerlevel->setVisible(true);
			m_pClassImage[0]->setVisible(true);
		}
		else
		{
			m_fHide = true;
		}
	}	
	else 
	{
		m_fHide = true;
	}
	if(m_fHide)
	{	//setVisible( false );
		for(int i=0;i<4;i++)
		{
			m_pLineImage[i]->setVisible(false);
		}
		m_pLabelName->setVisible(false);
		m_pLabelClass->setVisible(false);
		m_pLabelPowerlevel->setVisible(false);
		m_pClassImage[0]->setVisible(false);
		return;
	}
	CMenuPanel::paint();
}

void CScouterStats::DrawPlayerStats(int x, int y)
{
	

	//if(gEngfuncs.pTriAPI->WorldToScreen(pClient->origin,screen)!=1)
	{
		int iArrowKwadrant = 0;
		

		
		y-=10;
		y-=HEIGHT;		
		if(y<0)
		{
			y+=HEIGHT+20;
			iArrowKwadrant = 1;
		}
		if(x>ScreenWidth - WIDTH - SCOUTER_WIDTH)
		{
			x-=WIDTH;
			if(iArrowKwadrant==0)
			{
				iArrowKwadrant=3;
			}
			else
			{
				iArrowKwadrant=2;
			}
		}
		
		for(int i=0;i<4;i++)
		{
			if(i==iArrowKwadrant)
			{
				m_pLineImage[i]->setVisible(true);
			}
			else
			{
				m_pLineImage[i]->setVisible(false);
			}
		}

		if ((m_bIsBuddy == false) && g_PlayerExtraInfo[m_iStatsEnt].playerclass!=PC_A18)
		{
			//int iClassWidth =  gHUD.GetSpriteRect(m_HUD_class[1]).right - gHUD.GetSpriteRect(m_HUD_class[1]).left;
			//int iClassHeight = gHUD.GetSpriteRect(m_HUD_class[1]).bottom - gHUD.GetSpriteRect(m_HUD_class[1]).top;

			//draw sprites of the class
			//int iClassY = y + iHeight * 0.5 - iClassHeight * 0.5;
			//int iClassX = x + iClassWidth;

			//UnpackRGB( r,g,b, RGB_WHITEISH );
			//SPR_Set(gHUD.GetSprite(m_HUD_class[g_PlayerExtraInfo[m_iStatsEnt].playerclass]), r, g, b );		
			//SPR_DrawHoles(0, iClassX, iClassY, &gHUD.GetSpriteRect(m_HUD_class[g_PlayerExtraInfo[m_iStatsEnt].playerclass]) );
			
			setPos(x, y);
			setSize(WIDTH, HEIGHT);
		
			//put stats in vgui panel
			SetNameLabel(g_PlayerInfoList[m_iStatsEnt].name);
			SetClassLabel(CHudTextMessage::BufferedLocaliseTextString( sLocalisedScoreboardClassNames[g_PlayerExtraInfo[m_iStatsEnt].scoreboardclass]));
			SetPowerlevelLabel(g_PlayerExtraInfo[m_iStatsEnt].powerlevel);
			//setVisible(true);			
		}
		else
		{

			//draw sprites of the class
			//int iClassY = y + iHeight * 0.5 - iClassHeight * 0.5;
			//int iClassX = x + iClassWidth;

			//UnpackRGB( r,g,b, RGB_WHITEISH );
			//SPR_Set(gHUD.GetSprite(m_HUD_class[g_PlayerExtraInfo[m_iStatsEnt].playerclass]), r, g, b );		
			//SPR_DrawHoles(0, iClassX, iClassY, &gHUD.GetSpriteRect(m_HUD_class[g_PlayerExtraInfo[m_iStatsEnt].playerclass]) );

			setPos(x,y);
			setSize(HEIGHT,WIDTH);
		
			//put stats in vgui panel
			SetNameLabel(g_PlayerInfoList[m_iStatsEnt].name);
			SetClassLabel(CHudTextMessage::BufferedLocaliseTextString( sLocalisedScoreboardClassNames[g_PlayerExtraInfo[m_iStatsEnt].scoreboardclass]));
			SetPowerlevelLabel(gBuddyInfo[m_iStatsEnt].powerlevel);
			//setVisible(true);
		}
	}
	
}

void CScouterStats::SetClassLabel(char *playerclass)
{	char label[255];
	sprintf(label,"Class: %s",playerclass);	
	m_pLabelClass->setText(label);
}

void CScouterStats::SetPowerlevelLabel(int powerlevel)
{	char pl[255];
	Int2String(powerlevel,pl);
	char label[255];
	sprintf(label,"PL: %s",pl);	
	m_pLabelPowerlevel->setText(label);
}

void CScouterStats::Int2String(int nr,char *s)
{	//convert to reverse string
	int three = 0;
	int i = 0;
	while(nr>0)
	{	if(three < 3)
		{	s[i] = '0'+(nr%10);
			nr/=10;
			three++;
		}
		else
		{	s[i] = ',';
			three = 0;
		}
		i++;
	}
	s[i]='\0';

	//reverse string
	int length = strlen(s);
	char *reverse=new char[length + 1];
	strcpy(reverse,s);
	for (i = 0;i<length;i++)
	{	s[i] = reverse[length-i-1];
	}
	delete [] reverse;
}
