// End Level Screen GUI Handler.
// Worthless Bums 2012.


// End Level Screen GUI onwardButton Pressed.
function onwardButton ()
{//echo("onwardButton()");
	// Unflag ui pushed!
	$deathScreenPushed = false;
	
	// Hide cursor.
	Canvas.hideCursor();
	// Pop End Level Screen Gui.
	Canvas.popDialog(endLevelScreenGui);
	
	// Continue elevator process.
	continueTestStairs();
}

// Pushes End Level Screen Gui as a Death Screen.
function pushEndLevelScreenGuiAsDeathScreen ()
{//echo("pushEndLevelScreenGuiAsDeathScreen()");
	if ($deathScreenPushed)
	{
		return;
	}
	else
	{
		triggerSquadDeathAchievements();
	}
	// Send data to Steam Marines score server!
	// NOTE: Should allow players to opt in!
	if ($OptInDataSend)
	{//echo("SENDING SCORE DATA TO SERVER!");
		tcpSendLevelData();
	}
	
	$deathScreenPushed = true;
	initializeEndLevelScreenGui();
	
	// Change backgrounds.
	endLevelScreenBackground.bitmap = "../../../Sprites/1/assets/images/UI/Backgrounds/Defeat";
	// Change end level screen button to death screen button.
	endLevelScreenButton.command = "$timescale = 1;Canvas.popDialog(endLevelScreenGui);gameOverMan();";
	endLevelScreenButton.text = "Game Over";
	// Change text label.
	endLevelScreenTextLabel.bitmap = "../../../Sprites/1/assets/images/UI/Backgrounds/defeatLabel";
	endLevelScreenTextLabel.position = "351 50";
	endLevelScreenTextLabel.extent = "98 23";
	endLevelScreenButton.position = "357 550";
	
	// Show cursor.
	Canvas.showCursor();
	// Push.
	$endLevelScreenPushed = true;
	Canvas.pushDialog(endLevelScreenGui);
	
	// Pause the game.
	$timescale = 0;	// REALLY need a full input lockdown function in input.cs!
	Canvas.popDialog(exploreGui);
	Canvas.popDialog(inventoryTabGui);
}

function getDifficultyLabel (%difficultyLevel)
{//echo("%difficultyLabel =()");
	switch (%difficultyLevel)
	{
		case 0:	return "Easy";
		case 1:	return "Normal";
		case 2:	return "Hard";
		case 3:	return "???";
	}
}

// Initializes End Level Screen Gui.
function initializeEndLevelScreenGui ()
{//echo("initializeEndLevelScreenGui()");
	// Change backgrounds.
	endLevelScreenBackground.bitmap = "../../../Sprites/1/assets/images/UI/Backgrounds/Victory";
	// Change end level screen button to death screen button.
	endLevelScreenButton.command = "$timescale = 1;onwardButton();";
	endLevelScreenButton.text = "Onward!";
	endLevelScreenButton.position = "369 550";
	
	// Change text label.
	endLevelScreenTextLabel.bitmap = "../../../Sprites/1/assets/images/UI/Backgrounds/victoryLabel";
	endLevelScreenTextLabel.position = "340 50";
	endLevelScreenTextLabel.extent = "121 24";
	
	// Push the death screen flag!
	$deathScreenPushed = true;
	
	// If we're on dungeon level 1, that means the player finished a deck.
	// So we push the ship overview ui to choose a new deck instead of automatically stringing the player along!
	if ($dungeonLevel == 1)
	{
		endLevelScreenButton.command = "pushShipOverview();";		// Comment this line to disconnect the Ship Overview UI!
	}
	
	%difficultyLabel = "";
	switch ($difficultyLevel)
	{
		case 0:	%difficultyLabel = "Easy";
		case 1:	%difficultyLabel = "Normal";
		case 2:	%difficultyLabel = "Hard";
		case 3:	%difficultyLabel = "Brutal";
	}
	
	// Compose description text.
	%descriptionText = 	"Current score"						NL
						"Turns taken"						NL
						"Levels passed"						NL
						//"Primary objectives completed"		NL
						//"Secondary objectives completed"	NL
						
						//""									NL
						
						"Squad member deaths"				NL
						"Survivors recruited"				NL
						"Kills"								NL
						"Ammunition expended"				NL
						//"Ammo expended mass"				NL
						"Times used melee"					NL
						"Damage dealt"						NL
						"Damage taken"						NL
						"Enemies knocked into space"		NL
						
						//""									NL
						
						"Walls smashed"						NL
						"Crates smashed"					NL
						"Dispensers used"					NL
						"Consoles used"						NL
						//"Doors opened"						NL
						//"Doors closed"						NL
						
						//""									NL
						
						"Items grabbed"						NL
						"Items used"						NL
						"Chests opened"						NL
						//"Switches used"						NL
						"Times entered Guard Mode"			NL
						
						""									NL
						
						%difficultyLabel SPC "Progress Report"
						;
	// Compose stats text.
	%statsText = 	calculateCurrentScore()									NL
					$GlobalStats.currentTurn - 1							NL
					$GlobalStats.totalLevelsPassed							NL
					//$GlobalStats.totalPrimaryMissionObjectivesCompleted		NL
					//$GlobalStats.totalSecondaryMissionObjectivesCompleted	NL
					
					//""														NL
					
					$GlobalStats.totalSquadMemberDeaths						NL
					$GlobalStats.totalSurvivorsRecruited					NL
					$GlobalStats.totalKills									NL
					$GlobalStats.totalAmmunitionExpended					NL
					//$GlobalStats.totalAmmunitionMassExpended				NL
					$GlobalStats.totalMeleeAttacks							NL
					$GlobalStats.totalDamageDealt							NL
					$GlobalStats.totalDamageTaken							NL
					$GlobalStats.totalEnemiesKnockedIntoSpace				NL
					
					//""														NL
					
					$GlobalStats.totalWallsSmashed							NL
					$GlobalStats.totalCratesSmashed							NL
					$GlobalStats.totalDispensersUsed						NL
					$GlobalStats.totalConsolesUsed							NL
					//$GlobalStats.totalDoorsOpened							NL
					//$GlobalStats.totalDoorsClosed							NL
					
					//""														NL
					
					$GlobalStats.totalItemsGrabbed							NL
					$GlobalStats.totalItemsUsed								NL
					$GlobalStats.totalChestsOpened							NL
					//$GlobalStats.totalLightSwitchesUsed						NL
					$GlobalStats.totalGuardModes							NL
					
					""														NL
					
					calculateLetterGrade()
					;
	
	// Set description text.
	endLevelScreenDescription.setText(%descriptionText);
	// Set stats text.
	endLevelScreenStats.setText(%statsText);
}

function calculateLetterGrade ()
{//echo("calculateLetterGrade()");
	%currentScore = calculateRunScore();//calculateCurrentScore();
	$SMRankScore	= 90000;
	$SSRankScore	= 50000;
	$SRankScore 	= 20000;
	$ARankScore 	= 10000;
	$BRankScore 	=  7500;
	$CRankScore 	=  5000;
	$DRankScore 	=  2500;
	$ERankScore 	=  1000;
	$FRankScore 	=  0;
	%currentRank = "L";	// Lowest rank! Negative score!
	if (%currentScore >= $SMRankScore)
	{
		%currentRank = "SM";
	}
	else if (%currentScore >= $SSRankScore)
	{
		%currentRank = "SS";
	}
	else if (%currentScore >= $SRankScore)
	{
		%currentRank = "S";
	}
	else if (%currentScore >= $ARankScore)
	{
		%currentRank = "A";
	}
	else if (%currentScore >= $BRankScore)
	{
		%currentRank = "B";
	}
	else if (%currentScore >= $CRankScore)
	{
		%currentRank = "C";
	}
	else if (%currentScore >= $DRankScore)
	{
		%currentRank = "D";
	}
	else if (%currentScore >= $ERankScore)
	{
		%currentRank = "E";
	}
	else if (%currentScore >= $FRankScore)
	{
		%currentRank = "F";
	}
	return %currentRank;
}