class WorldExchangeRegiSubWnd extends UICommonAPI;

var ItemWindowHandle ItemWnd;

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

function Initialize()
{
	ItemWnd = GetItemWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".itemEnchantSubWndItemWnd");
	GetTextBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".DescriptionMsgWnd.descTextBox").SetText(GetSystemMessage(4222));
}

event OnRegisterEvent()
{
	RegisterEvent(EV_AdenaInvenCount);
}

event OnLoad()
{
	Initialize();
}

event OnShow()
{
	refresh();
}

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

event OnDBClickItem(string ControlName, int Index)
{
	local ItemInfo iInfo;

	ItemWnd.GetItem(Index, iInfo);
	iInfo.DragSrcName = "itemEnchantSubWndItemWnd";
	class'WorldExchangeRegiWnd'.static.Inst().OnDropItem("", iInfo, 0, 0);
}

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

function _Show()
{
	m_hOwnerWnd.ShowWindow();
	refresh();
}

function _Hide()
{
	m_hOwnerWnd.HideWindow();
}

private function refresh()
{
	SetSellItems();
}

private function RefreshItemNum();

function _ResetSellItem()
{
	local int idx;
	local ItemInfo iInfo, inveniInfo, sellItemInfo;

	class'WorldExchangeRegiWnd'.static.Inst()._GetSellItemInfo(sellItemInfo);
	if(! class'UIDATA_INVENTORY'.static.HasItem(sellItemInfo.Id.ServerID))
	{
		class'WorldExchangeRegiWnd'.static.Inst().DelItemInfo();
		refresh();
		return;
	}
	class'UIDATA_INVENTORY'.static.FindItem(sellItemInfo.Id.ServerID, inveniInfo);
	idx = ItemWnd.FindItem(sellItemInfo.Id);
	if(idx == -1)
	{
		refresh();
		return;		
	}
	else
	{
		ItemWnd.GetItem(idx, iInfo);
		refresh();
		return;
	}
	iInfo.ItemNum = inveniInfo.ItemNum - sellItemInfo.ItemNum;
	ItemWnd.SetItem(idx, iInfo);
}

function SetSellItems()
{
	local int i;
	local ItemInfo sellItemInfo;
	local array<ItemInfo> iInfos;

	ItemWnd.Clear();
	iInfos = GetAllItemInfo();
	class'WorldExchangeRegiWnd'.static.Inst()._GetSellItemInfo(sellItemInfo);

	for(i = 0; i < iInfos.Length; i++)
	{
		if(IsAdena(iInfos[i].Id))
		{			
		}
		else if(! iInfos[i].bIsAuctionAble)
		{
			// [Explicit Continue]
			continue;
		}
		if(sellItemInfo.Id == iInfos[i].Id)
		{
			iInfos[i].ItemNum = iInfos[i].ItemNum - sellItemInfo.ItemNum;
		}
		if(iInfos[i].ItemNum == 0)
		{
			// [Explicit Continue]
			continue;
		}
		if(iInfos[i].bSecurityLock)
		{
			// [Explicit Continue]
			continue;
		}
		if(iInfos[i].CurrentPeriod > 0)
		{
			// [Explicit Continue]
			continue;
		}
		iInfos[i].bShowCount = IsStackableItem(iInfos[i].ConsumeType);
		ItemWnd.AddItem(iInfos[i]);
	}
	SetDescTextBox();
}

function array<ItemInfo> GetAllItemInfo()
{
	local array<ItemInfo> allItem;

	class'UIDATA_INVENTORY'.static.GetAllInvenItem(allItem);
	return allItem;
}

function SetDescTextBox()
{
	if(ItemWnd.GetItemNum() == 0)
	{
		GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".DescriptionMsgWnd").ShowWindow();		
	}
	else
	{
		GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".DescriptionMsgWnd").HideWindow();
	}
}
