//=========== (C) Copyright 1999 Valve, L.L.C. All rights reserved. ===========
//
// The copyright to the contents herein is the property of Valve, L.L.C.
// The contents may be used and/or copied only with the written permission of
// Valve, L.L.C., or in accordance with the terms and conditions stipulated in
// the agreement/contract under which the contents have been supplied.
//
// Purpose: TFC Class Menu 
//
// $Workfile:     $
// $Date: 1997/04/07 00:45:46 $
//
//-----------------------------------------------------------------------------
// $Log: vgui_teamclassmenu.cpp,v $
// Revision 1.7  1997/04/07 00:45:46  joe
// no message
//
// Revision 1.6  2005/04/03 10:14:50  harsens
// a18 added
//
// Revision 1.5  2005/01/30 17:50:49  joe
// no message
//
// Revision 1.4  2004/12/04 11:37:26  harsens
// ginyu added
//
// Revision 1.3  2004/03/26 14:02:32  harsens
// removed shortcut keys for invalid classes
//
// Revision 1.2  2003/07/01 17:30:44  harsens
// *** empty log message ***
//
//
// $NoKeywords: $
//=============================================================================

#include "VGUI_Font.h"
#include <VGUI_TextImage.h>

#include "hud.h"
#include "cl_util.h"
#include "camera.h"
#include "kbutton.h"
#include "cvardef.h"
#include "usercmd.h"
#include "const.h"
#include "camera.h"
#include "in_defs.h"
#include "parsemsg.h"

#include "vgui_int.h"
#include "vgui_TeamFortressViewport.h"
#include "vgui_ServerBrowser.h"

// Class Menu Dimensions
#define CLASSMENU_TITLE_X				XRES(40)
#define CLASSMENU_TITLE_Y				YRES(32)
#define CLASSMENU_TOPLEFT_BUTTON_X		XRES(40)
#define CLASSMENU_TOPLEFT_BUTTON_Y		YRES(80)
#define CLASSMENU_BUTTON_SIZE_X			XRES(124)
#define CLASSMENU_BUTTON_SIZE_Y			YRES(24)
#define CLASSMENU_BUTTON_SPACER_Y		YRES(8)
#define CLASSMENU_WINDOW_X				XRES(176)
#define CLASSMENU_WINDOW_Y				YRES(80)
#define CLASSMENU_WINDOW_SIZE_X			XRES(424)
#define CLASSMENU_WINDOW_SIZE_Y			YRES(312)
#define CLASSMENU_WINDOW_TEXT_X			XRES(150)
#define CLASSMENU_WINDOW_TEXT_Y			YRES(80)
#define CLASSMENU_WINDOW_NAME_X			XRES(150)
#define CLASSMENU_WINDOW_NAME_Y			YRES(8)
#define CLASSMENU_WINDOW_PLAYERS_Y		YRES(42)

#define GOOD_TEAM_LENGTH 8
#define EVIL_TEAM_LENGTH 7
#define MAX_TEAM_LENGTH 8
char *g_pGoodClasses[]=
{	"none",
	"goku",
	"gohan",
	"krillin",
	"piccolo",
	"trunks",
	"a18",
	"randompc",
};

char *g_pEvilClasses[]=
{	"none",
	"buu",
	"frieza",
	"vegeta",
	"cell",
	"ginyu",
	"randompc",
};

char *g_pGoodLocalisedClasses[] = 
{	"#None",
	"#Goku",
	"#Gohan",
	"#Krillin",
	"#Piccolo",
	"#Trunks",
	"#A18",
	"#Randompc",	
};

char *g_pEvilLocalisedClasses[] = 
{	"#None",
	"#Buu",
	"#Frieza",
	"#Vegeta",
	"#Cell",
	"#Ginyu",
	"#Randompc",	
};

// Creation
CTeamClassMenuPanel::CTeamClassMenuPanel(int iTrans, int iRemoveMe, int x,int y,int wide,int tall) : CMenuPanel(iTrans, iRemoveMe, x,y,wide,tall)
{
	// don't show class graphics at below 640x480 resolution
	bool bShowClassGraphic = true;
	if ( ScreenWidth < 640 )
	{
		bShowClassGraphic = false;
	}

	memset( m_pClassImages, 0, sizeof(m_pClassImages) );

	// Get the scheme used for the Titles
	CSchemeManager *pSchemes = gViewPort->GetSchemeManager();

	// schemes
	SchemeHandle_t hTitleScheme = pSchemes->getSchemeHandle( "Title Font" );
	SchemeHandle_t hClassWindowText = pSchemes->getSchemeHandle( "Briefing Text" );

	// color schemes
	int r, g, b, a;

	// Create the title
	Label *pLabel = new Label( "", CLASSMENU_TITLE_X, CLASSMENU_TITLE_Y );
	pLabel->setParent( this );
	pLabel->setFont( pSchemes->getFont(hTitleScheme) );
	pSchemes->getFgColor( hTitleScheme, r, g, b, a );
	pLabel->setFgColor( r, g, b, a );
	pSchemes->getBgColor( hTitleScheme, r, g, b, a );
	pLabel->setBgColor( r, g, b, a );
	pLabel->setContentAlignment( vgui::Label::a_west );
	pLabel->setText(gHUD.m_TextMessage.BufferedLocaliseTextString("#Title_SelectYourClass"));

	// Create the Scroll panel
	m_pScrollPanel = new CTFScrollPanel( CLASSMENU_WINDOW_X, CLASSMENU_WINDOW_Y, CLASSMENU_WINDOW_SIZE_X, CLASSMENU_WINDOW_SIZE_Y );
	m_pScrollPanel->setParent(this);
	//force the scrollbars on, so after the validate clientClip will be smaller
	m_pScrollPanel->setScrollBarAutoVisible(false, false);
	m_pScrollPanel->setScrollBarVisible(true, true);
	m_pScrollPanel->setBorder( new LineBorder( Color(255 * 0.7,170 * 0.7,0,0) ) );
	m_pScrollPanel->validate();

	int clientWide=m_pScrollPanel->getClient()->getWide();

	//turn scrollpanel back into auto show scrollbar mode and validate
	m_pScrollPanel->setScrollBarAutoVisible(false,true);
	m_pScrollPanel->setScrollBarVisible(false,false);
	m_pScrollPanel->validate();

	int iTeamLength;
	char pClasses[100][100];
	char pLocClasses[100][100];
	for (int j=0;j<2;j++)
	{	if(j==0)
		{	iTeamLength=GOOD_TEAM_LENGTH;
			for(int i=0;i<iTeamLength;i++)
			{	strcpy(pClasses[i],  g_pGoodClasses[i]);
				strcpy(pLocClasses[i], g_pGoodLocalisedClasses[i]);
			}
		}
		else
		{	iTeamLength=EVIL_TEAM_LENGTH;
			for(int i=0;i<iTeamLength;i++)
			{	strcpy(pClasses[i],  g_pEvilClasses[i]);
				strcpy(pLocClasses[i], g_pEvilLocalisedClasses[i]);
			}			
		}
		for (int i = 0; i < iTeamLength; i++)
		{
			char sz[256]; 
			int iYPos = CLASSMENU_TOPLEFT_BUTTON_Y + ( (CLASSMENU_BUTTON_SIZE_Y + CLASSMENU_BUTTON_SPACER_Y) * i );

			// Edited by Pcjoe, use default string command class (esf special class select uses CMenuHandler_StringCommandClassSelect
			ActionSignal *pASignal = new CMenuHandler_StringCommand( pClasses[i], true );

			// Class button
			sprintf(sz, " %s", CHudTextMessage::BufferedLocaliseTextString( pLocClasses[i] ) );
			
			//find right class nr
			int iClass=0;
			for (int k=0;k<PC_LASTCLASS;k++)
			{	if(!strcmp(sTFClassSelection[k],pClasses[i]))
				{	iClass = k;
				}
			}
			
			m_pButtons[j][i] = new ClassButton( iClass, sz, CLASSMENU_TOPLEFT_BUTTON_X, iYPos, CLASSMENU_BUTTON_SIZE_X, CLASSMENU_BUTTON_SIZE_Y, true);
		
			/*
			// RandomPC uses '0'
			if ( i >= 1 && i <= iTeamLength-2)
			{
				sprintf(sz,"%d",i);
			}
			else
			{	sprintf(sz,"0");
			}
		
			m_pButtons[j][i]->setBoundKey( sz[0] );
			*/
			m_pButtons[j][i]->setContentAlignment( vgui::Label::a_west );
			m_pButtons[j][i]->addActionSignal( pASignal );
			m_pButtons[j][i]->addInputSignal( new CHandler_MenuButtonOver(this, i) );
			m_pButtons[j][i]->setParent( this );

			// Create the Class Info Window
			m_pClassInfoPanel[j][i] = new CTransparentPanel( 255, 0, 0, clientWide, CLASSMENU_WINDOW_SIZE_Y );
			m_pClassInfoPanel[j][i]->setParent( m_pScrollPanel->getClient() );

			// don't show class pic in lower resolutions
			int textOffs = XRES(8);

			if ( bShowClassGraphic )
			{
				textOffs = CLASSMENU_WINDOW_NAME_X;
			}

			// Create the Class Name Label
			sprintf(sz, "#Title_%s", pClasses[i]);
			char* localName=CHudTextMessage::BufferedLocaliseTextString( sz );
			Label *pNameLabel = new Label( "", textOffs, CLASSMENU_WINDOW_NAME_Y );
			pNameLabel->setFont( pSchemes->getFont(hTitleScheme) ); 
			pNameLabel->setParent( m_pClassInfoPanel[j][i] );
			pSchemes->getFgColor( hTitleScheme, r, g, b, a );
			pNameLabel->setFgColor( r, g, b, a );
			pSchemes->getBgColor( hTitleScheme, r, g, b, a );
			pNameLabel->setBgColor( r, g, b, a );
			pNameLabel->setContentAlignment( vgui::Label::a_west );
			pNameLabel->setText(localName);

			// Create the Class Image
		
			if ( bShowClassGraphic )
			{	if ( j == 1 )
				{
					sprintf( sz, "%sevil", pClasses[i] );
				}
				else
				{
					sprintf( sz, "%sgood", pClasses[i] );
				}
			
				m_pClassImages[j][i] = new CImageLabel( sz, 0, 0, CLASSMENU_WINDOW_TEXT_X, CLASSMENU_WINDOW_TEXT_Y );

				CImageLabel *pLabel = m_pClassImages[j][i];
				pLabel->setParent( m_pClassInfoPanel[j][i] );
				
				if ( j != 1 )
				{
					pLabel->setVisible( false );
				}
				
				// Reposition it based upon it's size
				int xOut, yOut;
				pNameLabel->getTextSize( xOut, yOut );
				pLabel->setPos( (CLASSMENU_WINDOW_TEXT_X - pLabel->getWide()) / 2, yOut /2 );
			}
		
		
			// Create the Player count string
			gHUD.m_TextMessage.LocaliseTextString( "#Title_CurrentlyOnYourTeam", m_sPlayersOnTeamString, STRLENMAX_PLAYERSONTEAM );
			m_pPlayers[j][i] = new Label( "", textOffs, CLASSMENU_WINDOW_PLAYERS_Y );
			m_pPlayers[j][i]->setParent( m_pClassInfoPanel[j][i] );
			m_pPlayers[j][i]->setBgColor( 0, 0, 0, 255 );
			m_pPlayers[j][i]->setContentAlignment( vgui::Label::a_west );
			m_pPlayers[j][i]->setFont( pSchemes->getFont(hClassWindowText) );

			// Open up the Class Briefing File
			sprintf(sz, "classes/short_%s.txt", pClasses[i]);
			char *cText = "Class Description not available.";
			char *pfile = (char*)gEngfuncs.COM_LoadFile( sz, 5, NULL );
			if (pfile)
				cText = pfile;
			
			// Create the Text info window
			TextPanel *pTextWindow = new TextPanel(cText, textOffs, CLASSMENU_WINDOW_TEXT_Y, (CLASSMENU_WINDOW_SIZE_X - textOffs)-5, CLASSMENU_WINDOW_SIZE_Y - CLASSMENU_WINDOW_TEXT_Y);
			pTextWindow->setParent( m_pClassInfoPanel[j][i] );
			pTextWindow->setFont( pSchemes->getFont(hClassWindowText) );
			pSchemes->getFgColor( hClassWindowText, r, g, b, a );
			pTextWindow->setFgColor( r, g, b, a );
			pSchemes->getBgColor( hClassWindowText, r, g, b, a );
			pTextWindow->setBgColor( r, g, b, a );

			// Resize the Info panel to fit it all
			int wide,tall;
			pTextWindow->getTextImage()->getTextSizeWrapped( wide,tall);
			pTextWindow->setSize(wide,tall);
			//pTextWindow->setBorder(new LineBorder());
			int xx,yy;
			pTextWindow->getPos(xx,yy);
			int maxX=xx+wide;
			int maxY=yy+tall;

			//check to see if the image goes lower than the text
			//just use the red teams [0] images
			if(m_pClassImages[0][i]!=null)
			{
				m_pClassImages[j][i]->getPos(xx,yy);
				if((yy+m_pClassImages[j][i]->getTall())>maxY)
				{
					maxY=yy+m_pClassImages[j][i]->getTall();
				}
			}
			m_pClassInfoPanel[j][i]->setSize( maxX , maxY );
		}
	}

	// Create the Cancel button
	m_pCancelButton = new CommandButton( gHUD.m_TextMessage.BufferedLocaliseTextString( "#Menu_Cancel" ), CLASSMENU_TOPLEFT_BUTTON_X, 0, CLASSMENU_BUTTON_SIZE_X, CLASSMENU_BUTTON_SIZE_Y);
	m_pCancelButton->setContentAlignment( vgui::Label::a_center );	
	m_pCancelButton->setParent( this );
	m_pCancelButton->addActionSignal( new CMenuHandler_TextWindow(HIDE_TEXTWINDOW) );
	
	m_iCurrentInfo = 0;	
}


// Update
void CTeamClassMenuPanel::Update()
{	int	 iYPos = CLASSMENU_TOPLEFT_BUTTON_Y;
	int iTeam;
	int iTeamLength;
	if(g_iTeamNumber>0)
	{	iTeam = g_iTeamNumber-1;
		if(iTeam == 0)
		{	iTeamLength = GOOD_TEAM_LENGTH;
		}
		else
		{	iTeamLength = EVIL_TEAM_LENGTH;
		}
	}
	else
	{	return;
	}

	//hide all
	int iLength;
	for ( int team = 0; team < 2; team++ )
	{	if(team == 0)
		{	iLength = GOOD_TEAM_LENGTH;
		}
		else
		{	iLength = EVIL_TEAM_LENGTH;
		}
		for(int i=0;i<iLength;i++)
		{	// unset all the other images
			if ( m_pClassImages[team][i] )
			{	m_pClassImages[team][i]->setVisible( false );
			}
			if (m_pButtons[team][i])
			{	m_pButtons[team][i]->setVisible( false );
			}
		}
	}
			

	
	// Cycle through the rest of the buttons
	for (int i = 0; i < iTeamLength; i++)
	{	
		if ( m_pButtons[iTeam][i]->IsNotValid() || i == 0 )
		{	//maybe throwing in a dende here?
			m_pButtons[iTeam][i]->setVisible( false );
		}
		else
		{	m_pButtons[iTeam][i]->setPos( CLASSMENU_TOPLEFT_BUTTON_X, iYPos );
			iYPos += CLASSMENU_BUTTON_SIZE_Y + CLASSMENU_BUTTON_SPACER_Y;

			// Start with the first option up
			if (!m_iCurrentInfo)
				SetActiveInfo( i );

			// set the current team image
			if ( m_pClassImages[iTeam][i] != NULL )
			{
				m_pClassImages[iTeam][i]->setVisible( true );
			}
			m_pButtons[iTeam][i]->setVisible( true );			
		}
	}

	// If the player already has a class, make the cancel button visible
	if ( g_iPlayerClass!=PC_UNDEFINED )
	{
		m_pCancelButton->setPos( CLASSMENU_TOPLEFT_BUTTON_X, iYPos );
		m_pCancelButton->setVisible( true );
		iYPos += CLASSMENU_BUTTON_SIZE_Y + CLASSMENU_BUTTON_SPACER_Y;
	}
	else
	{
		m_pCancelButton->setVisible( false );
	}
}

//======================================
// Key inputs for the Class Menu
bool CTeamClassMenuPanel::SlotInput( int iSlot )
{
	int iMax;
	int iTeam = g_iTeamNumber-1;
	if( iTeam == 0)
	{	iMax = GOOD_TEAM_LENGTH-1;
	}
	else
	{	iMax = EVIL_TEAM_LENGTH-1;
	}

	if ( (iSlot < 0) || (iSlot >= iMax) )
		return false;
	
	//no dende yet :)
	if (iSlot==0)
	{	iSlot=iMax;
	}
	
	if ( !m_pButtons[iTeam][iSlot] )
		return false;

	if (m_pButtons[iTeam][ iSlot ]->IsNotValid())
		return false;
	
	m_pButtons[iTeam][iSlot]->fireActionSignal();
	return true;	
}

//======================================
// Update the Class menu before opening it
void CTeamClassMenuPanel::Open( void )
{	Update();
	CMenuPanel::Open();	
}

//-----------------------------------------------------------------------------
// Purpose: Called each time a new level is started.
//-----------------------------------------------------------------------------
void CTeamClassMenuPanel::Initialize( void )
{
	setVisible( false );
	m_pScrollPanel->setScrollValue( 0, 0 );
}

//======================================
// Mouse is over a class button, bring up the class info
void CTeamClassMenuPanel::SetActiveInfo( int iInput )
{
	if (g_iTeamNumber<1)
	{	ConsolePrint("Invalid team\n");
		return;
	}


	int iTeam = g_iTeamNumber-1;
	int iTeamLength;

	// Remove all the Info panels and bring up the specified one
	for(int j=0;j<2;j++)
	{	if(j == 0)
		{	iTeamLength = GOOD_TEAM_LENGTH;
		}
		else
		{	iTeamLength = EVIL_TEAM_LENGTH;
		}
		for (int i = 0; i < iTeamLength; i++)
		{
			m_pButtons[j][i]->setArmed( false );
			m_pClassInfoPanel[j][i]->setVisible( false );
		}
	}

	int iMax;
	if(iTeam == 0)
	{	iMax = GOOD_TEAM_LENGTH-1;
	}
	else
	{	iMax = EVIL_TEAM_LENGTH-1;
	}
	
	if ( iInput > iMax || iInput < 0 )
		iInput = 0;

	m_pButtons[iTeam][iInput]->setArmed( true );
	m_pClassInfoPanel[iTeam][iInput]->setVisible( true );
	m_iCurrentInfo = iInput;

	m_pScrollPanel->setScrollValue(0,0);
	m_pScrollPanel->validate();
}