#ifndef GAMEROOMWINDOW_H
#define GAMEROOMWINDOW_H
#ifdef _WIN32
#pragma once
#endif

#include <vgui_controls/Frame.h>
#include <game/client/iviewport.h>
#include <vgui_controls/Panel.h>
#include "vgui/keycode.h"
#include "hl2wars/rts_shareddefs.h"
#include <vgui/IScheme.h>
#include <vgui/keycode.h>
#include <vgui_controls/Frame.h>
#include <vgui_controls/TextEntry.h>
#include "hl2wars/vgui_bitmapbuttonrts.h"
#include "VGui_BitmapImage.h"
#include "inputsystem/ButtonCode.h"

using namespace vgui;

class CBaseChatInputLine;
class CPlayerList;
class CMapSelector;

class CGameRoomWindow : public Frame, public IViewPortPanel
{
	DECLARE_CLASS_SIMPLE(CGameRoomWindow,Frame);
public:
	CGameRoomWindow( IViewPort *pViewPort );

	virtual void ApplySchemeSettings(vgui::IScheme *pScheme);
	void Reset( void );

	virtual const char *GetName( void ) { return PANEL_GAMEROOM; }
	virtual void SetData(KeyValues *data);
	virtual void OnThink();
	virtual void Update();
	virtual bool NeedsUpdate( void ) { return false; }
	virtual bool HasInputElements( void ) { return true; }
	virtual void ShowPanel( bool bShow );

	// both vgui::Frame and IViewPortPanel define these, so explicitly define them here as passthroughs to vgui
	vgui::VPANEL GetVPanel( void ) { return BaseClass::GetVPanel(); }
  	virtual bool IsVisible() { return BaseClass::IsVisible(); }
  	virtual void SetParent( vgui::VPANEL parent ) { BaseClass::SetParent( parent ); }

	// chat related stuff here
	void			Send( void );
	//void			SayGR( bf_read &msg );
	void			ChatPrintf( int iPlayerIndex, int iFilter, const char *fmt, ... );

	// map info receivers
	void			GRMapList( bf_read &msg );
	void			GRMapInfo( bf_read &msg );

	// getters
	Button *GetLaunchButton() { return m_pLaunchGame; }
	CMapSelector *GetMapSelector() { return m_pMapSelector; }

protected:
	virtual void OnCommand( const char *command);
	//virtual void OnTick();

protected:
	IViewPort	*m_pViewPort;

private:	// -- chat stuff
	Button			*m_pLaunchGame;
	TextEntry		*m_pChatBox;
	CBaseChatInputLine	*m_pChatInput;
	int				m_nMessageMode;

	CPlayerList *m_pPlayerList;
	CMapSelector *m_pMapSelector;
};

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
class CMapSelector : public vgui::Panel
{
	DECLARE_CLASS_SIMPLE(CMapSelector,Panel);
public:
	CMapSelector( vgui::Panel *parent );

	virtual void ApplySchemeSettings(vgui::IScheme *pScheme);

	virtual void OnThink();
	virtual void Paint();
	void RefreshMapsList( void );
	void AddMapToList( char *map );
	void Reset( void );

	void SetMapName(char *mapname );
	void SetMapPlayers(char *players );
	void SetPositionOrigin(int i, Vector position );
	void SetPosAvailable(int i, bool state );

	virtual Vector2D WorldToMap( const Vector &worldpos );
	Vector2D		MapToPanel( const Vector2D &mappos );

protected:
	virtual void OnCommand( const char *command);
	void		 ActionPosition( int position );

private:
	ListPanel *pMapList;
	KeyValues *m_pSelected;
	int m_nMapTextureID;

	Label *m_pMapName;
	Label *m_pPlayers;

	CBitmapButtonRTS *m_pPositions[MAX_TEAMS_ID_RTS];
	BitmapImage	m_pImagesPositions[MAX_TEAMS_ID_RTS+1];
	Vector m_vPositionsOrigin[MAX_TEAMS_ID_RTS];
	bool m_bPositionsAvailable[MAX_TEAMS_ID_RTS];
	Vector	m_MapOrigin;
	Vector2D m_MapCenter;
	float	m_fMapScale;	// origin and scale used when screenshot was made
};

//-----------------------------------------------------------------------------
// Purpose: List of players and there preferences
//-----------------------------------------------------------------------------
class CPlayerListEntry : public vgui::Panel
{
	//typedef vgui::Panel BaseClass;
	DECLARE_CLASS_SIMPLE(CPlayerListEntry,Panel);
public:
	CPlayerListEntry( vgui::Panel *parent, int slot );

	virtual void ApplySchemeSettings(vgui::IScheme *pScheme);
	void SetOwner( CBasePlayer *pPlayer ) { m_pOwner = pPlayer; }
	void SetSlotName( const char *name );

	void UpdateGRInfo( int index, int slot );
	void UpdateGRInfoCPU( int slot );
	void Clear();	// make this slot free, cleared

	//void CheckMessages( KeyValues *pData );
private:
	MESSAGE_FUNC_PARAMS( OnTextChanged, "TextChanged", data );
	MESSAGE_FUNC_PTR( OnCheckButtonChecked, "CheckButtonChecked", panel );

private:
	int m_iSlot;
	CBasePlayer			*m_pOwner;		// the one that is occupying this slot

	vgui::ComboBox		*m_pSlotName;	// the name of the owner
	vgui::ComboBox		*m_pColor;		// color of the owner ( choice )
	//vgui::ComboBox		*m_pPosition;
	vgui::ComboBox		*m_pFaction;	// faction of the owner ( choice )
	vgui::CheckButton	*m_pReady;		// is this player ready?
};

//-----------------------------------------------------------------------------
// Purpose: List of players and there preferences
//-----------------------------------------------------------------------------
class CPlayerList : public vgui::Panel
{
	typedef vgui::Panel BaseClass;
public:
	CPlayerList( vgui::Panel *parent );

	virtual void ApplySchemeSettings(vgui::IScheme *pScheme);

	virtual void OnThink();

	//void CheckMessages( KeyValues *pData );

private:
	CPlayerListEntry *m_pPlayerList[MAX_TEAMS_ID_RTS];
	CGameRoomWindow *m_pGRW;
};


//-----------------------------------------------------------------------------
// Purpose: Chat input
//-----------------------------------------------------------------------------
class CBaseChatEntry : public vgui::TextEntry
{
	typedef vgui::TextEntry BaseClass;
public:
	CBaseChatEntry( vgui::Panel *parent, char const *panelName, CGameRoomWindow *pChat )
		: BaseClass( parent, panelName )
	{
		SetCatchEnterKey( true );
		SetAllowNonAsciiCharacters( true );
		SetDrawLanguageIDAtLeft( true );
		m_pRoomChat = pChat;

		SetPaintBackgroundEnabled( true );
		SetPaintBorderEnabled( true );

		SetMouseInputEnabled( true );
	}

	virtual void ApplySchemeSettings( vgui::IScheme *pScheme )
	{
		BaseClass::ApplySchemeSettings(pScheme);

		SetPaintBorderEnabled( false );
	}

	virtual void OnKeyCodeTyped(vgui::KeyCode code)
	{
		//if ( code == vgui::KEY_ENTER || code == vgui::KEY_PAD_ENTER || code == vgui::KEY_ESCAPE )
		if ( code == KEY_ENTER || code == KEY_PAD_ENTER )
		{
			if ( code != KEY_ESCAPE )
			{
				if ( m_pRoomChat )
					m_pRoomChat->Send();
			}
			else 
			{
				//Msg("Pressed escape, loosing chat input focus\n");	
				//SetKeyBoardInputEnabled(false);
			}
		}
		else if ( code == KEY_TAB )
		{
			// Ignore tab, otherwise vgui will screw up the focus.
			return;
		}
		else
		{
			BaseClass::OnKeyCodeTyped( code );
		}
	}

private:
	CGameRoomWindow *m_pRoomChat;
};

//-----------------------------------------------------------------------------
// Purpose: The prompt and text entry area for chat messages
//-----------------------------------------------------------------------------
class CBaseChatInputLine : public vgui::Panel
{
	typedef vgui::Panel BaseClass;
	
public:
	CBaseChatInputLine( CGameRoomWindow *parent, char const *panelName );

	void			SetPrompt( const wchar_t *prompt );
	void			ClearEntry( void );
	void			SetEntry( const wchar_t *entry );
	void			GetMessageText( wchar_t *buffer, int buffersizebytes );

	virtual void	PerformLayout();
	virtual void	ApplySchemeSettings(vgui::IScheme *pScheme);

	vgui::Panel		*GetInputPanel( void );
	virtual vgui::VPANEL GetCurrentKeyFocus() { return m_pInput->GetVPanel(); } 

	virtual void Paint()
	{
		BaseClass::Paint();
	}

protected:
	vgui::Label		*m_pPrompt;
	CBaseChatEntry	*m_pInput;
};

// --------- end chat entry

extern CGameRoomWindow *g_RoomWindow;

#endif // GAMEROOMWINDOW_H 
