//=============================================================================
// UnrealServerMenu
//=============================================================================
class UnrealServerMenu expands UnrealMenu;

var string[32] Map;
var string[32] Folder;
var class GameType;
var config class Games[16];
var int MaxGames;
var int CurrentGame;

function PostBeginPlay()
{
	CallParent.PostBeginPlay();

	GameType = Games[0];
	Folder = GetFolderName(Level.Game.DefaultMapFolder,0);
	Map = GetMapName(Folder,Map,0);
}

function bool ProcessLeft()
{
	if ( Selection == 1 )
	{
		CurrentGame--;
		if ( CurrentGame < 0 )
			CurrentGame = MaxGames;
		GameType = Games[CurrentGame];
	}
	else if ( Selection == 2 )
	{
		Folder = GetFolderName(Folder, -1);
		Map = GetMapName(Folder, Map, 0);
	}
	else if ( Selection == 3 )
		Map = GetMapName(Folder, Map, -1);
	else 
		return false;

	return true;
}

function bool ProcessRight()
{
	if ( Selection == 1 )
	{
		CurrentGame++;
		if ( CurrentGame > MaxGames )
			CurrentGame = 0;
		GameType = Games[CurrentGame];
	}
	else if ( Selection == 2 )
	{
		Folder = GetFolderName(Folder, 1);
		Map = GetMapName(Folder, Map, 0);
	}
	else if ( Selection == 3 )
		Map = GetMapName(Folder, Map, 1);
	else
		return false;

	return true;
}

function bool ProcessSelection()
{
	local Menu ChildMenu;

	if ( Selection == 4 )
	{
		if ( (GameType != None) && (Level.Game.Class != GameType) )
		{
			Level.Game.Destroy();
			Level.Game = UnrealGameInfo(spawn(GameType));
		}
		if ( (Level.Game != None) && (Level.Game.GameMenuType != None) )
			ChildMenu = Menu(spawn(Level.Game.GameMenuType, owner));
	}
	else if ( Selection == 5 )
	{
		bExitAllMenus = true;
		PlayerOwner.ConsoleCommand("Listen");
		PlayerOwner.DelayedCommand = "..\\maps\\"$Folder$"\\"$Map$"?Game="$GameType;
		if ( PlayerOwner.PreferredClass != None )
			PlayerOwner.DelayedCommand = PlayerOwner.DelayedCommand$"?Class="$PlayerOwner.PreferredClass;
		PlayerOwner.ClientLinkToURL(PlayerOwner.DelayedCommand, false);
	}
	else
		return false;

	if ( ChildMenu != None )
	{
		HUD(Owner).MainMenu = ChildMenu;
		ChildMenu.ParentMenu = self;
		ChildMenu.PlayerOwner = PlayerOwner;
	}
	return true;
}

function SaveConfigs()
{
	PlayerOwner.SaveConfig('PlayerPawn');
}

function DrawMenu(canvas Canvas)
{
	local int StartX, StartY, Spacing;
	local string[32] MapName;

	// Draw Title
	StartX = Max(8, 0.5 * Canvas.ClipX - 96);
	Canvas.Font = Font'LargeFont';
	Canvas.SetPos(StartX, 4 );
	Canvas.DrawText("MULTIPLAYER MENU", False);
		
	Spacing = Clamp(0.07 * Canvas.ClipY, 24, 48);
	StartX = Max(40, 0.5 * Canvas.ClipX - 120);
	StartY = Max(40, 0.5 * (Canvas.ClipY - MenuLength * Spacing - 128));
	Canvas.Font = Font'MedFont';

	// draw text
	Canvas.SetPos(StartX, StartY );
	Canvas.DrawText("Select Game", False);
	Canvas.SetPos(StartX + 160, StartY );
	Canvas.DrawText(GameType, False);
	Canvas.SetPos(StartX, StartY + Spacing );
	Canvas.DrawText("Select Folder", False);
	Canvas.SetPos(StartX + 160, StartY + Spacing );
	Canvas.DrawText(Folder, False);
	Canvas.SetPos(StartX, StartY + 2 * Spacing );
	Canvas.DrawText("Select Map", False);
	MapName = Left(Map, Len(Map) - 4 );
	Canvas.SetPos(StartX + 160, StartY + 2 * Spacing );
	Canvas.DrawText(MapName, False);
	Canvas.SetPos(StartX, StartY + 3 * Spacing );
	Canvas.DrawText("Configure Game", False);
	Canvas.SetPos(StartX, StartY + 4 * Spacing );
	Canvas.DrawText("Start Game", False);

	// show selection
	Canvas.SetPos( StartX - 20, StartY - Spacing + Spacing * Selection);
	Canvas.DrawText("[]", false);	

	// Draw help panel
	DrawHelpPanel(Canvas, StartY + MenuLength * Spacing);
}
