// Merchant Handler.
// Worthless Bums 2013.


function updateMoneyCounter ()
{
	moneyCounterButton.setText("Credits: " @ $squadCredits);
}
function pushMerchantGui ()
{
	$merchantAccessed++;
	randomizeMerchantItems();
	$merchantGuiPushed = true;//enableGuiInProgress();	// Redundant but it -should- be here! We just have merchant dialogue where we set it initially!
	%xPos = (getWord($Game::Resolution, 0) / 2) - 400;
	%yPos = (getWord($Game::Resolution, 1) / 2) - 300;
	merchantPanelScreen.position = %xPos SPC %yPos;
	Canvas.pushDialog(merchantGui);
}
function popMerchantGui ()
{
	updatemoneycounter();	// Just in case!
	$merchantGuiPushed = false;
	Canvas.popDialog(merchantGui);
	disableGuiInProgress();
}
function randomizeMerchantItems ()
{
	for (%i = 1; %i <= 4; %i++)
	{
		%itemIndex = %i;
		%itemType = getRandom(0, 6);
		%itemBitmap = "";
		%cost = 0;
		switch (%itemType)
		{
			case 0:	// Medkit.
				%itemType = 0;
				%itemBitmap = "../../../Sprites/1/assets/images/Items/Item-Medkit";
				%cost = getRandom(2000, 2500) + $merchantAccessed * 100;
				
			case 1:	// Ammo Crate.
				%itemType = 1;
				%itemBitmap = "../../../Sprites/1/assets/images/Items/Item-Ammokit";
				%cost = getRandom(500, 1000) + $merchantAccessed * 100;
				
			case 2:	// Adrenal Shot.
				%itemType = 5;
				%itemBitmap = "../../../Sprites/1/assets/images/Items/Item-AdrenalShot";
				%cost = getRandom(1000, 3000) + $merchantAccessed * 100;
				
			case 3:	// Canteen.
				%itemType = 7;
				%itemBitmap = "../../../Sprites/1/assets/images/Items/Item-Canteen";
				%cost = getRandom(1000, 2000) + $merchantAccessed * 100;
				
			case 4:	// Grenade.
				%itemType = 10;
				%itemBitmap = "../../../Sprites/1/assets/images/Items/Item-Grenade";
				%cost = getRandom(500, 1000) + $merchantAccessed * 100;
				
			case 5: // Armor Kit.
				%itemType = 16;
				%itemBitmap = "../../../Sprites/1/assets/images/Items/Plating";
				%cost = getRandom(1000, 3000) + $merchantAccessed * 100;
				
			case 6:	// Plastic Explosive.
				%itemType = 20;
				%itemBitmap = "../../../Sprites/1/assets/images/Items/PlasticExplosive";
				%cost = getRandom(500, 1500) + $merchantAccessed * 100;
		}
		// The fourth item has a small chance to be a "special" item!
		if (%i == 4	&&
			getRandom(1, 100) <= $MerchantSpecialItemChance)
		{
			%specialItemType = getRandom(0, 4);
			switch (%specialItemType)
			{
				case 0:	// Lasershot.
					%itemType = 17;
					%itemBitmap = "../../../Sprites/1/assets/images/Items/Lasershot";
					%cost = getRandom(4000, 5000) + $merchantAccessed * 100;
					
				case 1:	// Hulk Suit.
					%itemType = 21;
					%itemBitmap = "../../../Sprites/1/assets/images/Items/Item-HulkSuit";
					%cost = getRandom(12000, 15000) + $merchantAccessed * 100;
					
				case 2:	// Laser Mod.
					%itemType = 23;
					%itemBitmap = "../../../Sprites/1/assets/images/Items/LaserMod";
					%cost = getRandom(4000, 6000) + $merchantAccessed * 100;
					
				case 3:	// Plasma Mod.
					%itemType = 24;
					%itemBitmap = "../../../Sprites/1/assets/images/Items/PlasmaMod";
					%cost = getRandom(7000, 10000) + $merchantAccessed * 100;
					
				case 4:	// MedStim.
					%itemType = 25;
					%itemBitmap = "../../../Sprites/1/assets/images/Items/Item-MedStim";
					%cost = getRandom(9000, 11000) + $merchantAccessed * 100;
			}
		}
		%itemTooltip = getItemTypeTooltip(%itemType);
		// Set itemType, tooltip, and bitmap.
		("merchantItem" @ %itemIndex).itemType = %itemType;
		("merchantItem" @ %itemIndex).tooltip = %itemTooltip;
		("merchantItem" @ %itemIndex).setBitmap(%itemBitmap);
		// Set cost.
		("merchantItem" @ %itemIndex @ "CostText").text = %cost @ " credits";
		
		// Set visible.
		("merchantItem" @ %itemIndex).visible = 1;
		("merchantItem" @ %itemIndex @ "CostText").visible = 1;
	}
}
function buyMerchantItem (%itemIndex)
{//echo("buyMerchantItem()");
	// Get item cost.
	%itemCost = ("merchantItem" @ %itemIndex @ "CostText").text;
	%itemCost = strreplace(%itemCost, " credits", "");
	if ($squadCredits >= %itemCost)
	{
		if ($squadInventory.getCount() < $squadInventoryMaxItems)
		{
			playAudio("Audio:itemBought", EffectsAudioVolume.value);
			$squadCredits -= %itemCost;
			updatemoneycounter();
			spawnItemInPlayerInventory(("merchantItem" @ %itemIndex).itemType);
			// Hide item!
			("merchantItem" @ %itemIndex).visible = 0;
			("merchantItem" @ %itemIndex @ "CostText").visible = 0;
		}
		else
		{
			// Inform the player of lack of inventory space.
			playAudio("Audio:inventoryFull", EffectsAudioVolume.value);
		}
	}
	else
	{
		// Inform the player of lack of funds.
		playAudio("Audio:lackFunds", EffectsAudioVolume.value);
	}
}

function giveRandomMoney (%min, %max)
{
	%creditsFound = getRandom(%min, %max);
	$squadCredits += %creditsFound;
	updateMoneyCounter();
	scrollMoneyGain($playerSquad[$activeCharacter], %creditsFound);
	playAudio("Audio:moneyGain", EffectsAudioVolume.value);
}