class HomunculusWndMainList extends UICommonAPI;

const LISTITEMMAX = 9;
const LISTMAX = 18;

var WindowHandle Me;
var string m_WindowName;
var HomunculusWndMainViewport homunculusWndMainViewportScript;
var HomunculusWndMainDetailStats homunculusWndMainDetailStatsScript;
var HomunculusWndMainDetailStatsEnchant homunculusWndMainDetailStatsEnchantScript;
var HomunculusWndBirth homunculusWndBirthScript;
var HomunculusWnd HomunculusWndScript;
var array<HomunculusWndMainListItem> listItems;
var int currentSelectedItemIndex;
var int currentSelectedIdx;
var int currentSelectedDataIndex;
var array<HomunculusAPI.HomunculusData> homunculusDatas;
var array<int> listNew;
var bool inited;
var int nActivateSlotIndex;
var UIControlPageNavi pageNavi;
var bool naviBtnClicked;

function HandleGameInit()
{
	if(! HomunculusWndScript.ChkSerVer())
	{
		return;
	}
	inited = false;
}

function HandleRestart()
{
	if(! HomunculusWndScript.ChkSerVer())
	{
		return;
	}
	inited = false;
}

function ClearAll()
{
	local int i;

	for ( i = 0;i < listItems.Length;i++ )
	{
		listItems[i].ClearAll();
	}
}

function Initialize()
{
	m_WindowName = getCurrentWindowName(string(self));
	Me = GetWindowHandle(m_WindowName);
	homunculusWndMainViewportScript = HomunculusWndMainViewport(GetScript("HomunculusWnd.HomunculusWndMainViewport"));
	homunculusWndMainDetailStatsScript = HomunculusWndMainDetailStats(GetScript("HomunculusWnd.HomunculusWndMainDetailStats"));
	homunculusWndMainDetailStatsEnchantScript = HomunculusWndMainDetailStatsEnchant(GetScript("HomunculusWnd.HomunculusWndMainDetailStatsEnchant"));
	homunculusWndBirthScript = HomunculusWndBirth(GetScript("HomunculusWnd.HomunculusWndBirth"));
	HomunculusWndScript = HomunculusWnd(GetScript("HomunculusWnd"));
	InitPagenavi();
	InitHandleListItems();
}

function InitHandleListItems()
{
	local int i;

	for ( i = 0;i < LISTITEMMAX; i++ )
	{
		GetWindowHandle(m_WindowName $ ".Item" $ string(i)).SetScript("HomunculusWndMainListItem");
		listItems[i] = HomunculusWndMainListItem(GetWindowHandle(m_WindowName $ ".Item" $ string(i)).GetScript());
		listItems[i].Init(m_WindowName $ ".Item" $ string(i));
		listItems[i].SetState(listItems[i].type_State.Normal);
	}
	currentSelectedItemIndex = -1;
}

function InitPagenavi()
{
	local WindowHandle PageNaviControl;

	PageNaviControl = GetWindowHandle(m_WindowName $ ".PageNaviControl");
	PageNaviControl.SetScript("UIControlPageNavi");
	pageNavi = UIControlPageNavi(PageNaviControl.GetScript());
	pageNavi.Init(m_WindowName $ ".PageNaviControl");
	pageNavi.SetTotalPage(2);
	pageNavi.Go(1);
	pageNavi.DelegeOnChangePage = pageChanged;
	pageNavi.DelegateOnClickButton = pageNaviButtonClicked;
}

function API_EX_SHOW_HOMUNCULUS_INFO()
{
	HomunculusWndScript.API_C_EX_SHOW_HOMUNCULUS_INFO(1);
}

function API_C_EX_HOMUNCULUS_ACTIVATE_SLOT(int SlotIndex)
{
	HomunculusWndScript.API_C_EX_HOMUNCULUS_ACTIVATE_SLOT(SlotIndex);
}

function OnRegisterEvent()
{
	RegisterEvent(EV_GamingStateEnter);
	RegisterEvent(EV_ShowHomunculusList);
	RegisterEvent(EV_PacketID(class'UIPacket'.const.S_EX_HOMUNCULUS_ACTIVATE_SLOT_RESULT));
	RegisterEvent(EV_PacketID(class'UIPacket'.const.S_EX_DELETE_HOMUNCLUS_DATA_RESULT));
}

function OnLoad()
{
	Initialize();
}

function OnEvent(int Event_ID, string param)
{
	switch(Event_ID)
	{
		case EV_ShowHomunculusList:
			HandleListItems();
			inited = true;
			break;
		case EV_GamingStateEnter:
			HandleGameInit();
			break;
		case EV_Restart:
			HandleRestart();
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_HOMUNCULUS_ACTIVATE_SLOT_RESULT):
			Handle_S_EX_HOMUNCULUS_ACTIVATE_SLOT_RESULT();
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_DELETE_HOMUNCLUS_DATA_RESULT):
			Handle_S_EX_DELETE_HOMUNCLUS_DATA_RESULT();
			break;
	}
}

function ShowPopup()
{
	local UIControlDialogAssets popupExpandScript;
	local array<RequestItem> requestItems;
	local int i;

	popupExpandScript = HomunculusWndScript.GetPopupExpandScript();
	popupExpandScript.SetDialogDesc(GetSystemString(13557));
	popupExpandScript.SetUseNeedItem(true);
	requestItems = API_GetHomunculusSlotActivateCost(nActivateSlotIndex + 1);
	popupExpandScript.StartNeedItemList(requestItems.Length);

	for(i = 0; i < requestItems.Length; i++)
	{
		popupExpandScript.AddNeedItemClassID(requestItems[i].Id, requestItems[i].Amount);
	}
	popupExpandScript.SetItemNum(1);
	popupExpandScript.Show();
	popupExpandScript.DelegateOnClickBuy = SetActivateSlot;
}

function array<RequestItem> API_GetHomunculusSlotActivateCost(int SlotIndex)
{
	return class'HomunculusAPI'.static.GetHomunculusSlotActivateCost(SlotIndex);
}

function SetActivateSlot()
{
	local UIControlDialogAssets popupExpandScript;

	popupExpandScript = HomunculusWndScript.GetPopupExpandScript();
	popupExpandScript.Hide();
	API_C_EX_HOMUNCULUS_ACTIVATE_SLOT(nActivateSlotIndex + 1);
}

function HandleListItemClicked(int itemIndex)
{
	switch(listItems[itemIndex].currState)
	{
		case listItems[itemIndex].type_State.NOTACTIVE:
			ShowPopup();
			break;
		case listItems[itemIndex].type_State.Normal:
			RemNew(listItems[itemIndex].currHomunculusData.idx);
			SetSelect(GetDataIndex(itemIndex));
			break;
		case listItems[itemIndex].type_State.READY:
			homunculusWndBirthScript.isGachaState = false;
			HomunculusWndScript.SetState(HomunculusWndScript.type_State.birth);
			break;
	}
}

function DeSelect()
{
	local int i;

	for(i = 0; i < LISTITEMMAX; i++)
	{
		listItems[i].SetSelected(false);
	}

	HomunculusWndScript.ClearHomunculusData();
	currentSelectedDataIndex = -1;
	currentSelectedItemIndex = -1;
	currentSelectedIdx = -1;
}

function SetSelect(int dataindex)
{
	local int itemIndex;
	local int i;

	for(i = 0; i < LISTITEMMAX; i++)
	{
		listItems[i].SetSelected(false);
	}

	itemIndex = GetItemIndex(dataindex);
	currentSelectedIdx = homunculusDatas[dataindex].idx;
	currentSelectedDataIndex = dataindex;
	currentSelectedItemIndex = itemIndex;
	if(itemIndex != -1)
	{
		listItems[itemIndex].SetSelected(true);
	}
	HomunculusWndScript.RenewHomunculusData();
}

function Show()
{
	API_EX_SHOW_HOMUNCULUS_INFO();
	Me.ShowWindow();
	Me.SetFocus();
}

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

function SetActiveIdx(bool bActive, int idx)
{
	local int Index;

	Index = GetDataIndexByIdx(idx);
	if(Index == -1)
	{
		return;
	}
	homunculusDatas[Index].Activate = bActive;
	listItems[Index].SetActive(bActive);
	homunculusWndMainViewportScript.SetActive(bActive);
}

function pageChanged(int Page)
{
	HandleListItems(naviBtnClicked);
}

function pageNaviButtonClicked(string buttonName)
{
	naviBtnClicked = true;
}

function SetActiveSlotIndex(int SlotIndex)
{
	if(nActivateSlotIndex > 0)
	{
		HandleListItems();
	}
	nActivateSlotIndex = SlotIndex;
}

function AddNew(int idx)
{
	local int Index;

	Index = GetIndexNew(idx);
	if(Index > -1)
	{
		return;
	}
	listNew[listNew.Length] = idx;
}

function RemNew(int idx)
{
	local int Index;

	Index = GetIndexNew(idx);
	if(Index > -1)
	{
		listNew.Remove(Index, 1);
	}
}

function int HandleNew()
{
	local int i;
	local int dataindex;
	local int lastNewIdx;

	if(! inited)
	{
		listNew.Length = 0;
		return -1;
	}

	for(i = 0; i < homunculusDatas.Length; i++)
	{
		if(homunculusDatas[i].IsNew)
		{
			lastNewIdx = homunculusDatas[i].idx;
			AddNew(lastNewIdx);
		}
	}
	if(lastNewIdx > 0)
	{
		dataindex = GetDataIndexByIdx(lastNewIdx);
		if(dataindex > -1)
		{
			currentSelectedIdx = lastNewIdx;
			currentSelectedDataIndex = dataindex;
			currentSelectedItemIndex = -1;
			return lastNewIdx;
		}
	}
	return -1;
}

function HandleListItems(optional bool dontGoToPage)
{
	local HomunculusAPI.HomunculusData newHomunculusData;
	local int i;
	local int dataindex;
	local int lastNewIdx;

	naviBtnClicked = false;
	homunculusDatas = HomunculusWndScript.API_GetHomunculusDatas();
	if(homunculusDatas.Length == 0)
	{
		homunculusWndMainViewportScript.SetNoneHomunculus();
	}
	if(! dontGoToPage)
	{
		lastNewIdx = HandleNew();
		if(lastNewIdx > 0)
		{
			pageNavi.Go(((GetDataIndexByIdx(lastNewIdx)) / 9) + 1);
			return;
		}
		if(currentSelectedIdx > -1)
		{
			if((GetPageByDataIndex(currentSelectedDataIndex)) != pageNavi.GetPage())
			{
				pageNavi.Go(GetPageByDataIndex(currentSelectedDataIndex));
				return;
			}
		}
	}
	
	for (i = 0; i < LISTITEMMAX; i++)
	{
		dataindex = GetDataIndex(i);
		if(dataindex < homunculusDatas.Length)
		{
			newHomunculusData = homunculusDatas[dataindex];
			listItems[i].SetState(listItems[i].type_State.Normal);
			newHomunculusData.IsNew = (GetIndexNew(newHomunculusData.idx)) != -1;
			listItems[i].SetHomunculusData(newHomunculusData);
			listItems[i].SetEnable();
		}
		if(dataindex == homunculusDatas.Length && homunculusWndBirthScript.BeProgress())
		{
			listItems[i].SetState(listItems[i].type_State.READY);
			listItems[i].SetOnBirth();
			listItems[i].SetEnable();
		}
		if(dataindex < nActivateSlotIndex)
		{
			listItems[i].SetState(listItems[i].type_State.READY);
		}
		if(dataindex == nActivateSlotIndex)
		{
			listItems[i].SetState(listItems[i].type_State.NOTACTIVE);
			listItems[i].SetEnable();
		}
		listItems[i].SetState(listItems[i].type_State.Lock);
	}
	dataindex = GetDataIndexByIdx(currentSelectedIdx);
	if(dataindex == -1)
	{
		DeSelect();		
	}
	else
	{
		SetSelect(dataindex);
	}
}

function Handle_S_EX_DELETE_HOMUNCLUS_DATA_RESULT()
{
	local UIPacket._S_EX_DELETE_HOMUNCLUS_DATA_RESULT packet;

	if(! class'UIPacket'.static.Decode_S_EX_DELETE_HOMUNCLUS_DATA_RESULT(packet))
	{
		return;
	}
	if(packet.Type == 1)
	{
		AddSystemMessage(13252);
		DeSelect();		
	}
	else
	{
		AddSystemMessage(packet.nID);
	}
}

function Handle_S_EX_HOMUNCULUS_ACTIVATE_SLOT_RESULT()
{
	local UIPacket._S_EX_HOMUNCULUS_ACTIVATE_SLOT_RESULT packet;

	if(! class'UIPacket'.static.Decode_S_EX_HOMUNCULUS_ACTIVATE_SLOT_RESULT(packet))
	{
		return;
	}
	if(packet.Type == 1)
	{
		HandleListItems();
	}
}

function int GetDataIndex(int itemIndex)
{
	return itemIndex + ((pageNavi.GetPage() - 1) * 9);
}

function int GetItemIndex(int dataindex)
{
	if(pageNavi.GetPage() != ((dataindex / 9) + 1))
	{
		return -1;
	}
	return dataindex - ((pageNavi.GetPage() - 1) * 9);
}

function int GetPageByDataIndex(int dataindex)
{
	return (dataindex / 9) + 1;
}

function int GetDataIndexByIdx(int idx)
{
	local int i;

	for ( i = 0;i < homunculusDatas.Length;i++ )
	{
		if(homunculusDatas[i].idx == idx)
		{
			return i;
		}
	}
	return -1;
}

function int GetIndexNew(int idx)
{
	local int i;

	for ( i = 0;i < listNew.Length;i++ )
	{
		if(listNew[i] == idx)
		{
			return i;
		}
	}
	return -1;
}

function HomunculusAPI.HomunculusData GetCurrHomunculusData()
{
	local HomunculusAPI.HomunculusData emptyHomunculusData;

	if(currentSelectedDataIndex >= 0)
	{
		return homunculusDatas[currentSelectedDataIndex];
	}
	else
	{
		return emptyHomunculusData;
	}
}

function int GetEmptySlot()
{
	local int firstEmptySlot;

	firstEmptySlot = homunculusDatas.Length;
	if(homunculusWndBirthScript.BeProgress())
	{
		firstEmptySlot = firstEmptySlot + 1;
	}
	if(firstEmptySlot == nActivateSlotIndex)
	{
		return -1;
	}
	return firstEmptySlot;
}

function bool CanActiveLock()
{
	local int SlotIndex;

	SlotIndex = nActivateSlotIndex;
	if(homunculusWndBirthScript.BeProgress())
	{
		SlotIndex = SlotIndex + 1;
	}
	return SlotIndex < 18;
}
