class ItemEnchantSubWnd extends UICommonAPI;

enum SortOrder 
{
	non,
	up,
	Down
};

var TextureHandle SlotBg1_Texture;
var ItemWindowHandle ItemWnd;
var ItemEnchantSubWnd.SortOrder currentSortOrder;

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

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);
	class'ItemEnchantWnd'.static.Inst().RequestInputItemInfo(iInfo);
}

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'ItemEnchantWnd'.static.Inst().GetStateName())
	{
		case class'ItemEnchantWnd'.const.STATE_NONE:
			break;
		case class'ItemEnchantWnd'.const.STATE_READY_SCROLL:
			SetTitle(GetSystemString(1532));
			SetScrollItemWnds();
			break;
		case class'ItemEnchantWnd'.const.STATE_READY_EQUIPMENT:
		case class'ItemEnchantWnd'.const.STATE_READY_SUPPORT:
		case class'ItemEnchantWnd'.const.STATE_READY_SUPPORT_STONE:
		case class'ItemEnchantWnd'.const.STATE_READY_SUPPORT_SYSTEM:
			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(IsEnchantScroll(EEtcItemType(iInfos[i].EtcItemType)))
		{
			iInfos[i].bShowCount = IsStackableItem(EEtcItemType(iInfos[i].ConsumeType));
			ItemWnd.AddItem(iInfos[i]);
		}
	}
}

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 ItemInfo iInfo, scrollInfo;
	local array<ItemInfo> iInfos;
	local EnchantScrollSetUIData enchantScrollData;

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

	for(i = 0; i < iInfos.Length; i++)
	{
		if(iInfo.Id == iInfos[i].Id)
		{
			// [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 array<ItemInfo> GetSupportItems()
{
	local int i, supportClassID;
	local array<ItemInfo> iInfos, iInfosSupport;

	supportClassID = GetScrollInfoSupport().Id.ClassID;
	class'UIDATA_INVENTORY'.static.GetAllItem(iInfos);

	for(i = 0; i < iInfos.Length; i++)
	{
		if(! IsIncPropItem(EEtcItemType(iInfos[i].EtcItemType)))
		{
			// [Explicit Continue]
			continue;
		}
		if(! CheckMatchSupportItem(EEtcItemType(iInfos[i].EtcItemType)))
		{
			// [Explicit Continue]
			continue;
		}
		if(iInfos[i].Id.ClassID == supportClassID)
		{
			// [Explicit Continue]
			continue;
		}
		iInfosSupport[iInfosSupport.Length] = iInfos[i];
	}
	return iInfosSupport;
}

function SetSupportItems(ItemWindowHandle supportItemWnd)
{
	local int i;
	local array<ItemInfo> iInfos;

	supportItemWnd.Clear();
	if(! class'ItemEnchantWnd'.static.Inst().UseSupportSlot())
	{
		return;
	}
	iInfos = GetSupportItems();

	for(i = 0; i < iInfos.Length; i++)
	{
		iInfos[i].bShowCount = IsStackableItem(iInfos[i].ConsumeType);
		supportItemWnd.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 IsIncPropItem(EEtcItemType Type)
{
	return Type == ITEME_ENCHT_ATTR_INC_PROP_ENCHT_AM || Type == ITEME_ENCHT_ATTR_INC_PROP_ENCHT_WP || Type == ITEME_BLESS_INC_PROP_ENCHT_WP || Type == ITEME_BLESS_INC_PROP_ENCHT_AM || Type == ITEME_MULTI_INC_PROB_ENCHT_WP || Type == ITEME_MULTI_INC_PROB_ENCHT_AM || Type == ITEME_POLY_INC_ENCHANT_PROP_WP || Type == ITEME_POLY_INC_ENCHANT_PROP_AM;
}

function bool IsEnchantScroll(EEtcItemType Type)
{
	return Type == ITEME_ENCHT_WP || Type == ITEME_ENCHT_AM || Type == ITEME_BLESS_ENCHT_WP || Type == ITEME_BLESS_ENCHT_AM || Type == ITEME_ENCHT_ATTR_CRYSTAL_ENCHANT_AM || Type == ITEME_ENCHT_ATTR_CRYSTAL_ENCHANT_WP || Type == ITEME_ENCHT_ATTR_ANCIENT_CRYSTAL_ENCHANT_AM || Type == ITEME_ENCHT_ATTR_ANCIENT_CRYSTAL_ENCHANT_WP || Type == ITEME_MULTI_ENCHT_WP || Type == ITEME_MULTI_ENCHT_AM || Type == ITEME_ENCHT_AG || Type == ITEME_BLESS_ENCHT_AG || Type == ITEME_MULTI_ENCHT_AG || Type == ITEME_ANCIENT_CRYSTAL_ENCHANT_AG || Type == ITEME_CURSED_ENCHANT_AM || Type == ITEME_CURSED_ENCHANT_WP || Type == ITEME_POLY_ENCHANT_WP || Type == ITEME_POLY_ENCHANT_AM || Type == ITEME_SPECIAL_ENCHT_WP || Type == ITEME_SPECIAL_ENCHT_AM;
}

function bool CheckMatchSupportItem(EEtcItemType supportSubType)
{
	local EEtcItemType scrollSubType;

	return true;
	scrollSubType = EEtcItemType(GetIteminfoScroll().EtcItemType);
	switch(supportSubType)
	{
		case ITEME_BLESS_INC_PROP_ENCHT_WP:
			return scrollSubType == ITEME_BLESS_ENCHT_WP;
		case ITEME_BLESS_INC_PROP_ENCHT_AM:
			return scrollSubType == ITEME_BLESS_ENCHT_AM;
		case ITEME_MULTI_INC_PROB_ENCHT_WP:
			return scrollSubType == ITEME_MULTI_ENCHT_WP;
		case ITEME_MULTI_INC_PROB_ENCHT_AM:
			return scrollSubType == ITEME_MULTI_ENCHT_AM;
		case ITEME_POLY_INC_ENCHANT_PROP_WP:
			return scrollSubType == ITEME_POLY_ENCHANT_WP;
		case ITEME_POLY_INC_ENCHANT_PROP_AM:
			return scrollSubType == ITEME_POLY_ENCHANT_AM;
		default:
			return false;
	}
}

function ItemInfo GetIteminfoScroll()
{
	local ItemInfo scrollItem;

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

function ItemInfo GetItemInfoEquipment()
{
	local ItemInfo equipmentItem;

	class'ItemEnchantWnd'.static.Inst().equipmentItemWindow.GetItem(0, equipmentItem);
	return equipmentItem;
}

function ItemInfo GetScrollInfoSupport()
{
	local ItemInfo supportiInfo;

	class'ItemEnchantWnd'.static.Inst().supportItemWindow.GetItem(0, supportiInfo);
	return supportiInfo;
}

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;
}
