// Handles Armor Swap GUI Controls.
// Worthless Bums 2012.


// Switches to the next valid squad member that can use the selected armor.
function armorSwapNextSquadMember ()
{//echo("armorSwapNextSquadMember()");
	%armor = $swapArmorItem;
	if ($armorSwapSquadList.getCount() > $swapArmorSquadIndex + 1)
	{
		$swapArmorSquadIndex++;
	}
	else
	{
		$swapArmorSquadIndex = 0;
	}
	$weaponSwapPushed = true;			// BAD! Just use the armor swap flag for armor swapping for now since they can't both be up at the same time.
	$timescale = 0;
	%armorName = %armor.name;
	%squadMemberName = ($armorSwapSquadList.getObject($swapArmorSquadIndex)).name;
	%armorArmorRating = %armor.armorRating;
	%armorSpeed = %armor.speed;
	%armorImageMap = %armor.imageMapName;
	%currentArmor = ($armorSwapSquadList.getObject($swapArmorSquadIndex)).armor;
	squadMemberNameArmorSwap.setText(%squadMemberName @ " (" @ getCharacterClassName(($armorSwapSquadList.getObject($swapArmorSquadIndex)).characterClass) @ ")");
	newArmorText.setText(%armorName									NL
						 %armorArmorRating SPC "Armor" 				NL
						 %armorSpeed SPC "Speed"					NL
						 %armor.repair SPC "Repair"					NL
						 //%armor.oxygen SPC "Oxygen"					NL
						 %armor.fireResistance SPC "Fir.Res."		NL
						 %armor.frostResistance SPC "Fro.Res."		NL
						 %armor.acidResistance SPC "Acd.Res."		NL
						 %armor.explosiveResistance SPC "Exp.Res."
						 );
	currentArmorText.setText(%currentArmor.name									NL
							 %currentArmor.armorRating SPC "Armor" 				NL
							 %currentArmor.speed SPC "Speed"					NL
							 %currentArmor.repair SPC "Repair"					NL
							 //%currentArmor.oxygen SPC "Oxygen"					NL
							 %currentArmor.fireResistance SPC "Fir.Res."		NL
							 %currentArmor.frostResistance SPC "Fro.Res."		NL
							 %currentArmor.acidResistance SPC "Acd.Res."		NL
							 %currentArmor.explosiveResistance SPC "Exp.Res."
							 );
	newArmorIcon.setBitmap(getArmorBitmapFromImagemap(%armorImageMap));
	currentArmorIcon.setBitmap(getArmorBitmapFromImagemap(%currentArmor.imageMapName));
	Canvas.pushDialog(armorSwapControl);
	return true;
}

// Pushes the armor swap gui with the specified armor object information.
function pushArmorSwapGuiFromInventory (%armor)
{//echo("pushArmorSwapGuiFromInventory()");
	$armorSwapSquadList = new SimSet();
	$swapArmorSquadIndex = 0;
	$swapArmorItem = %armor;
	if ($leaderPersist	&&
		%armor.squadMember == $playerSquad[0].characterClass)
	{
		$armorSwapSquadList.add($playerSquad[0]);
	}
	if ($scoutPersist	&&
		%armor.squadMember == $playerSquad[1].characterClass)
	{
		$armorSwapSquadList.add($playerSquad[1]);
	}
	if ($grenadierPersist	&&
		%armor.squadMember == $playerSquad[2].characterClass)
	{
		$armorSwapSquadList.add($playerSquad[2]);
	}
	if ($supportPersist	&&
		%armor.squadMember == $playerSquad[3].characterClass)
	{
		$armorSwapSquadList.add($playerSquad[3]);
	}
	if ($engineerPersist	&&
		%armor.squadMember == $playerSquad[4].characterClass)
	{
		$armorSwapSquadList.add($playerSquad[4]);
	}
	if ($armorSwapSquadList.getCount() <= 0)	// Make sure there is at least one squad member who can use this armor, otherwise leave!
	{
		return false;
	}
	$weaponSwapPushed = true;			// BAD! Just use the armor swap flag for armor swapping for now since they can't both be up at the same time.
	$timescale = 0;
	%armorName = %armor.name;
	%squadMemberName = ($armorSwapSquadList.getObject($swapArmorSquadIndex)).name;//$playerSquad[%armor.squadMember].name;
	%armorArmorRating = %armor.armorRating;
	%armorSpeed = %armor.speed;
	%armorImageMap = %armor.imageMapName;
	%currentArmor = ($armorSwapSquadList.getObject($swapArmorSquadIndex)).armor;//$playerSquad[%armor.squadMember].armor;
	squadMemberNameArmorSwap.setText(%squadMemberName @ " (" @ getCharacterClassName(($armorSwapSquadList.getObject($swapArmorSquadIndex)).characterClass) @ ")");
	newArmorText.setText(%armorName									NL
						 %armorArmorRating SPC "Armor" 				NL
						 %armorSpeed SPC "Speed"					NL
						 %armor.repair SPC "Repair"					NL
						 //%armor.oxygen SPC "Oxygen"					NL
						 %armor.fireResistance SPC "Fir.Res."		NL
						 %armor.frostResistance SPC "Fro.Res."		NL
						 %armor.acidResistance SPC "Acd.Res."		NL
						 %armor.explosiveResistance SPC "Exp.Res."
						 );
	currentArmorText.setText(%currentArmor.name									NL
							 %currentArmor.armorRating SPC "Armor" 				NL
							 %currentArmor.speed SPC "Speed"					NL
							 %currentArmor.repair SPC "Repair"					NL
							 //%currentArmor.oxygen SPC "Oxygen"					NL
							 %currentArmor.fireResistance SPC "Fir.Res."		NL
							 %currentArmor.frostResistance SPC "Fro.Res."		NL
							 %currentArmor.acidResistance SPC "Acd.Res."		NL
							 %currentArmor.explosiveResistance SPC "Exp.Res."
							 );
	newArmorIcon.setBitmap(getArmorBitmapFromImagemap(%armorImageMap));
	currentArmorIcon.setBitmap(getArmorBitmapFromImagemap(%currentArmor.imageMapName));
	Canvas.pushDialog(armorSwapControl);
	return true;
}

// Pushes the armor swap gui with the specified weapon code information.
function pushArmorSwapGui (%armorCode)
{//echo("pushArmorSwapGui()");
//echo("%armorCode =" SPC %armorCode);
	$weaponSwapPushed = true;			// Just use the weapon swap flag for armor swapping for now.
	$currentArmorCode = %armorCode;
	$timescale = 0;
	%armorName = getWord(%armorCode, 0);
	%squadMemberName = $playerSquad[getWord(%armorCode, 1)].name;
	%armorArmorRating = getWord(%armorCode, 2);
	%armorSpeed = getWord(%armorCode, 3);
	%armorImageMap = getWord(%armorCode, 4);
	%currentArmor = $playerSquad[getWord(%armorCode, 1)].armor;
	squadMemberNameArmorSwap.setText(%squadMemberName @ " (" @ getCharacterClassName($playerSquad[getWord(%armorCode, 1)].characterClass) @ ")");
	newArmorText.setText(%armorName							NL
						 %armorArmorRating SPC "Armor" 		NL
						 %armorSpeed SPC "Speed");
	currentArmorText.setText(%currentArmor.name						NL
							 %currentArmor.armorRating SPC "Armor" 	NL
							 %currentArmor.speed SPC "Speed");
	newArmorIcon.setBitmap(getArmorBitmapFromImagemap(%armorImageMap));
	currentArmorIcon.setBitmap(getArmorBitmapFromImagemap(%currentArmor.imageMapName));
	Canvas.pushDialog(armorSwapControl);
}

// Clear armor swap data.
function clearArmorSwapDialog ()
{//echo("clearArmorSwapDialog()");
	$weaponSwapPushed = false;
	squadMemberNameArmorSwap.setText("");
	newArmorText.setText("");
	currentArmorText.setText("");
	newArmorIcon.setBitmap("");
	currentArmorIcon.setBitmap("");
	$currentArmorCode = "";
}

// Keep Armor Button clicked.
function keepArmorButton ()
{//echo("keepArmorButton()");
	playAudio("Audio:armorSwap", EffectsAudioVolume.value);
	$weaponSwapPushed = false;
	$timescale = 1;
	Canvas.popDialog(armorSwapControl);
}

// Swap Armor Button clicked.
function swapArmorButton ()
{//echo("swapArmorButton()");
	playAudio("Audio:armorSwap", EffectsAudioVolume.value);
	%characterIndex = getWord($currentArmorCode, 1);
	%currentArmor = createNewArmor($currentArmorCode);
	%apChange = ($playerSquad[%characterIndex].armor).speed;
	$playerSquad[%characterIndex].maxActionPoints -= %apChange;
	mountArmorToCharacter(%currentArmor, %characterIndex);
	%apChange = ($playerSquad[%characterIndex].armor).speed;
	$playerSquad[%characterIndex].maxActionPoints += %apChange;
	if ($playerSquad[%characterIndex].currentActionPoints > $playerSquad[%characterIndex].maxActionPoints)
	{
		setCurrentActionPoints(%characterIndex, $playerSquad[%characterIndex].maxActionPoints);
	}
	updateReadouts();
	updatePortraits();
	$weaponSwapPushed = false;
	Canvas.popDialog(armorSwapControl);
	$timescale = 1;
	clearArmorSwapDialog();
	scheduleMonsterTurn();
}

// Swap Armor Button clicked.
function swapArmorFromInventoryButton ()
{//echo("swapArmorFromInventoryButton()");
	playAudio("Audio:armorSwap", EffectsAudioVolume.value);
	%characterIndex = -1;
	if ($leaderPersist	&&
		$playerSquad[0] == $armorSwapSquadList.getObject($swapArmorSquadIndex))
	{
		%characterIndex = 0;
	}
	if ($scoutPersist	&&
		$playerSquad[1] == $armorSwapSquadList.getObject($swapArmorSquadIndex))
	{
		%characterIndex = 1;
	}
	if ($grenadierPersist	&&
		$playerSquad[2] == $armorSwapSquadList.getObject($swapArmorSquadIndex))
	{
		%characterIndex = 2;
	}
	if ($supportPersist	&&
		$playerSquad[3] == $armorSwapSquadList.getObject($swapArmorSquadIndex))
	{
		%characterIndex = 3;
	}
	%apChange = ($playerSquad[%characterIndex].armor).speed;
	$playerSquad[%characterIndex].maxActionPoints -= %apChange;
	%oldArmor = $playerSquad[%characterIndex].armor;
	%currentArmor = $currentArmorCode;
	$playerSquad[%characterIndex].armor = %currentArmor;
	%armorToRemove = $squadInventory.getObject($currentItemSlot);
	$squadInventory.remove(%armorToRemove);
	%armorToRemove.safeDelete();
	$squadInventory.add(%oldArmor);
	mountArmorToCharacter($playerSquad[%characterIndex].armor, %characterIndex);
	%apChange = ($playerSquad[%characterIndex].armor).speed;
	$playerSquad[%characterIndex].maxActionPoints += %apChange;
	if ($playerSquad[%characterIndex].currentActionPoints > $playerSquad[%characterIndex].maxActionPoints)
	{
		setCurrentActionPoints(%characterIndex, $playerSquad[%characterIndex].maxActionPoints);
	}
	updateReadouts();
	updatePortraits();
	$weaponSwapPushed = false;
	Canvas.popDialog(armorSwapControl);
	$timescale = 1;
	clearArmorSwapDialog();
	updateSquadInfoGui();
	updateInventoryTabInformation();
}

// Keep Armor Button clicked.
function keepArmorFromInventoryButton ()
{//echo("keepArmorFromInventoryButton()");
	playAudio("Audio:armorSwap", EffectsAudioVolume.value);
	$weaponSwapPushed = false;
	$timescale = 1;
	Canvas.popDialog(armorSwapControl);
	updateInventoryTabInformation();
}