class ItemMultiEnchantSubWnd extends UICommonAPI;

enum SortOrder 
{
	non,
	up,
	Down
};

var ItemWindowHandle ItemWnd;
var SortOrder currentSortOrder;

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

function Initialize()
{
	ItemWnd = GetItemWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".itemEnchantSubWndItemWnd");
	GetTextBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".DescriptionMsgWnd.descTextBox").SetText(GetSystemMessage(4222));
	currentSortOrder = SortOrder.Down/*2*/;
}

event OnRegisterEvent()
{
	RegisterEvent(EV_AdenaInvenCount);
}

event OnLoad()
{
	Initialize();
}

event OnShow()
{
	Refresh();
}

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

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

	ItemWnd.GetItem(Index, iInfo);
	switch(class'ItemMultiEnchantWnd'.static.Inst().GetStateName())
	{
		case class'ItemMultiEnchantWnd'.const.STATE_READY_SCROLL:
			class'ItemMultiEnchantWnd'.static.Inst().RQ_C_EX_REQ_START_MULTI_ENCHANT_SCROLL(iInfo);
			break;
		case class'ItemMultiEnchantWnd'.const.STATE_READY_EQUIPMENT:
			class'ItemMultiEnchantWnd'.static.Inst().SetSlotEmpty(iInfo);
			break;
	}
}

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

event OnClickButton(string strID)
{
	switch(strID)
	{
		case "sort_btn":
			ToggleSort();
			break;
	}
}

function ToggleSort()
{
	switch(currentSortOrder)
	{
		case SortOrder.up/*1*/:
			currentSortOrder = SortOrder.Down/*2*/;
			break;
		default:
			currentSortOrder = SortOrder.up/*1*/;
			break;
	}
	Refresh();
}

function Refresh()
{
	switch(class'ItemMultiEnchantWnd'.static.Inst().GetStateName())
	{
		case class'ItemMultiEnchantWnd'.const.STATE_NONE:
			break;
		case class'ItemMultiEnchantWnd'.const.STATE_READY_SCROLL:
			SetTitle(GetSystemString(1532));
			SetScrollItemWnds();
			break;
		case class'ItemMultiEnchantWnd'.const.STATE_READY_EQUIPMENT:
			SetTitle(GetSystemString(13846));
			SetEnchantableItems();
			break;
	}
	SetDescTextBox();
}

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

	ItemWnd.Clear();
	class'UIDATA_INVENTORY'.static.GetAllItem(iInfos);

	for(i = 0; i < iInfos.Length; i++)
	{
		if(IsNormalEnchantScroll(EEtcItemType(iInfos[i].EtcItemType)))
		{
			iInfos[i].bShowCount = IsStackableItem(iInfos[i].ConsumeType);
			ItemWnd.AddItem(iInfos[i]);
		}
	}
}

function Show()
{
	m_hOwnerWnd.ShowWindow();
	Refresh();
}

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

private function bool API_GetEnchantScrollSetData(ItemInfo iInfo, ItemInfo iInfo2, out EnchantScrollSetUIData enchantScrollData)
{
	return class'UIDATA_ITEM'.static.GetEnchantScrollSetData(iInfo.Id.ClassID, iInfo2.Id.ClassID, enchantScrollData);	
}

function SetEnchantableItems()
{
	local int i, enchantMax, enchantMin;
	local array<ItemInfo> iInfos;
	local ItemInfo scrollInfo;
	local EnchantScrollSetUIData enchantScrollData;

	ItemWnd.Clear();
	scrollInfo = GetIteminfoScroll();
	class'UIDATA_INVENTORY'.static.GetAllEnchantableInvenItem(scrollInfo.Id.ClassID, iInfos);
	iInfos.Sort(OnSortProbCompare);
	iInfos.Sort(OnSortNameCompare);

	for(i = 0; i < iInfos.Length; i++)
	{
		if(class'ItemMultiEnchantWnd'.static.Inst().FindIndexEquipmentWithServerID(iInfos[i].Id.ServerID) > -1)
		{
			// [Explicit Continue]
			continue;
		}
		if(! API_GetEnchantScrollSetData(scrollInfo, iInfos[i], enchantScrollData))
		{
			// [Explicit Continue]
			continue;
		}
		enchantMax = enchantScrollData.EnchantRangeDatas[enchantScrollData.EnchantRangeDatas.Length - 1].RangeMax;
		enchantMin = enchantScrollData.EnchantRangeDatas[0].RangeMin;
		if((iInfos[i].Enchanted > enchantMax) && enchantMax > -1)
		{
			// [Explicit Continue]
			continue;
		}
		if((iInfos[i].Enchanted < enchantMin) && enchantMin > -1)
		{
			// [Explicit Continue]
			continue;
		}
		iInfos[i].bShowCount = IsStackableItem(iInfos[i].ConsumeType);
		ItemWnd.AddItem(iInfos[i]);
	}
}

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

function SetTitle(string Title)
{
	GetTextBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".ItemMaterialTitle_Txt").SetText(Title);
}

function bool IsEnchantableItem(EItemParamType Type)
{
	return Type == ITEMP_WEAPON || Type == ITEMP_ARMOR || Type == ITEMP_ACCESSARY || Type == ITEMP_SHIELD;
}

function bool IsNormalEnchantScroll(EEtcItemType Type)
{
	return Type == ITEME_ENCHT_WP || Type == ITEME_ENCHT_AM || Type == ITEME_ENCHT_AG;
}

function ItemInfo GetIteminfoScroll()
{
	local ItemInfo scrollItem;

	class'ItemMultiEnchantWnd'.static.Inst().scrollItemWindow.GetItem(0, scrollItem);
	return scrollItem;
}

delegate int OnSortNameCompare(ItemInfo A, ItemInfo B)
{
	switch(currentSortOrder)
	{
		case SortOrder.non/*0*/:
			return 0;
		case SortOrder.up/*1*/:
			if(float(A.Id.ClassID) > float(B.Id.ClassID))
			{
				return -1;				
			}
			else
			{
				break;
			}
		case SortOrder.Down/*2*/:
			if(float(A.Id.ClassID) < float(B.Id.ClassID))
			{
				return -1;				
			}
			else
			{
				break;
			}
	}
	return 0;
}

delegate int OnSortProbCompare(ItemInfo A, ItemInfo B)
{
	switch(currentSortOrder)
	{
		case SortOrder.non/*0*/:
			return 0;
		case SortOrder.up/*1*/:
			if(float(A.Enchanted) > float(B.Enchanted))
			{
				return -1;				
			}
			else
			{
				break;
			}
		case SortOrder.Down/*2*/:
			if(float(A.Enchanted) < float(B.Enchanted))
			{
				return -1;				
			}
			else
			{
				break;
			}
	}
	return 0;
}
