class ItemAutoPeelInvenWnd extends UICommonAPI
	dependson(ItemAutoPeelWnd);

var WindowHandle Me;
var ItemWindowHandle ItemWnd;

static function ItemAutoPeelInvenWnd Inst()
{
	return ItemAutoPeelInvenWnd(GetScript("ItemAutoPeelInvenWnd"));
}

function Initialize()
{
	initControls();
}

function initControls()
{
	local string ownerFullPath;

	ownerFullPath = m_hOwnerWnd.m_WindowNameWithFullPath;
	Me = GetWindowHandle(ownerFullPath);
	ItemWnd = GetItemWindowHandle(ownerFullPath $ ".ItemEnchantSubWndItemWnd");
}

function UpdateInventoryWnd()
{
	local int i;
	local INT64 RemainItemNum;
	local ItemInfo tempInfo;
	local array<ItemInfo> itemInfos;
	local ItemAutoPeelWnd.ItemAutoPeelInfo ItemAutoPeelInfo;

	ItemAutoPeelInfo = class'ItemAutoPeelWnd'.static.Inst().GetItemAutoPeelInfo();
	if(Me.IsShowWindow() == false)
	{
		return;
	}
	ItemWnd.Clear();
	class'UIDATA_INVENTORY'.static.GetAllDefaultActionPeelItem(itemInfos);

	for(i = 0; i < itemInfos.Length; i++)
	{
		tempInfo = itemInfos[i];
		if(tempInfo.Id.ServerID == ItemAutoPeelInfo.targetItemSId)
		{
			RemainItemNum = tempInfo.ItemNum - ItemAutoPeelInfo.totalPeelCnt;
			if(RemainItemNum <= 0)
			{
				// [Explicit Continue]
				continue;
			}
			tempInfo.ItemNum = RemainItemNum;
		}
		tempInfo.bShowCount = true;
		tempInfo.bDisabled = 0;
		ItemWnd.AddItem(tempInfo);
	}
}

event OnRegisterEvent()
{
	RegisterEvent(EV_AdenaInvenCount);
}

event OnEvent(int Event_ID, string param)
{
	switch(Event_ID)
	{
		case EV_AdenaInvenCount:
			if(m_hOwnerWnd.IsShowWindow())
			{
				UpdateInventoryWnd();
			}
			break;
	}
}

event OnLoad()
{
	SetClosingOnESC();
	Initialize();
}

event OnShow()
{
	UpdateInventoryWnd();
}

event OnDBClickItem(string strID, int Index)
{
	local ItemInfo targetItemInfo;

	if(Index >= 0)
	{
		ItemWnd.GetItem(Index, targetItemInfo);
		class'ItemAutoPeelWnd'.static.Inst().RegisterItem(targetItemInfo.Id.ServerID, class'InputAPI'.static.IsAltPressed());
	}
}

event OnRClickItem(string strID, int Index)
{
	OnDBClickItem(strID, Index);
}

event OnReceivedCloseUI()
{
	class'ItemAutoPeelWnd'.static.Inst().CloseWindow();
}
