//=============================================================================
// ChallengeGameMenu
//=============================================================================
class ChallengeGameMenu expands UnrealShortMenu
	localized
	config;

var config string[16] GameID[8];
var int IDnum;
var string[16] OldGameID;
var config int Rank[8]; //rank for each gameID (0-2 = novice-expert)
var localized string[16] Ranking[4];
var localized string[128] EnterAName;
var localized string[20] RungName[16];
var class<LadderMenu> Ladders[8];
var int NumLadders;

function string[20] GetRungName(int i)
{
	return RungName[i];
}

function ProcessMenuInput( coerce string[64] InputString )
{
	local menu ChildMenu;
	local LadderMenu L;
	local int i;

	if ( selection == 1 )
	{
		GameID[IDnum] = Left(InputString, 16);
		if ( OldGameID == "" )
		{
			// clear all config values for this game ID
			Rank[IDnum] = 0;
			for ( i=0; i<numLadders; i++ )
			{
				L = Spawn(Ladders[i]);
				L.rung[IDnum] = 0;
				L.SaveConfig();
				L.Destroy();
			}
		}
		ChildMenu = spawn(class'ChallengeMeshMenu', owner);
		PlayerOwner.PlayerReplicationInfo.PlayerName = GameID[Idnum];
		SaveConfigs();
		if ( ChildMenu != None )
		{
			HUD(Owner).MainMenu = ChildMenu;
			ChildMenu.ParentMenu = self;
			ChildMenu.PlayerOwner = PlayerOwner;
		}
		for ( i=1; i<MenuLength; i++ )
			HelpMessage[i] = Default.HelpMessage[i];
	}
}

function ProcessMenuEscape()
{
	if ( selection == 1 )
		GameID[IDnum] = OldGameID;
}

function ProcessMenuUpdate( coerce string[64] InputString )
{
	InputString = Left(InputString, 20);

	if ( selection == 1 )
		GameID[IDnum] = (InputString$"_");
}

function bool ProcessLeft()
{
	if ( Selection == 1 )
	{
		if ( GameID[0] != "" )
		{
			IDnum--;
			if ( IDnum < 0 )
				IDnum = 7;

			while ( (IDnum > 0) && (GameID[IDnum-1] == "") )
			{
				IDnum--;
			}
		}
	}

	return true;
}

function bool ProcessRight()
{
	if ( Selection == 1 )
	{
		if ( (IDnum == 7) || (GameID[IDNum] == "") )
			IDnum = 0;
		else
			IDnum++;
	}

	return true;
}

function bool ProcessSelection()
{
	local Menu ChildMenu;

	if ( Selection == 1 )
	{
		OldGameID = GameID[IDnum];
		GameID[IDnum] = "_";
		PlayerOwner.Player.Console.GotoState('MenuTyping');
	}
	else if ( Selection < 6 )
	{
		if ( GameID[IDnum] == "" )
		{
			HelpMessage[Selection] = EnterAName;
			return false;
		}
		if ( Ladders[Selection - 2].Default.Rung[IDnum] == 15 )
			return false; //finished
		ChildMenu = spawn(Ladders[Selection - 2], owner);
		LadderMenu(ChildMenu).Ranking = Rank[IDnum]; 
		LadderMenu(ChildMenu).IDnum = IDnum;
		if ( GameID[IDnum] == "" )
			GameID[IDnum] = "NewGame";
	}
	else if ( Selection == 6 )
	{
		ChildMenu = spawn(class'UnrealServerMenu', owner);
		UnrealServerMenu(ChildMenu).bStandAlone = true;
	}

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

function SaveConfigs()
{
	SaveConfig();
	//PlayerOwner.PlayerReplicationInfo.SaveConfig();
}

function MenuTick(float DeltaTime)
{
	local int I;
	
	Super.MenuTick(DeltaTime);
	
	// Update FadeTimes.
	if (TitleFadeTime >= 0.0)
		TitleFadeTime += DeltaTime;
	for (I=0; I<24; I++)
		if (MenuFadeTimes[I] >= 0.0)
			MenuFadeTimes[I] += DeltaTime;
}

function DrawMenu(canvas Canvas)
{
	local int i, StartX, StartY, Spacing;
	local vector DrawOffset;
	local rotator NewRot;

	// Draw title.
	DrawFadeTitle(Canvas);

	Spacing = Clamp(0.04 * Canvas.ClipY, 12, 32);
	StartX = Canvas.ClipX/2 - 80;
	StartY = Max(40, 0.5 * (Canvas.ClipY - MenuLength * Spacing - 128));

	DrawFadeList(Canvas, Spacing, StartX, StartY);  

	Canvas.SetPos(StartX + 144, StartY);
	
	Canvas.DrawColor.R = 255;
	Canvas.DrawColor.G = 255;
	Canvas.DrawColor.B = 0;
	if ( GameID[IDnum] != "" )
		Canvas.DrawText(GameID[IDnum]$" ("$Ranking[Rank[IDnum]]$")", false);
	else
		Canvas.DrawText("_", false);

	Canvas.DrawColor.R = 0;
	Canvas.DrawColor.G = 0;
	Canvas.DrawColor.B = 255;
	for ( i=2; i<2+NumLadders; i++ )
	{
		Canvas.SetPos(StartX + 144, StartY + (i-1) * Spacing );
		Canvas.DrawText(RungName[Ladders[i-2].Default.Rung[IDnum]], false);
	}					

	// Draw help panel.
	Canvas.DrawColor.R = 0;
	Canvas.DrawColor.G = 255;
	Canvas.DrawColor.B = 0;
	DrawHelpPanel(Canvas, Canvas.ClipY - 64, 228);
}

defaultproperties
{
     MenuLength=6
     HelpMessage(1)="Hit enter to type a new Game ID, or use the arrow keys to select an existing one."
     HelpMessage(6)="Hit enter to configure and start a custom (non-Ladder) game."
     MenuList(1)="Game ID: "
     MenuList(2)="CTF Ladder"
     MenuList(3)="Deathmatch Ladder"
     MenuList(4)="Assault Ladder"
	 MenuList(5)="Domination Ladder"
	 MenuList(6)="Challenge Botmatch"
     MenuTitle="UNREAL TOURNAMENT"
	 Ranking(0)="Novice"
	 Ranking(1)="Advanced"
	 Ranking(2)="Expert"
	 Ranking(3)="Champion"
	 RungName(0)="Untested"
	 RungName(1)="Amateur"
	 RungName(2)="Adept"
	 RungName(3)="Warrior"
	 RungName(4)="Challenger"
	 RungName(5)="Hero"
	 RungName(6)="Master"
	 RungName(7)="Lord"
     RungName(15)="GRANDMASTER"
	 NumLadders=4
	 Ladders(0)=Class'BotPack.CTFLadder'
	 Ladders(1)=Class'BotPack.DMLadder'
	 Ladders(2)=Class'BotPack.AssaultLadder'
	 Ladders(3)=Class'BotPack.DominationLadder'
	 GameID(0)="Player1"
}
