/***********************************************************************/
/** Witcher Script file - inventory
/***********************************************************************/
/** Copyright © 2013 CDProjektRed
/** Author : Bartosz Bigaj
/***********************************************************************/

class CR4BlacksmithMenu extends CR4MenuBase
{
	private var _disassembleInv : W3GuiDisassembleInventoryComponent;
	private var _repairInv 		: W3GuiRepairInventoryComponent;
	private var _socketInv 		: W3GuiSocketsInventoryComponent;
	private var _curInv	   		: W3GuiBaseInventoryComponent;
	private var _addSocketInv   : W3GuiAddSocketsInventoryComponent;
	
	protected var _tooltipDataProvider	: W3TooltipComponent;
	protected var _inv : CInventoryComponent;
	protected var _fixerNpc : CNewNPC;
	protected var _fixerInventory : CInventoryComponent;
	private var m_menuInited : bool;
	private var m_lastConfirmedDisassembleQuantity : int;

	private var MAX_ITEM_NR : int;
	default MAX_ITEM_NR = 64;
	private var currentItemsNr : int;
	default	currentItemsNr = 0;
	
	private var InitDataConfirmation 	: PriceConfirmationPopupData;
	private var repairAllPopupData 		: RepairAllPopupData;
	private var quantityPopupData		: QuantityPopupData;
	
	private var m_fxRemoveItem   		: CScriptedFlashFunction;
	private var m_fxConfirmAction		: CScriptedFlashFunction;
	private var m_fxSetPlayerMoney		: CScriptedFlashFunction;
	private var m_fxSetXActionLabel 	: CScriptedFlashFunction;
	
	event /*flash*/ OnConfigUI()
	{	
		var l_flashPaperdoll		: CScriptedFlashSprite;
		var l_flashInventory		: CScriptedFlashSprite;
		var l_flashObject			: CScriptedFlashObject;
		var l_flashArray			: CScriptedFlashArray;
		var l_obj		 			: IScriptable;
		var l_fixerEntity			: CGameplayEntity;		
		var l_initData				: W3InventoryInitData;
		var hasUpgrades				: bool;
		var items 					: array<SItemUniqueId>;
		var i 						: int;
		
		m_menuInited = false;
		super.OnConfigUI();
		
		m_initialSelectionsToIgnore = 0;
		
		l_obj = GetMenuInitData();
		l_initData = (W3InventoryInitData)l_obj;
		
		if (l_initData)
		{
			l_fixerEntity = l_initData.containerNPC;
		}
		else
		{
			l_fixerEntity = (CGameplayEntity)l_obj;
		}		
		if ( l_fixerEntity )
		{
			_fixerNpc = (CNewNPC)l_fixerEntity;
			_fixerInventory = l_fixerEntity.GetInventory();
		}
		
		_inv = thePlayer.GetInventory();
		m_flashModule = GetMenuFlash();
		m_fxConfirmAction = m_flashModule.GetMemberFlashFunction( "confirmAction" );
		m_fxSetPlayerMoney = m_flashModule.GetMemberFlashFunction( "setPlayerMoney" );
		m_fxRemoveItem = m_flashModule.GetMemberFlashFunction( "removeItem" );
		m_fxSetXActionLabel = m_flashModule.GetMemberFlashFunction( "setXActionLabel" );
		
		_tooltipDataProvider = new W3TooltipComponent in this;
		_tooltipDataProvider.initialize(_inv, m_flashValueStorage);
		_tooltipDataProvider.setCurrentInventory(_inv);
		
		// repair inv:
		_repairInv = new W3GuiRepairInventoryComponent in this;
		_repairInv.Initialize( _inv );
		_repairInv.merchantInv = _fixerInventory;
		
		// #Y can do everything
		_repairInv.repairArmors = true;
		_repairInv.repairSwords = true;
		_repairInv.masteryLevel = 5;
		
		// sockets inv:
		_socketInv = new W3GuiSocketsInventoryComponent in this;
		_socketInv.Initialize( _inv );
		_socketInv.SetSocketsFilter(true);
		_socketInv.merchantInv = _fixerInventory;
		
		// add sockets inv:
		_addSocketInv = new W3GuiAddSocketsInventoryComponent in this;
		_addSocketInv.Initialize( _inv );
		_addSocketInv.merchantInv = _fixerInventory;
		_addSocketInv.ignorePosition = true;
		
		// disassemble inv:
		_disassembleInv = new W3GuiDisassembleInventoryComponent in this;
		_disassembleInv.Initialize( _inv );
		_disassembleInv.merchantInv = _fixerInventory;
		
		m_menuInited = true;
		ApplyMenuState(m_menuState);
		
		//UpdateData();
		m_flashValueStorage.SetFlashString("repair.grid.player.name", GetLocStringByKeyExt("panel_inventory_grid_name"));
	}
	
	public /* override */ function SetMenuState(newState : name) : void
	{
		var i : int;
		var hasUpgrades : bool;
		var items : array<SItemUniqueId>;
		var numEnhancements : int;
		var hasEnchantment : bool;
		
		if (newState == 'Sockets')
		{
			m_initialSelectionsToIgnore = 1;
		}
		
		super.SetMenuState(newState);
		if (m_menuInited)
		{
			ApplyMenuState(newState);
		}
		
		if(m_menuState == 'Sockets' && ShouldProcessTutorial('TutorialUpgRemovalDescription'))
		{
			thePlayer.inv.GetAllItems(items);
			hasUpgrades = false;
			
			// Search for non-enchantment ( upgrade ) enhancements.
			for(i=0; i<items.Size(); i+=1)
			{
				hasEnchantment = thePlayer.inv.GetEnchantment( items[ i ] ) != '';
				numEnhancements = thePlayer.inv.GetItemEnhancementCount( items[ i ] ); // Returns '3' when using Rune/glyphwords
				if( numEnhancements > 0 && !hasEnchantment )
				{
					hasUpgrades = true;
					break;
				}
			}
			
			GameplayFactsSet("tutorial_upg_removal_cond", (int)hasUpgrades);
			theGame.GetTutorialSystem().uiHandler.OnOpeningMenu(GetMenuName());
		}
		else if(m_menuState == 'Disassemble' && ShouldProcessTutorial('TutorialDismantleDescription'))
		{
			items = _curInv.GetAllItems();
			for(i=0; i<items.Size(); i+=1)
			{
				if(_curInv.ShouldShowItem(items[i]))
				{
					//has something to show on the grid
					GameplayFactsSet("tut_dismantle_cond", 1);				
					theGame.GetTutorialSystem().uiHandler.OnOpeningMenu(GetMenuName());
				
					break;
				}
			}
		}
	}
	
	protected function ApplyMenuState(newState : name) : void
	{
		switch (newState)
		{
			case 'Repair':
				_curInv = _repairInv;
				break;
			case 'Sockets':
				_curInv = _socketInv;
				break;
			case 'Disassemble':
				_curInv = _disassembleInv;
				break;
			case 'AddSockets':
				_curInv = _addSocketInv;
				break;
			default:
				break;
		}
		
		UpdateRepairAllInputFeedback(newState);
		
		UpdateData();
	}
	
	protected function UpdateRepairAllInputFeedback(newState : name) : void
	{
		var repairString : string;
		var totalRepairCost : int;
		repairString = "";
		
		if (newState == 'Repair')
		{
			totalRepairCost = _repairInv.GetTotalRepairCost();
			
			if (totalRepairCost > 0)
			{
				repairString = GetLocStringByKeyExt("panel_button_common_repair_all") + " (" + totalRepairCost + ")";
			}
		}
		
		m_fxSetXActionLabel.InvokeSelfOneArg(FlashArgString(repairString));
	}
	
	event /*flash*/ OnClearSlotNewFlag(item : SItemUniqueId)
	{
		RemoveNewItemMark(item);
	}
	
	event /*flash*/ OnPlayDeniedSound()
	{
		OnPlaySoundEvent( "gui_global_denied" );
	}
	
	function RemoveNewItemMark(item : SItemUniqueId)
	{
		if (_inv.IsIdValid(item))
		{
			_curInv.ClearItemIsNewFlag(item);
		}
	}
	
	public function HandleActionConfirmation(value:bool):void
	{
		m_fxConfirmAction.InvokeSelfOneArg(FlashArgBool(value));
	}
	
	event /*flash*/ OnRequestConfirmation( itemId : SItemUniqueId, price : int )
	{
		if ( m_menuState == 'Disassemble' && _inv.GetItemQuantity(itemId) > 1 )
		{
			if (_inv.GetMoney() < price) // #J if you can't even afford one stack, don't bother opening quantity popup
			{
				showNotification( GetLocStringByKeyExt( "panel_shop_notification_not_enough_money" ) );
				HandleActionConfirmation(false);
				OnPlaySoundEvent( "gui_global_denied" );
			}
			else
			{
				ShowDisassembleQuantityPopup( itemId, price );
			}
		}
		else
		{
			ShowConfirmationPopup(itemId, price, 1);
		}
	}
	
	public function ShowConfirmationPopup( itemId : SItemUniqueId, price : int, quantity : int )
	{
		var confirmationTitle 	: string;
		var confirmationText  	: string;
		var costRepairPoint   	: int;
		var invItem 			: SInventoryItem;
		var playerMoney	 		: int;
		
		playerMoney = _inv.GetMoney();
		
		invItem = _inv.GetItem( itemId );
		
		switch (m_menuState)
		{
			case 'Repair':
				confirmationText = "panel_repair_popup_repair_item_text";
				confirmationTitle = "panel_button_common_repair";
				break;
			case 'Sockets':
				confirmationText = "panel_blacksmith_remove_improvement_confirmation";
				confirmationTitle = "panel_title_blacksmith_sockets";
				break;
			case 'Disassemble':
				confirmationText = "panel_repair_popup_disassemble_item_text";
				confirmationTitle = "panel_title_blacksmith_disassamble";
				m_lastConfirmedDisassembleQuantity = quantity;
				break;				
			case 'AddSockets':
				confirmationText = "panel_blacksmith_add_socket_confirmation";
				confirmationTitle = "panel_title_blacksmith_add_sockets";
				break;
			default:
				confirmationText = "";
				confirmationTitle = "";
				break;
		}
		
		if ( playerMoney >= price )
		{
			if (m_lastConfirmedDisassembleQuantity > 1)
			{
				HandleActionConfirmation(true);
			}
			else
			{
				InitDataConfirmation = new PriceConfirmationPopupData in this;
				InitDataConfirmation.menuRef = this;
				InitDataConfirmation.BlurBackground = true;
				
				if ( confirmationText != "" )
				{
					InitDataConfirmation.SetMessageText( GetLocStringByKeyExt( confirmationText ) );
				}
				
				if ( confirmationTitle != "" )
				{
					InitDataConfirmation.SetMessageTitle( GetLocStringByKeyExt( confirmationTitle ) );
				}
				
				InitDataConfirmation.SetPrice( price );
				
				RequestSubMenu( 'PopupMenu', InitDataConfirmation );
			}
		}
		else
		{
			showNotification( GetLocStringByKeyExt( "panel_shop_notification_not_enough_money" ) );
			HandleActionConfirmation(false);
			OnPlaySoundEvent( "gui_global_denied" );
		}
	}
	
	public function OnDisassembleStack( itemId : SItemUniqueId, price : int, quantity : int )
	{
		var playerMoney	 		: int;
		
		playerMoney = _inv.GetMoney();
		
		if ( playerMoney >= price )
		{
			m_lastConfirmedDisassembleQuantity = quantity;
			HandleActionConfirmation(true);
		}
		else
		{
			showNotification( GetLocStringByKeyExt( "panel_shop_notification_not_enough_money" ) );
			HandleActionConfirmation(false);
			OnPlaySoundEvent( "gui_global_denied" );
		}
	}
	
	private function ShowDisassembleQuantityPopup( itemId : SItemUniqueId, price : int )
	{
		var maxValue : int;
		
		maxValue = _inv.GetItemQuantity(itemId);
		
		if ( quantityPopupData )
		{
			delete quantityPopupData;
		}
		
		quantityPopupData = new QuantityPopupData in this;
		
		quantityPopupData.showPrice = true;
		quantityPopupData.itemCost = price;
		quantityPopupData.itemId = itemId;
		quantityPopupData.minValue = 1;
		quantityPopupData.currentValue = _inv.GetItemQuantity(itemId);
		quantityPopupData.maxValue = _inv.GetItemQuantity(itemId);
		quantityPopupData.actionType = QTF_Dismantle;
		quantityPopupData.blacksmithRef = this;
		RequestSubMenu( 'PopupMenu', quantityPopupData );
	}
	
	event /*flash*/ OnRemoveImprovements(item : SItemUniqueId, price : int)
	{
		var socketItems	  : array<name>;
		var itemsListText : string;
		var idx, len	  : int;
		
		_inv.GetItemEnhancementItems(item, socketItems);
		itemsListText = GetLocStringByKeyExt("panel_blacksmith_items_added");
		len = socketItems.Size();
		for (idx = 0; idx < len; idx+=1)
		{
			itemsListText += ("<br/> +" + GetLocStringByKeyExt(_inv.GetItemLocalizedNameByName(socketItems[idx])));
		}
		
		_fixerInventory.AddMoney(price);
		_inv.RemoveMoney(price);
		_inv.RemoveAllItemEnhancements(item);
		
		if (_inv.GetItemQuantity(item) > 1)
		{
			UpdateItem(item);
		}
		else
		{
			RemoveItem(item);
		}
		
		UpdatePlayerMoney();
		UpdateMerchantData();
		theSound.SoundEvent( 'gui_inventory_buy' );		
	}
	
	event /*flash*/ OnDisassembleItem( item : SItemUniqueId, price : int)
	{
		var partList	  		: array<SItemParts>;
		var currentPartList 	: array<SItemParts>;
		var runesList	  		: array<name>;
		var itemsListText 		: string;
		var idx, len			: int;
		var i, x				: int;
		var entryFound			: bool;
		var itemsCount	  		: int;
		var craftComp			: W3CraftsmanComponent;
		
		var itemsAdded			: array<SItemUniqueId>;
		var itemsToUpdate		: array<SItemUniqueId>;
		var curPart				: SItemUniqueId;
		
		itemsListText = "<font face=\"$BoldFont\">" + GetLocStringByKeyExt("panel_blacksmith_items_removed") + ": </font>";
		itemsListText += "<br/>" + (GetLocStringByKeyExt(_inv.GetItemLocalizedNameByUniqueID(item)) + " x" + m_lastConfirmedDisassembleQuantity);
		
		partList = _inv.GetItemRecyclingParts(item);
		len = partList.Size();
		for (idx = 0; idx < len; idx+=1)
		{
			partList[idx].quantity = _inv.GetItemQuantityByName(partList[idx].itemName);
		}
		
		itemsListText += "<br/>" + "<font face=\"$BoldFont\">" + GetLocStringByKeyExt("panel_blacksmith_items_added") + ": </font>";		
		
		_fixerInventory.AddMoney(price * m_lastConfirmedDisassembleQuantity);
		_inv.RemoveMoney(price * m_lastConfirmedDisassembleQuantity);
		if ( _inv.GetItemEnhancementCount(item) > 0 )
		{
			_inv.GetItemEnhancementItems(item, runesList);
			for (idx = 0; idx <  runesList.Size(); idx+=1)
			{
				_inv.AddAnItem( runesList[idx] );
				itemsListText += "<br/>" + (GetLocStringByKeyExt(_inv.GetItemLocalizedNameByName(runesList[idx])) + " x1");
			}		
		}
		
		craftComp = (W3CraftsmanComponent)_fixerNpc.GetComponentByClassName( 'W3CraftsmanComponent' );
		
		for (i = 0; i < m_lastConfirmedDisassembleQuantity; i += 1)
		{
			if ( craftComp )
			{
				if ( craftComp.IsCraftsmanType( ECT_Smith ) )
				{
					itemsAdded = _inv.RecycleItem(item, craftComp.GetCraftsmanLevel( ECT_Smith ) );
				}
				else if ( craftComp.IsCraftsmanType( ECT_Armorer ) )
				{
					itemsAdded = _inv.RecycleItem(item, craftComp.GetCraftsmanLevel( ECT_Armorer ) );
				}
				else
				{
					itemsAdded = _inv.RecycleItem( item, ECL_Journeyman );
				}
			}
			else
			{
				itemsAdded = _inv.RecycleItem( item, ECL_Journeyman );
			}
		}
		

		for (idx = 0; idx < len; idx+=1)
		{
			itemsCount = _inv.GetItemQuantityByName(partList[idx].itemName) - partList[idx].quantity;
			itemsListText += "<br/>" + GetLocStringByKeyExt(_inv.GetItemLocalizedNameByName(partList[idx].itemName)) + " x" + itemsCount;
		}
		
		showNotification(itemsListText);
		
		if (_inv.GetItemQuantity(item) > 0)
		{
			itemsToUpdate.PushBack(item);
		}
		else
		{
			RemoveItem(item);
		}
		
		len = itemsAdded.Size();
		for (idx = 0; idx < len; idx+=1)
		{
			curPart = itemsAdded[idx];
			partList.Clear();
			partList = _inv.GetItemRecyclingParts( curPart );
			if (partList.Size() > 0)
			{
				itemsToUpdate.PushBack(curPart);
			}
		}
		
		if (itemsToUpdate.Size() > 0)
		{
			UpdateItemsList(itemsToUpdate);
		}
		
		UpdatePlayerMoney();
		UpdateMerchantData();
		
		theSound.SoundEvent( 'gui_inventory_buy' );
	}
	
	event /*flash*/ OnStartCrafting()
	{
		OnPlaySoundEvent("gui_crafting_craft_item");
	}
	
	event /*flash*/ OnRepairItem( item : SItemUniqueId, price : int )
	{
		_repairInv.RepairItem( item, 1 );
		_fixerInventory.AddMoney(price);
		_inv.RemoveMoney(price);
		
		if (_inv.GetItemQuantity(item) > 1)
		{
			UpdateItem(item);
		}
		else
		{
			RemoveItem(item);
		}
		
		UpdatePlayerMoney();
		UpdateMerchantData();
		
		theSound.SoundEvent( 'gui_inventory_buy' );
		theSound.SoundEvent( 'gui_inventory_repair' );
		
		UpdateRepairAllInputFeedback('Repair');
	}
	
	event /*flash*/ OnAddSocket( item : SItemUniqueId, price : int)
	{
		_addSocketInv.AddSocket( item );
		
		_fixerInventory.AddMoney(price);
		_inv.RemoveMoney(price);
		
		UpdateItem(item);
		
		UpdatePlayerMoney();
		UpdateMerchantData();
		
		theSound.SoundEvent( 'gui_enchanting_socket_add' );
	}
	
	event /*flash*/ OnRepairAllItems()
	{
		var totalRepairCost : int;
		
		totalRepairCost = _repairInv.GetTotalRepairCost();
		
		if (_inv.GetMoney() < totalRepairCost) // #J if you can't even afford one stack, don't bother opening quantity popup
		{
			showNotification( GetLocStringByKeyExt( "panel_shop_notification_not_enough_money" ) );
			HandleActionConfirmation(false);
			OnPlaySoundEvent( "gui_global_denied" );
		}
		else
		{
			repairAllPopupData = new RepairAllPopupData in this;
			repairAllPopupData.menuRef = this;
			repairAllPopupData.BlurBackground = true;
				
			repairAllPopupData.SetPrice( totalRepairCost );
			
			RequestSubMenu( 'PopupMenu', repairAllPopupData );
			
		}
	}
	
	public function RepairAll()
	{
		var emptyString : string;
		var totalRepairCost : int;
		
		emptyString = "";
		totalRepairCost = _repairInv.GetTotalRepairCost();
		
		_repairInv.RepairAllItems(1);
		
		_fixerInventory.AddMoney(totalRepairCost);
		_inv.RemoveMoney(totalRepairCost);
		UpdatePlayerMoney();
		UpdateMerchantData();
		
		theSound.SoundEvent( 'gui_inventory_buy' );
		theSound.SoundEvent( 'gui_inventory_repair' );
		
		InventoryUpdateAll();
		m_fxSetXActionLabel.InvokeSelfOneArg(FlashArgString(emptyString));
	}
	
	event /* flash */ OnGetItemData(item : SItemUniqueId, compareItemType : int)
	{
		ShowItemTooltip(item, compareItemType);
	}
	
	event /*flash*/ OnGlobalUpdate()
	{
		UpdateData();
	}
		
	event /*flash*/ OnSaveItemGridPosition( item : SItemUniqueId, gridPos : int )
	{
		var UIData : SInventoryItemUIData;
		UIData = _inv.GetInventoryItemUIData( item );
		UIData.gridPosition = gridPos;
		//_inv.SetInventoryItemUIData( item , UIData );
	}
	
	/*
		--- UPDATE DATA ---
	*/
	
	function UpdateData()
	{
		InventoryUpdateAll();
		UpdateItemsCounter();
		UpdateMerchantData();
		m_fxSetPlayerMoney.InvokeSelfOneArg( FlashArgInt( thePlayer.GetMoney() ) );
	}
	
	function InventoryUpdateAll()
	{
		var l_flashObject			: CScriptedFlashObject;
		var l_flashArray			: CScriptedFlashArray;
		
		if (_curInv)
		{
			l_flashObject = m_flashValueStorage.CreateTempFlashObject();
			l_flashArray = m_flashValueStorage.CreateTempFlashArray();		
			_curInv.GetInventoryFlashArray(l_flashArray, l_flashObject);		
			m_flashValueStorage.SetFlashArray( "repair.grid.player", l_flashArray );
		}
	}
	
	function UpdateMerchantData() : void
	{
		var l_merchantData			: CScriptedFlashObject;
		l_merchantData = m_flashValueStorage.CreateTempFlashObject();
		GetNpcInfo((CGameplayEntity)_fixerNpc, l_merchantData);
		m_flashValueStorage.SetFlashObject("blacksmith.merchant.info", l_merchantData);
	}
	
	function InventoryUpdateItems( itemsList : array<SItemUniqueId>  )
	{
		var i					: int;
		var tempFlashObject		: CScriptedFlashObject;
		var itemDataObject		: CScriptedFlashObject;
		var itemsDataList		: CScriptedFlashArray;
		
		if (_curInv)
		{
			itemsDataList = m_flashValueStorage.CreateTempFlashArray();
			tempFlashObject = m_flashValueStorage.CreateTempFlashObject();
			for ( i = 0; i < itemsList.Size(); i += 1 )
			{
				itemDataObject = tempFlashObject.CreateFlashObject("red.game.witcher3.menus.common.ItemDataStub");
				_curInv.SetInventoryFlashObjectForItem(itemsList[i], itemDataObject);
				itemsDataList.PushBackFlashObject(itemDataObject);
			}
			m_flashValueStorage.SetFlashArray( "repair.grid.player.itemsUpdate", itemsDataList );
		}
	}
	
	function UpdatePlayerMoney()
	{
		var commonMenu 				: CR4CommonMenu;
		commonMenu = (CR4CommonMenu)m_parentMenu;
		
		if( commonMenu )
		{
			commonMenu.UpdatePlayerOrens();
		}
	}
	
	private function UpdateItemsList( itemsList : array<SItemUniqueId> ):void
	{
		var tempFlashObject : CScriptedFlashObject;
		var tempFlashArray  : CScriptedFlashArray;
		var itemDataObject	: CScriptedFlashObject;
		var idx, len:int;
		
		tempFlashObject = m_flashValueStorage.CreateTempFlashObject();
		tempFlashArray = m_flashValueStorage.CreateTempFlashArray();
		len = itemsList.Size();
		for (idx = 0; idx < len; idx +=1)
		{
			itemDataObject = tempFlashObject.CreateFlashObject("red.game.witcher3.menus.common.ItemDataStub");
			_curInv.SetInventoryFlashObjectForItem(itemsList[idx], itemDataObject);
			tempFlashArray.PushBackFlashObject(itemDataObject);
		}
		
		m_flashValueStorage.SetFlashArray( "blacksmith.item.list.update", tempFlashArray );
	}
	
	private function UpdateItem( item : SItemUniqueId )
	{
		var tempFlashObject		: CScriptedFlashObject;
		var itemDataObject		: CScriptedFlashObject;
		
		if (_curInv)
		{
			tempFlashObject = m_flashValueStorage.CreateTempFlashObject();
			itemDataObject = tempFlashObject.CreateFlashObject("red.game.witcher3.menus.common.ItemDataStub");
			_curInv.SetInventoryFlashObjectForItem(item, itemDataObject);
			m_flashValueStorage.SetFlashObject( "blacksmith.item.update", itemDataObject );
		}
	}
	
	private function RemoveItem( item : SItemUniqueId ):void
	{
		m_fxRemoveItem.InvokeSelfTwoArgs( FlashArgUInt(ItemToFlashUInt(item)), FlashArgBool( false ));
	}
	
	private function UpdateItemsCounter()
	{
		var itemsNr : int;
		var commonMenu 				: CR4CommonMenu;
		
		commonMenu = (CR4CommonMenu)m_parentMenu;
		if( commonMenu )
		{
			itemsNr = commonMenu.UpdateItemsCounter();
		}
		
		if( itemsNr >= MAX_ITEM_NR && currentItemsNr != itemsNr )
		{
			currentItemsNr = itemsNr;
			//OnPlaySoundEvent("gui_inventory_overweighted");
		}
	}
	
	function GetCurrentInventory():CInventoryComponent
	{
		return _inv;
	}
	
	function GetItemDefaultActionName( item : SItemUniqueId ) : string
	{
		var itemAction : EInventoryActionType;
		itemAction = GetItemDefaultAction(item);
		
		return GetItemActionFriendlyName(itemAction,GetWitcherPlayer().IsItemEquipped(item));
	}
	
	function GetItemDefaultAction( item : SItemUniqueId ) : EInventoryActionType
	{
		return IAT_Repair;
	}
	
	event /*flash*/ OnCloseMenu()
	{
		CloseMenu();
		
		if( m_parentMenu )
		{
			m_parentMenu.ChildRequestCloseMenu();
		}
	}
	
	event /* C++ */ OnClosingMenu()
	{
		if (_repairInv)
		{
			delete _repairInv;
		}
		if (_socketInv)
		{
			delete _socketInv;
		}
		if (InitDataConfirmation)
		{
			delete InitDataConfirmation;
		}
		if (quantityPopupData)
		{
			delete quantityPopupData;
		}
		if (repairAllPopupData)
		{
			delete repairAllPopupData;
		}
		
		theGame.GetGuiManager().SetLastOpenedCommonMenuName( GetMenuName() );
		super.OnClosingMenu();		
	}

	// Stub
	event /*flash*/ OnMoveItem( item : SItemUniqueId, moveToIndex : int )
	{
	}

	// Stub
	event /*flash*/ OnMoveItems( item : SItemUniqueId, moveToIndex : int, itemSecond : SItemUniqueId, moveToSecondIndex : int )
	{
	}
	
	event /*flash*/ OnPlaySound( soundKey : string )
	{
		theSound.SoundEvent( soundKey );
	}
	
	// Stub, remove?
	event /*flash*/ OnSetCurrentPlayerGrid( value : string )
	{
		// TODO:
	}
	
	// Stub, remove?
	event /*flash*/ OnEquipItem( item : SItemUniqueId, slot : int, quantity : int )
	{
		// TODO:
	}
	
	
	/*
		TOOLTIP
	*/

	public function ShowItemTooltip(item : SItemUniqueId, compareItemType : int)
	{
		var tooltipData : CScriptedFlashObject;
		tooltipData = _tooltipDataProvider.GetTooltipData(item, true, true);
		m_flashValueStorage.SetFlashObject("context.tooltip.data", tooltipData);
	}
	
	event /*flash*/ OnShowItemPopup( item : SItemUniqueId )
	{
		var initData : ItemInfoPopupData;
		
		initData = new ItemInfoPopupData in this;
		initData.invRef = GetCurrentInventory();
		initData.itemId = item;
		RequestSubMenu('PopupMenu', initData);	
	}
	
	function PlayOpenSoundEvent()
	{
		// Common Menu takes care of this for us
		//OnPlaySoundEvent("gui_global_panel_open");	
	}
	
	event /*flash*/ OnAppendGFxButton(actionId:int, gamepadNavCode:String, keyboardKeyCode:int, label:String, holdPrefix:bool)
	{
		var newButtonDef:SKeyBinding;
		
		RemoveGFxButtonById(actionId);
		newButtonDef.ActionID = actionId;
		newButtonDef.Gamepad_NavCode = gamepadNavCode;
		newButtonDef.Keyboard_KeyCode = keyboardKeyCode;
		
		if (holdPrefix)
		{
			newButtonDef.LocalizationKey = GetHoldLabel() + " " + GetLocStringByKeyExt(label);
			newButtonDef.IsLocalized = true;
		}
		else
		{
			newButtonDef.LocalizationKey = label;
		}
		
		if (actionId == 1002)
		{
			newButtonDef.IsLocalized = true;
		}
		
		m_GFxInputBindings.PushBack(newButtonDef);
	}
}


/*
	Price confirmation popup
*/

class PriceConfirmationPopupData extends ConfirmationPopupData
{
	private var m_Price : float;
	public var menuRef : CR4BlacksmithMenu; 

	public /* override */ function GetGFxData(parentFlashValueStorage : CScriptedFlashValueStorage) : CScriptedFlashObject
	{
		var l_flashObject : CScriptedFlashObject;
		
		l_flashObject = parentFlashValueStorage.CreateTempFlashObject();
		l_flashObject.SetMemberFlashString("ContentRef", GetContentRef());
		l_flashObject.SetMemberFlashString("TextContent", m_TextContent);
		l_flashObject.SetMemberFlashString("TextTitle", m_TextTitle);
		l_flashObject.SetMemberFlashNumber("ItempPrice", m_Price);
		return l_flashObject;
	}
	
	public function SetPrice( value : float ) : void
	{
		m_Price = value;
	}
	
	protected function GetContentRef() : string
	{
		return "PriceConfirmationPopupRef";
	}
	
	protected function OnUserAccept() : void
	{
		menuRef.HandleActionConfirmation(true);
		ClosePopup();
	}
	
	protected function OnUserDecline() : void
	{
		menuRef.HandleActionConfirmation(false);
		ClosePopup();
	}
}

class RepairAllPopupData extends ConfirmationPopupData
{
	private var m_Price : float;
	public var menuRef : CR4BlacksmithMenu; 

	public /* override */ function GetGFxData(parentFlashValueStorage : CScriptedFlashValueStorage) : CScriptedFlashObject
	{
		var l_flashObject : CScriptedFlashObject;
		
		l_flashObject = parentFlashValueStorage.CreateTempFlashObject();
		l_flashObject.SetMemberFlashString("ContentRef", GetContentRef());
		l_flashObject.SetMemberFlashString("TextContent", "");
		l_flashObject.SetMemberFlashString("TextTitle", GetLocStringByKeyExt("panel_button_common_repair_all"));
		l_flashObject.SetMemberFlashNumber("ItempPrice", m_Price);
		return l_flashObject;
	}
	
	public function SetPrice( value : float ) : void
	{
		m_Price = value;
	}
	
	protected function GetContentRef() : string
	{
		return "PriceConfirmationPopupRef";
	}
	
	protected function OnUserAccept() : void
	{
		menuRef.RepairAll();
		ClosePopup();
	}
	
	protected function OnUserDecline() : void
	{
		ClosePopup();
	}
}

// DEBUG
exec function repairmenu()
{
	theGame.RequestMenuWithBackground( 'BlacksmithMenu', 'CommonMenu' );
}
