/**
 *  ЗХјє UI єёБ¶Гў
 **/
class ItemJewelEnchantSubWnd extends UICommonAPI;

var TextureHandle SlotBg1_Texture;
var ItemWindowHandle itemJewelEnchantSubWnd_ItemWnd;
var array<ItemInfo> itemInfoArray;
var ButtonHandle ItemAddBtn;
var ItemJewelEnchantWnd itemJewelEnchantWndScript;

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

function Initialize()
{
	itemJewelEnchantSubWnd_ItemWnd = GetItemWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".ItemJewelEnchantSubWnd_ItemWnd");
	ItemAddBtn = GetButtonHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".ItemAddBtn");
	ItemAddBtn.DisableWindow();
	itemJewelEnchantWndScript = ItemJewelEnchantWnd(GetScript("ItemJewelEnchantWnd"));
}

event OnRegisterEvent()
{
	RegisterEvent(EV_AdenaInvenCount);
}

event OnLoad()
{
	Initialize();
}

event OnShow()
{
	_Refresh();
}

event OnHide()
{
	itemJewelEnchantSubWnd_ItemWnd.Clear();
	SetDescTextBox();
}

event OnClickButton(string strID)
{
	switch(strID)
	{
		case "itemAddBtn":
			MoveAll();
			break;
	}	
}

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

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

	if(itemJewelEnchantWndScript._IsWorkingEnchant())
	{
		return;
	}
	itemJewelEnchantSubWnd_ItemWnd.GetItem(Index, Info);
	if(Info.Id.ClassID > 0)
	{
		itemJewelEnchantWndScript._DropProcess(Info, 0);
	}
}

event OnEvent(int Event_ID, string param)
{
	switch(Event_ID)
	{
		case EV_AdenaInvenCount:
			if(m_hOwnerWnd.IsShowWindow())
			{
				if(itemJewelEnchantWndScript.GetStateName() != itemJewelEnchantWndScript.const.STATE_RESULT)
				{
					_Refresh();
				}
			}
			break;
	}
}

function _Refresh()
{
	local int i;
	local INT64 nCommissionAdena;
	local CombinationItemUIData o_data;

	if(! itemJewelEnchantWndScript.m_hOwnerWnd.IsShowWindow())
	{
		return;
	}
	itemJewelEnchantSubWnd_ItemWnd.Clear();
	SetDescTextBox();
	ItemAddBtn.DisableWindow();
	if(IsAutoMode())
	{
		if(GetOwnerStateName() == class'ItemJewelEnchantWnd'.const.STATE_ALLREADY)
		{
			ItemAddBtn.EnableWindow();
			if(API_GetCombinationItemData(o_data))
			{
				if(o_data.AutomaticType == 0)
				{
					ItemAddBtn.DisableWindow();
					return;
				}
			}
		}
	}
	if(_GetCanEnchantItemCurrent(nCommissionAdena, itemInfoArray))
	{
		if((GetOwnerStateName()) == itemJewelEnchantWndScript.const.STATE_READY)
		{
			if(class'ItemJewelEnchantWnd'.static.Inst().currentEnchantType == class'ItemJewelEnchantWnd'.static.Inst().EnchantType.dye)
			{
				itemInfoArray = GetDyeCombines(itemInfoArray);
			}
		}
		itemInfoArray.Sort(DelegateSortCompare);

		for(i = 0; i < itemInfoArray.Length; i++)
		{
			itemJewelEnchantSubWnd_ItemWnd.AddItem(itemInfoArray[i]);
		}
	}
	SetDescTextBox();	
}

function bool _GetCanEnchantItemCurrent(out INT64 nCommissionAdena, out array<ItemInfo> MaterialItems)
{
	local int slot1_ClassID, slot2_ClassID, slot1_Enchanted, slot2_Enchanted;

	slot1_ClassID = itemJewelEnchantWndScript._GetClassIDBySlotItemIndex(1);
	slot1_Enchanted = itemJewelEnchantWndScript._GetEnchantedBySlotItemIndex(1);
	slot2_ClassID = itemJewelEnchantWndScript._GetClassIDBySlotItemIndex(2);
	slot2_Enchanted = itemJewelEnchantWndScript._GetEnchantedBySlotItemIndex(2);
	switch(GetOwnerStateName())
	{
		case class'ItemJewelEnchantWnd'.const.STATE_READY:
			API_GetMaterialItemForEnchantFromInven(-1, -1, -1, -1, nCommissionAdena, MaterialItems);
			CheckNDisableItemInfo(MaterialItems);
			break;
		case class'ItemJewelEnchantWnd'.const.STATE_ONEREADY:
			API_GetMaterialItemForEnchantFromInven(slot1_ClassID, slot1_Enchanted, -1, -1, nCommissionAdena, MaterialItems);
			break;
		case class'ItemJewelEnchantWnd'.const.STATE_ALLREADY:
			if(IsAutoMode())
			{
				API_GetMaterialItemForEnchantFromInven(slot1_ClassID, slot1_Enchanted, slot2_ClassID, slot2_Enchanted, nCommissionAdena, MaterialItems);				
			}
			else
			{
				API_GetMaterialItemForEnchantFromInven(slot1_ClassID, slot1_Enchanted, -1, -1, nCommissionAdena, MaterialItems);
			}
			break;
	}
	return MaterialItems.Length > 0;	
}

private function CheckNDisableItemInfo(out array<ItemInfo> MaterialItems)
{
	local int i;
	local INT64 nCommissionAdenaSlot2;
	local array<ItemInfo> materialItemSlot2;

	for(i = 0; i < MaterialItems.Length; i++)
	{
		materialItemSlot2.Length = 0;
		if(! _CanEnchatItem(MaterialItems[i].Id.ClassID, MaterialItems[i].Enchanted, nCommissionAdenaSlot2, materialItemSlot2))
		{
			MaterialItems[i].bDisabled = 1;
		}
	}	
}

function bool _CheckCanEnchatItem(int ClassID, int Enchanted)
{
	local array<ItemInfo> MaterialItems;
	local INT64 nCommissionAdena;

	return _CanEnchatItem(ClassID, Enchanted, nCommissionAdena, MaterialItems);	
}

function bool _CanEnchatItem(int slot1ClassID, int Enchanted, out INT64 nCommissionAdena, out array<ItemInfo> MaterialItems)
{
	local array<ItemInfo> iInfos;

	MaterialItems.Length = 0;
	nCommissionAdena = 0;
	if(slot1ClassID != 0)
	{
		if(class'UIDATA_INVENTORY'.static.FindItemByClassID(slot1ClassID, iInfos) < 1)
		{
			return false;
		}
		if(iInfos[0].ItemNum < 1)
		{
			return false;
		}
	}
	API_GetMaterialItemForEnchantFromInven(GetSlotClassID(slot1ClassID), Enchanted, -1, -1, nCommissionAdena, MaterialItems);
	if(slot1ClassID == 0)
	{
		CheckNDisableItemInfo(MaterialItems);
		return false;		
	}
	else
	{
		CheckNRemoveSameClassid(slot1ClassID, Enchanted, MaterialItems);
	}
	return MaterialItems.Length > 0;	
}

// desc јіён
private function SetDescTextBox()
{
	local CombinationItemUIData o_data;

	if(itemJewelEnchantSubWnd_ItemWnd.GetItemNum() == 0)
	{
		ItemAddBtn.DisableWindow();
		GetWindowHandle("ItemJewelEnchantSubWnd.DescriptionMsgWnd").ShowWindow();		
	}
	else
	{
		GetWindowHandle("ItemJewelEnchantSubWnd.DescriptionMsgWnd").HideWindow();
	}
	if(API_GetCombinationItemData(o_data))
	{
		if(o_data.AutomaticType == 0)
		{
			GetTextBoxHandle("ItemJewelEnchantSubWnd.DescriptionMsgWnd.descTextBox").SetText(GetSystemMessage(13729));
			return;
		}
	}
	GetTextBoxHandle("ItemJewelEnchantSubWnd.DescriptionMsgWnd.descTextBox").SetText(GetSystemMessage(4222));	
}

private function int API_GetEnchantCandidateMaterialList(int ClassID, out array<int> CandidateMaterials)
{
	class'NewEnchantAPI'.static.GetEnchantCandidateMaterialList(ClassID, CandidateMaterials);
	return CandidateMaterials.Length;	
}

private function bool API_GetCombinationItemData(out CombinationItemUIData o_data)
{
	if(itemJewelEnchantWndScript._GetClassIDBySlotItemIndex(1) == -1)
	{
		return false;
	}
	if(itemJewelEnchantWndScript._GetClassIDBySlotItemIndex(2) == -1)
	{
		return false;
	}
	return class'NewEnchantAPI'.static.GetCombinationItemData(itemJewelEnchantWndScript._GetClassIDBySlotItemIndex(1), itemJewelEnchantWndScript._GetEnchantedBySlotItemIndex(1), itemJewelEnchantWndScript._GetClassIDBySlotItemIndex(2), itemJewelEnchantWndScript._GetEnchantedBySlotItemIndex(2), o_data);	
}

private function API_GetMaterialItemForEnchantFromInven(int OneSlotItemClassID, int OneSlotItemEnchant, int TwoSlotItemClassID, int TwoSlotItemEnchant, out INT64 Commission, out array<ItemInfo> MaterialItems)
{
	local int i;

	MaterialItems.Length = 0;
	if(getInstanceUIData().getIsClassicServer())
	{
		class'NewEnchantAPI'.static.GetMaterialItemForEnchantFromInven(OneSlotItemClassID, OneSlotItemEnchant, TwoSlotItemClassID, TwoSlotItemEnchant, Commission, MaterialItems, 1);		
	}
	else
	{
		class'NewEnchantAPI'.static.GetMaterialItemForEnchantFromInven(OneSlotItemClassID, OneSlotItemEnchant, TwoSlotItemClassID, TwoSlotItemEnchant, Commission, MaterialItems);
	}
	CheckNRemoveSlots(MaterialItems);
	CheckNRemoveSameAutoInventory(MaterialItems);

	for(i = 0; i < MaterialItems.Length; i++)
	{
		MaterialItems[i].bShowCount = IsStackableItem(MaterialItems[i].ConsumeType);
	}	
}

private function API_GetMaterialItemForEnchantFromEquip(int OneSlotItemClassID, int OneSlotItemEnchant, int TwoSlotItemClassID, int TwoSlotItemEnchant, out INT64 Commission, out array<ItemInfo> MaterialItems)
{
	if(getInstanceUIData().getIsClassicServer())
	{
		class'NewEnchantAPI'.static.GetMaterialItemForEnchantFromEquip(OneSlotItemClassID, OneSlotItemEnchant, TwoSlotItemClassID, TwoSlotItemEnchant, Commission, MaterialItems, 1);		
	}
	else
	{
		class'NewEnchantAPI'.static.GetMaterialItemForEnchantFromEquip(OneSlotItemClassID, OneSlotItemEnchant, TwoSlotItemClassID, TwoSlotItemEnchant, Commission, MaterialItems);
	}
}

private function int GetSlotClassID(int nClassID)
{
	if(nClassID > 0)
	{
		return nClassID;
	}
	return -1;
}

private function name GetOwnerStateName()
{
	return class'ItemJewelEnchantWnd'.static.Inst().GetStateName();	
}

private function array<ItemInfo> GetDyeCombines(array<ItemInfo> iInfos)
{
	local int i;
	local DyeCombinationUIData o_data;
	local array<ItemInfo> dyeCombines;

	for(i = 0; i < iInfos.Length; i++)
	{
		if(class'UIDATA_HENNA'.static.GetDyeCombinationData(iInfos[i].Id.ClassID, o_data))
		{
			dyeCombines[dyeCombines.Length] = iInfos[i];
		}
	}
	return dyeCombines;
}

private function CheckNRemoveSlotSlotIndex(int SlotIndex, out array<ItemInfo> MaterialItems)
{
	local int i, ServerID, Enchanted;

	ServerID = itemJewelEnchantWndScript._GetServerIDBySlotItemIndex(SlotIndex);
	Enchanted = itemJewelEnchantWndScript._GetEnchantedBySlotItemIndex(SlotIndex);
	if(ServerID == -1)
	{
		return;
	}

	for(i = 0; i < MaterialItems.Length; i++)
	{
		if(MaterialItems[i].Id.ServerID == ServerID && MaterialItems[i].Enchanted == Enchanted)
		{
			MaterialItems[i].ItemNum = MaterialItems[i].ItemNum - 1;
			if(MaterialItems[i].ItemNum == 0)
			{
				MaterialItems.Remove(i, 1);
			}
			return;
		}
	}	
}

private function CheckNRemoveSlots(out array<ItemInfo> MaterialItems)
{
	switch(GetOwnerStateName())
	{
		case class'ItemJewelEnchantWnd'.const.STATE_ONEREADY:
			CheckNRemoveSlotSlotIndex(1, MaterialItems);
			break;
		case class'ItemJewelEnchantWnd'.const.STATE_ALLREADY:
			CheckNRemoveSlotSlotIndex(1, MaterialItems);
			CheckNRemoveSlotSlotIndex(2, MaterialItems);
			break;
		case class'ItemJewelEnchantWnd'.const.STATE_PROCESS:
			CheckNRemoveSlotSlotIndex(1, MaterialItems);
			CheckNRemoveSlotSlotIndex(2, MaterialItems);
			break;
	}	
}

private function CheckNRemoveSameClassid(int ClassID, int Enchanted, out array<ItemInfo> MaterialItems)
{
	local int i;

	for(i = 0; i < MaterialItems.Length; i++)
	{
		if((MaterialItems[i].Id.ClassID == ClassID) && MaterialItems[i].Enchanted == Enchanted)
		{
			MaterialItems[i].ItemNum = MaterialItems[i].ItemNum - 1;
			if(MaterialItems[i].ItemNum == 0)
			{
				MaterialItems.Remove(i, 1);
			}
			return;
		}
	}
}

private function CheckNRemoveSameAutoInventory(out array<ItemInfo> MaterialItems)
{
	local int i, j, Len;
	local ItemInfo iInfo;

	Len = class'ItemJewelEnchantWnd'.static.Inst().InventoryItem.GetItemNum();

	for(j = 0; j < Len; j++)
	{
		if(class'ItemJewelEnchantWnd'.static.Inst().InventoryItem.GetItem(j, iInfo))
		{
			for(i = 0; i < MaterialItems.Length; i++)
			{
				if(iInfo.Id == MaterialItems[i].Id && iInfo.Enchanted == MaterialItems[i].Enchanted)
				{
					MaterialItems[i].ItemNum = MaterialItems[i].ItemNum - iInfo.ItemNum;
					if(MaterialItems[i].ItemNum < 1)
					{
						MaterialItems.Remove(i, 1);
					}
					// [Explicit Break]
					break;
				}
			}
		}
	}	
}

function int GetIndexSubIvenItem(int ClassID)
{
	local ItemInfo Info;
	local int ItemNum, i;

	ItemNum = itemJewelEnchantSubWnd_ItemWnd.GetItemNum();

	for(i = 0; i < ItemNum; i++)
	{
		itemJewelEnchantSubWnd_ItemWnd.GetItem(i, Info);
		if(Info.Id.ClassID == ClassID)
		{
			return i;
		}
	}
	return -1;
}

delegate int DelegateSortCompare(ItemInfo A, ItemInfo B)
{
	if(A.Id.ClassID > B.Id.ClassID) // їАё§ Вчјш. Б¶°З№®їЎ < АМёй і»ёІВчјш.
	{
		return -1; // АЪё®ё¦ №ЩІгѕЯЗТ¶§ -1ё¦ ё®ЕП ЗП°Ф ЗФ.
	}
	else
	{
		return 0;
	}	
}

function MoveAll()
{
	local int i, Len;
	local ItemInfo iInfo;

	Len = itemJewelEnchantSubWnd_ItemWnd.GetItemNum();

	for(i = 0; i < Len; i++)
	{
		itemJewelEnchantSubWnd_ItemWnd.GetItem(i, iInfo);
		itemJewelEnchantWndScript._AddInventoryItem(iInfo);
	}
	itemJewelEnchantWndScript._SortItemInventory();
	itemJewelEnchantSubWnd_ItemWnd.Clear();
	SetDescTextBox();	
}

function bool _MoveToOwner(ItemInfo iInfo, optional INT64 ItemNum)
{
	local int i, idx, Len;
	local ItemInfo iiInfo;

	if(IsStackableItem(iInfo.ConsumeType))
	{
		idx = itemJewelEnchantSubWnd_ItemWnd.FindItem(iInfo.Id);
		if(idx == -1)
		{
			return false;
		}
		if(iInfo.ItemNum == ItemNum)
		{
			itemJewelEnchantSubWnd_ItemWnd.DeleteItem(idx);			
		}
		else
		{
			iInfo.ItemNum = iInfo.ItemNum - ItemNum;
			itemJewelEnchantSubWnd_ItemWnd.SetItem(idx, iInfo);
		}
		iInfo.ItemNum = ItemNum;
		itemJewelEnchantWndScript._AddInventoryItem(iInfo);		
	}
	else
	{
		Len = itemJewelEnchantSubWnd_ItemWnd.GetItemNum();

		for(i = Len - 1; i >= 0; i--)
		{
			itemJewelEnchantSubWnd_ItemWnd.GetItem(i, iiInfo);
			if(iInfo.Id.ClassID == iiInfo.Id.ClassID)
			{
				if(iInfo.Enchanted == iiInfo.Enchanted)
				{
					itemJewelEnchantSubWnd_ItemWnd.DeleteItem(i);
					itemJewelEnchantWndScript._AddInventoryItem(iiInfo);
					ItemNum = ItemNum - 1;
					if(ItemNum == 0)
					{
						// [Explicit Break]
						break;
					}
				}
			}
		}
	}
	itemJewelEnchantWndScript._SortItemInventory();
	SetDescTextBox();
	return true;	
}

function bool _GetSlot1ItemInfo(out ItemInfo iInfo)
{
	return itemJewelEnchantWndScript._GetSlot1ItemInfo(iInfo);	
}

function bool _GetSlot2ItemInfo(out ItemInfo iInfo)
{
	return itemJewelEnchantWndScript._GetSlot2ItemInfo(iInfo);	
}

function INT64 _GetIneventoryItemNum(ItemInfo iInfo)
{
	local int i, idx;
	local INT64 ItemNum;
	local ItemInfo iiInfo;

	if(IsStackableItem(iInfo.ConsumeType))
	{
		idx = itemJewelEnchantSubWnd_ItemWnd.FindItem(iInfo.Id);
		if(idx == -1)
		{
			return 0;
		}
		itemJewelEnchantSubWnd_ItemWnd.GetItem(idx, iiInfo);
		ItemNum = iiInfo.ItemNum;		
	}
	else
	{
		for(i = 0; i < itemJewelEnchantSubWnd_ItemWnd.GetItemNum(); i++)
		{
			while(! itemJewelEnchantSubWnd_ItemWnd.GetItem(i, iiInfo))
			{
				idx++;
			}
			idx++;
			if(iInfo.Id.ClassID == iiInfo.Id.ClassID)
			{
				if(iInfo.Enchanted == iiInfo.Enchanted)
				{
					ItemNum = ItemNum + 1;
				}
			}
		}
	}
	return ItemNum;	
}

private function bool IsAutoMode()
{
	return class'ItemJewelEnchantWnd'.static.Inst()._IsAutoMode();	
}
