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

var int ranking, IDnum, LadderSize;
var Localized String[20] Ladder[16];
var String[32] Maps[16];
var class<GameInfo> GameClass;
var config int rung[8]; //position in ladder for each game ID
var String[32] TrainerMap;
var Battle2D Battle;

function Destroyed()
{
	if ( Battle != None )
		Battle.Destroy();

	Super.Destroyed();
}

function Climb(int ID)
{
	rung[ID]++;
	if ( rung[ID] >= LadderSize )
		rung[ID] = 15; //completed
		
	SaveConfig();
}

function bool ProcessSelection()
{
	local Menu ChildMenu;

	StartGame();
	return true;
}

function StartGame()
{
	local string[64] ClassString;
	local Gameinfo NewGame;
	local String[255] URL;
	local int d;
		
	NewGame = Spawn(GameClass);
	NewGame.ResetGame();
	NewGame.Destroy();

	d = rung[IDnum]/3 + ranking;
	URL = Maps[rung[IDnum]]$"?Game=BotPack."$GameClass$"?RatedGame="$IDnum$"?Difficulty="$d;

	SaveConfigs();
	bExitAllMenus = true;
	ClassString = class'ChallengeMeshMenu'.Default.ClassString;
	if ( ClassString == "" )
		ClassString = string(class'DeathMatchPlus'.Default.DefaultPlayerClass);

	URL = URL   $"?Class="$ClassString
				$"?Skin="$class'ChallengeMeshMenu'.Default.PreferredSkin
				$"?Name="$PlayerOwner.PlayerReplicationInfo.PlayerName
				$"?Team="$PlayerOwner.PlayerReplicationInfo.Team
				$"?Rate="$PlayerOwner.NetSpeed;

	log( "Ladder URL: "$URL );
	SaveConfigs();
	Level.ServerTravel( URL, false );
}

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

function MenuTick(float DeltaTime)
{
	local int I;
	
	Super.MenuTick(DeltaTime);
	if ( Battle != None )
		Battle.DeltaTime = DeltaTime;

	// Update FadeTimes.
	if (TitleFadeTime >= 0.0)
		TitleFadeTime += DeltaTime;
	for (I=0; I<24; I++)
		if (MenuFadeTimes[I] >= 0.0)
			MenuFadeTimes[I] += DeltaTime;

	// Update PulseTime.
	if (bPulseDown)
	{
		if (PulseTime > 0.0)
		{
			PulseTime -= DeltaTime * 30;
			if (PulseTime < 0.0)
				PulseTime = 0.0;
		} else {
			PulseTime = 0.0;
			bPulseDown = false;
		}
	} else {
		if (PulseTime < 25.5)
		{
			PulseTime += DeltaTime * 30;
			if (PulseTime > 25.5)
				PulseTime = 25.5;
		} else {
			PulseTime = 25.5;
			bPulseDown = true;
		}
	}
}

function DrawLadder(canvas Canvas, int Spacing, int StartX, int StartY)
{
	local int i;
	local color DrawColor;

	// draw Rating
	Canvas.Font = Font'WhiteFont';
	for ( i=0; i<3; i++ )
	{
		if ( Ranking == i )
		{
			Canvas.DrawColor.R = PulseTime * 10;
			Canvas.DrawColor.G = PulseTime * 10;
			Canvas.DrawColor.B = 255;
		}
		else
		{
			Canvas.DrawColor.R = 0;
			Canvas.DrawColor.G = 150;
			Canvas.DrawColor.B = 0;
		}
		Canvas.SetPos(StartX + Canvas.ClipX * 0.25 * (i+1) - 25, StartY );
		Canvas.DrawText(class'ChallengeGameMenu'.Default.Ranking[i], false);
	}

	// draw ladder

	StartY += 3 * Spacing;
	for (i=0; i< LadderSize; i++ )
	{
		if (i == Rung[IDnum])
		{
			Canvas.DrawColor.R = 255;
			Canvas.DrawColor.G = PulseTime * 10;
			Canvas.DrawColor.B = PulseTime * 10;
		} 
		else 
		{
			Canvas.DrawColor.R = 0;
			Canvas.DrawColor.G = 150;
			Canvas.DrawColor.B = 0;
		}
		Canvas.SetPos(Canvas.ClipX/2 - 80, StartY + Spacing * i );
		Canvas.DrawText(Ladder[i], false);
	}
}

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

	// Draw title.
	DrawFadeTitle(Canvas);

	//Draw battle
	if ( Battle == None )
	{
		Battle = Spawn(class'Battle2D');
		Battle.PlayerOwner = PlayerOwner;
	}
	Battle.DrawSelf(Canvas, 5);

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

	DrawLadder(Canvas, Spacing, 0, StartY + Spacing);
	DrawFadeList(Canvas, Spacing, StartX, StartY);  

	// Draw help panel.
	Canvas.DrawColor.R = 0;
	Canvas.DrawColor.G = 255;
	Canvas.DrawColor.B = 0;
	HelpMessage[1] = Default.HelpMessage[1]$Ladder[rung[IDnum]]$".";
	DrawHelpPanel(Canvas, Canvas.ClipY - 64, 228);
}

defaultproperties
{
	 GameClass=class'Botpack.DeathMatchPlus'
     MenuLength=1
     HelpMessage(1)="Hit enter to challenge the "
     MenuList(1)=""
     MenuTitle="Ladder"
	 LadderSize=6
	 Ladder(0)="Canucks"
	 Ladder(1)="Dudes"
	 Ladder(2)="Commies"
	 Ladder(3)="Babes"
	 Ladder(4)="Homies"
	 Ladder(5)="Reapers"
	 Ladder(6)="Evil Publishers"
	 Ladder(7)="Badasses"
	 rung(0)=15
	 rung(1)=15
	 rung(2)=15
	 rung(3)=15
	 rung(4)=15
	 rung(5)=15
	 rung(6)=15
	 rung(7)=15
}