class QuestAcceptableListWnd extends UICommonAPI;

var private RichListCtrlHandle questAcceptableList_RichList;
var private bool bRequest_S_EX_QUEST_ACCEPTABLE_LIST;

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

event OnRegisterEvent()
{
	RegisterEvent(EV_Restart);
	RegisterEvent(EV_PacketID(class'UIPacket'.const.S_EX_QUEST_ACCEPTABLE_LIST));
	RegisterEvent(EV_PacketID(class'UIPacket'.const.S_EX_QUEST_ACCEPTABLE_ALARM));
	RegisterEvent(EV_PacketID(class'UIPacket'.const.S_EX_QUEST_NOTIFICATION));	
}

event OnLoad()
{
	SetClosingOnESC();
	Initialize();	
}

function Initialize()
{
	questAcceptableList_RichList = GetRichListCtrlHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".questAcceptableList_RichList");
	questAcceptableList_RichList.SetSelectedSelTooltip(false);
	questAcceptableList_RichList.SetAppearTooltipAtMouseX(true);	
}

event OnEvent(int a_EventID, string a_Param)
{
	switch(a_EventID)
	{
		case EV_Restart:
			bRequest_S_EX_QUEST_ACCEPTABLE_LIST = false;
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_QUEST_ACCEPTABLE_LIST):
			RT_S_EX_QUEST_ACCEPTABLE_LIST();
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_QUEST_ACCEPTABLE_ALARM):
			if(m_hOwnerWnd.IsShowWindow())
			{
				RQ_C_EX_QUEST_ACCEPTABLE_LIST();
			}
			RT_S_EX_QUEST_ACCEPTABLE_ALARM();
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_QUEST_NOTIFICATION):
			RT_S_EX_QUEST_NOTIFICATION();
			break;
	}	
}

event OnShow()
{
	GetWindowHandle("QuestWnd").HideWindow();
	RQ_C_EX_QUEST_ACCEPTABLE_LIST();
	QuestProgressWnd(GetScript("QuestProgressWnd"))._Show();
	m_hOwnerWnd.SetFocus();	
}

event OnClickButton(string Name)
{
	switch(Name)
	{
		case "SwapBtn":
			getInstanceL2Util().syncWindowLoc(getCurrentWindowName(string(self)), "QuestWnd");
			GetWindowHandle("QuestWnd").ShowWindow();
			break;
	}	
}

event OnClickListCtrlRecord(string ListCtrlID)
{
	questAcceptableList_RichList.SetSelectedIndex(-1, false);	
}

event OnDBClickListCtrlRecord(string ListCtrlID)
{
	class'QuestDialogWnd'.static.Inst()._ShowStartDialog(GetQuestIDAtIndex(questAcceptableList_RichList.GetSelectedIndex()));
	questAcceptableList_RichList.SetSelectedIndex(-1, false);	
}

event OnReceivedCloseUI()
{
	PlayConsoleSound(IFST_WINDOW_CLOSE);
	GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath).HideWindow();	
}

private function AcceptQuest(int qID)
{
	local int Index;

	if(! m_hOwnerWnd.IsShowWindow())
	{
		return;
	}
	Index = GetQuestIndex(qID);
	if(Index == -1)
	{
		return;
	}
	questAcceptableList_RichList.DeleteRecord(Index);	
}

private function bool MakeRowData(int qID, out RichListCtrlRowData o_rowData)
{
	local string questName;
	local NQuestUIData questUIData;
	local RichListCtrlRowData rowData;

	if(! API_GetNQuestData(qID, questUIData))
	{
		return false;
	}
	rowData.cellDataList.Length = 1;
	rowData.nReserved1 = qID;
	questName = questUIData.Name;
	addRichListCtrlString(rowData.cellDataList[0].drawitems, questName, GetColor(211, 211, 211, 255));
	o_rowData = rowData;
	return true;	
}

private function Handle_S_EX_QUEST_NOTIFICATION(int qID, int cNotifType)
{
	switch(cNotifType)
	{
		case QuestWnd(GetScript("QuestWnd")).const.QUEST_WINDOW_ONE:
			AcceptQuest(qID);
			break;
	}	
}

private function Handle_S_EX_QUEST_ACCEPTABLE_LIST(array<int> questIDs)
{
	local int i;
	local RichListCtrlRowData rowData;

	questAcceptableList_RichList.DeleteAllItem();

	for(i = 0; i < questIDs.Length; i++)
	{
		if(MakeRowData(questIDs[i], rowData))
		{
			if(IsMainQuest(questIDs[i]))
			{
				InsertRecord(rowData, 0);
				// [Explicit Continue]
				continue;
			}
			questAcceptableList_RichList.InsertRecord(rowData);
		}
	}	
}

private function InsertRecord(RichListCtrlRowData rowDataNew, int Index)
{
	local int i, lastIndex;
	local RichListCtrlRowData rowData;

	if(questAcceptableList_RichList.GetRecordCount() == 0)
	{
		questAcceptableList_RichList.InsertRecord(rowDataNew);		
	}
	else
	{
		lastIndex = questAcceptableList_RichList.GetRecordCount() - 1;
		questAcceptableList_RichList.GetRec(lastIndex, rowData);
		questAcceptableList_RichList.InsertRecord(rowData);

		for(i = lastIndex; i > Index; i--)
		{
			questAcceptableList_RichList.GetRec(i - 1, rowData);
			questAcceptableList_RichList.ModifyRecord(i, rowData);
		}
		questAcceptableList_RichList.ModifyRecord(Index, rowDataNew);
	}	
}

private function RT_S_EX_QUEST_NOTIFICATION()
{
	local UIPacket._S_EX_QUEST_NOTIFICATION packet;

	if(! m_hOwnerWnd.IsShowWindow())
	{
		return;
	}
	if(! class'UIPacket'.static.Decode_S_EX_QUEST_NOTIFICATION(packet))
	{
		return;
	}
	Handle_S_EX_QUEST_NOTIFICATION(packet.nID, packet.cNotifType);	
}

private function RT_S_EX_QUEST_ACCEPTABLE_LIST()
{
	local UIPacket._S_EX_QUEST_ACCEPTABLE_LIST packet;

	bRequest_S_EX_QUEST_ACCEPTABLE_LIST = false;
	if(! class'UIPacket'.static.Decode_S_EX_QUEST_ACCEPTABLE_LIST(packet))
	{
		return;
	}
	Handle_S_EX_QUEST_ACCEPTABLE_LIST(packet.questIDs);	
}

private function RQ_C_EX_QUEST_ACCEPTABLE_LIST()
{
	if(bRequest_S_EX_QUEST_ACCEPTABLE_LIST)
	{
		return;
	}
	class'UIPacket'.static.RequestUIPacket(class'UIPacket'.const.C_EX_QUEST_ACCEPTABLE_LIST);	
}

private function RT_S_EX_QUEST_ACCEPTABLE_ALARM()
{
	NoticeWnd(GetScript("NoticeWnd")).ArriveShowQuest();	
}

private function bool API_GetNQuestData(int a_QuestID, out NQuestUIData o_data)
{
	return GetNQuestData(a_QuestID, o_data);	
}

private function bool IsMainQuest(int qID)
{
	return qID < class'QuestDialogWnd'.const.QUEST_CATEGORY_SUB;	
}

private function int GetQuestIDAtIndex(int Index)
{
	local RichListCtrlRowData rowData;

	if(questAcceptableList_RichList.GetRecordCount() <= Index)
	{
		return -1;
	}
	questAcceptableList_RichList.GetRec(Index, rowData);
	return int(rowData.nReserved1);	
}

private function int GetQuestIndex(int qID)
{
	local int i;
	local RichListCtrlRowData rowData;

	for(i = 0; i < questAcceptableList_RichList.GetRecordCount(); i++)
	{
		questAcceptableList_RichList.GetRec(i, rowData);
		if(rowData.nReserved1 == qID)
		{
			return i;
		}
	}
	return -1;	
}
