class InventoryViewer extends UICommonAPI;

struct InvenCountData
{
	var int curCnt;
	var int maxCnt;
};

var private ItemWindowHandle totalInven_ItemWnd;
var private int baseWidth;
var private int BaseHeight;
var private InventoryWnd inventoryWndScript;
var private TextBoxHandle InvenoryCount;
var private TextBoxHandle AdenaText;
var private WindowHandle ParentWindow;
var private ButtonHandle HelpButton;
var private InvenCountData invenCountDatainfo;

function Initialize()
{
	totalInven_ItemWnd = GetItemWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".InventoryItem_ItemWnd");
	InvenoryCount = GetTextBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".InventoryCount_TextBox");
	AdenaText = GetTextBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".AdenaText");
	HelpButton = GetButtonHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".HelpButton");
	inventoryWndScript = InventoryWnd(GetScript("inventoryWnd"));	
}

event OnRegisterEvent()
{
	RegisterEvent(EV_InventoryClear);
	RegisterEvent(EV_AdenaInvenCount);
	RegisterEvent(EV_SetMaxCount);
	RegisterEvent(EV_InventoryUpdateItem);
}

event OnLoad()
{
	Initialize();
}

event OnLButtonDown(WindowHandle a_WindowHandle, int nX, int nY)
{
	// Debug("a_WindowHandle-> " @ a_WindowHandle.GetWindowName() @ nX @ nY);
	ParentWindow.SetFocus();
	m_hOwnerWnd.SetFocus();
}

event OnEvent(int Event_ID, string param)
{
	//debug("Inven Event ID :" $string(Event_ID)$" "$param);
	switch( Event_ID )
	{
		case EV_AdenaInvenCount:
			HandleEV_AdenaInvenCount(param);
			break;
		case EV_SetMaxCount:       //2070
			HandleEV_SetMaxCount(param);
	    break;
	    case EV_InventoryClear:
			totalInven_ItemWnd.Clear();
	    break;
		case EV_InventoryUpdateItem:
			HandleUpdateItem(param);
			break;
	}
}

event OnShow()
{
	SetAdenaText();
	SetItemCount();
	syncInventory();
}

event onClickButton ( string StrID )
{
	if ( StrID == "CloseButton")  m_hOwnerWnd.HideWindow();
}

//function OnSetFocus( WindowHandle a_WindowHandle, bool bFocused )
//{
//	Debug("a_WindowHandle " @ a_WindowHandle.GetWindowName() @ bFocused);

//	if(a_WindowHandle.GetWindowName() == Me.getWin && bFocused == true)
//	{
//		if(ParentWindow.GetWindowName() != "")
//		{
//			ParentWindow.SetFocus();		
//			Me.SetFocus();
//		}
//	}   
//}

event OnTick()
{
	HandleOnTick();	
}

private function HandleOnTick()
{
	syncInventory();
	m_hOwnerWnd.DisableTick();	
}

private function ValidateInventoryAdd()
{
	m_hOwnerWnd.EnableTick();	
}

private function HandleUpdateItem(string param)
{
	local string Type;

	ParseString(param, "type", Type);
	if(Type == "add")
	{
		ValidateInventoryAdd();
		SetItemCount();
		return;		
	}
	else if(Type == "update")
	{
		UpdateItem(param);			
	}
	else if(Type == "delete")
	{
		DeleteItem(param);
		SetItemCount();
	}
}

private function UpdateItem(string param)
{
	local int Index;
	local ItemInfo iInfo;

	ParamToItemInfo(param, iInfo);
	Index = totalInven_ItemWnd.FindItem(iInfo.Id);
	if(iInfo.bEquipped)
	{
		iInfo.ForeTexture = "L2UI_CT1.Icon.WearPanel";
	}
	setShowItemCount(iInfo);
	totalInven_ItemWnd.SetItem(Index, iInfo);	
}

private function DeleteItem(string param)
{
	local int Index;
	local ItemInfo iInfo, kClearItem;

	kClearItem.IconName = "L2ui_ct1.emptyBtn";
	ClearItemID(kClearItem.Id);
	ParamToItemInfo(param, iInfo);
	Index = totalInven_ItemWnd.FindItem(iInfo.Id);
	if(Index < 0)
	{
		return;
	}
	totalInven_ItemWnd.DeleteItem(Index);
	totalInven_ItemWnd.AddItem(kClearItem);	
}

private function HandleEV_AdenaInvenCount(string param)
{
	local int curCnt;

	SetAdenaText();
	ParseInt(param, "InvenCount", curCnt);
	if(invenCountDatainfo.curCnt == curCnt)
	{
		return;
	}
	invenCountDatainfo.curCnt = curCnt;
	if(! m_hOwnerWnd.IsShowWindow())
	{
		return;
	}
	SetItemCount();	
}

private function HandleEV_SetMaxCount(string param)
{
	local int maxCnt;

	ParseInt(param, "Inventory", maxCnt);
	if(invenCountDatainfo.maxCnt == maxCnt)
	{
		return;
	}
	invenCountDatainfo.maxCnt = maxCnt;
	if(! m_hOwnerWnd.IsShowWindow())
	{
		return;
	}
	SetItemCount();
	syncInventory();	
}

// ¢à¡ì¡¾¬Ó, ¬¢¬ï¬ª??¢ç ¡Æ¬î¡¤¬¤ ?¬¨¬¡¬®¬¦¬½ ¬¡¨Ï¥ì¥ì?¬Þ ?¬é¥ì?¬¡¬®¬¨¢ç
private function syncInventory()
{
	local int i;//, totalLen;
	local ItemInfo kClearItem;

	local array<ItemInfo> itemarray, equipItemArray;

	kClearItem.IconName = "L2ui_ct1.emptyBtn";
	ClearItemID( kClearItem.ID );

	// ¬¡¬°?????¡©, ?¬¨¬¡¬®¬¦¬½ ¬×¬ã¡¤¬±, ¬£¬ê?¬Ý¬©¬³ ¬¡¬Ö?¬ã ¬×¬ã¡¤¬±¬¡¡í ¥ì¬í¡¤¬° ?¬ä?¬à?¬¡¡¾¬Ó
	itemArray = getInstanceL2Util().SortItemArray ( inventoryWndScript.getInventoryAllItemArray(true)) ;
	equipItemArray = getInstanceL2Util().SortItemArray ( inventoryWndScript.getInventoryEquipItemArray() );			
	
	for(i = 0; i < equipItemArray.Length; i++) 
	{
		equipItemArray[i].ForeTexture = "L2UI_CT1.Icon.WearPanel";
		setShowItemCount(equipItemArray[i]);

		totalInven_ItemWnd.SetItem( i, equipItemArray[i]);
	}
	

	// ¬¡¬±¢à¬¿ ?¬¨¬¡¬®¬¦¬½ ¬¤¬Á¡Æ?
	for(i = 0; i < itemArray.Length ; i++) 
	{
		setShowItemCount(itemArray[i]);
		totalInven_ItemWnd.SetItem ( i + equipItemArray.Length, itemArray[i]);
	}
	
	// ?¬å ?¬¨¬¡¬®¬¦¬½ ¬¤¬Á¡Æ?
	for ( i = equipItemArray.Length + itemArray.Length; i < inventoryWndScript.GetMyInventoryLimit(); i ++ ) 
	{
		//Debug ("syncInventory" @  i );
		totalInven_ItemWnd.SetItem( i,  kClearItem ) ;
	}
}

private function SetItemCount()
{
	InvenoryCount.SetText("(" $ string(invenCountDatainfo.curCnt) $ "/" $ string(invenCountDatainfo.maxCnt) $ ")");
	getInstanceL2Util().ItemboxUpdate(totalInven_ItemWnd, invenCountDatainfo.maxCnt);
	if(HelpButton.IsShowWindow())
	{
		HelpButton.HideWindow();
	}	
}

private function SetAdenaText()
{
	local string Adenastring;

	Adenastring = string(GetAdena());
	AdenaText.SetText(MakeCostString(Adenastring));
	AdenaText.SetTooltipString(ConvertNumToText(Adenastring));	
}

function showWindowByParentWindow(WindowHandle pWnd, optional bool bToggleShow)
{	
	ParentWindow = pWnd;
	//getInstanceL2Util().windowMoveToSide(pWnd, m_hOwnerWnd);	
	getInstanceL2Util().windowAnchorToSide(pWnd, m_hOwnerWnd, 2, 29);	

	if (!m_hOwnerWnd.IsShowWindow())
	{
		m_hOwnerWnd.ShowWindow();
		m_hOwnerWnd.SetFocus();
		pWnd.SetFocus();
	}
	else 
	{
		if(bToggleShow) m_hOwnerWnd.HideWindow();
	}
}
