class SiegeRankingWnd extends UICommonAPI;

var string m_WindowName;
var WindowHandle Me;
var ButtonHandle btnClose;
var TextBoxHandle Descriptext;
var array<string> arrRankData;
var bool bFlag;
var RichListCtrlHandle Ranking_RichList;

function Initialize()
{
	Me = GetWindowHandle(m_WindowName);
	btnClose = GetButtonHandle(m_WindowName $ ".SiegeMercenaryClose_Btn");
	Ranking_RichList = GetRichListCtrlHandle(m_WindowName $ ".Ranking_RichList");
	Descriptext = GetTextBoxHandle(m_WindowName $ ".Descriptext");
	Ranking_RichList.SetAppearTooltipAtMouseX(true);
	Ranking_RichList.SetSelectedSelTooltip(false);
	bFlag = false;	
}

function OnRegisterEvent()
{
	RegisterEvent(EV_MCW_CastleSiegeAttackerListStart);
	RegisterEvent(EV_MCW_CastleSiegeAttackerListItem);
	RegisterEvent(EV_MCW_CastleSiegeAttackerListEnd);
	RegisterEvent(EV_MCW_CastleSiegeDefenderListStart);
	RegisterEvent(EV_MCW_CastleSiegeDefenderListItem);
	RegisterEvent(EV_MCW_CastleSiegeDefenderListEnd);	
}

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

function OnEvent(int Event_ID, string param)
{
	switch(Event_ID)
	{
		case EV_MCW_CastleSiegeAttackerListStart:
			if(bFlag)
			{
				HandleSiegeInfoAttackerStart();
			}
			break;
		case EV_MCW_CastleSiegeAttackerListItem:
			if(bFlag)
			{
				HandleSiegeInfoAttackerList(param);
			}
			break;
		case EV_MCW_CastleSiegeAttackerListEnd:
			break;
		case EV_MCW_CastleSiegeDefenderListStart:
			break;
		case EV_MCW_CastleSiegeDefenderListItem:
			if(bFlag)
			{
				HandleSiegeInfoDefenderList(param);
			}
			break;
		case EV_MCW_CastleSiegeDefenderListEnd:
			if(bFlag)
			{
				HandleSiegeInfoDefenderEnd();
			}
			break;
	}	
}

function HandleSiegeInfoAttackerStart()
{
	arrRankData.Remove(0, arrRankData.Length);
	Ranking_RichList.DeleteAllItem();
	Descriptext.ShowWindow();	
}

function HandleSiegeInfoAttackerList(string param)
{
	local int point;

	ParseInt(param, "CurrentPledgeSiegePoint", point);
	if(point > 0)
	{
		arrRankData.Insert(arrRankData.Length, 1);
		arrRankData[arrRankData.Length - 1] = param;
	}	
}

function HandleSiegeInfoAttackerEnd();

function HandleSiegeInfoDefenderStart();

function HandleSiegeInfoDefenderList(string param)
{
	local int point;

	ParseInt(param, "CurrentPledgeSiegePoint", point);
	if(point > 0)
	{
		arrRankData.Insert(arrRankData.Length, 1);
		arrRankData[arrRankData.Length - 1] = param;
	}	
}

function HandleSiegeInfoDefenderEnd()
{
	local int i, TOTALLEN;
	local string PledgeName;
	local int nCurrentPledgeSiegePoint;

	TOTALLEN = 3;
	arrRankData.Sort(SortByNumberDelegate);
	if(arrRankData.Length == 0)
	{
		Descriptext.ShowWindow();		
	}
	else
	{
		Descriptext.HideWindow();
		if(arrRankData.Length < 3)
		{
			TOTALLEN = arrRankData.Length;
		}

		for(i = 0; i < TOTALLEN; i++)
		{
			ParseString(arrRankData[i], "PledgeName", PledgeName);
			ParseInt(arrRankData[i], "CurrentPledgeSiegePoint", nCurrentPledgeSiegePoint);
			addRichListItemAmount(i + 1, PledgeName, nCurrentPledgeSiegePoint);
		}
	}
	bFlag = false;	
}

delegate int SortByNumberDelegate(string aParam, string bparam)
{
	local int aPoint, bPoint;

	ParseInt(aParam, "CurrentPledgeSiegePoint", aPoint);
	ParseInt(bparam, "CurrentPledgeSiegePoint", bPoint);
	if(aPoint < bPoint)
	{
		return -1;
	}
	return 0;	
}

function addRichListItemAmount(int Rank, string PledgeName, int sc)
{
	local RichListCtrlRowData Record;

	Record.cellDataList.Length = 3;
	Record.nReserved1 = Rank;
	addRichListCtrlString(Record.cellDataList[0].drawitems, string(Rank), GTColor().BWhite, false, 0, 0);
	addRichListCtrlString(Record.cellDataList[1].drawitems, PledgeName, GTColor().BWhite, false, 0, 0);
	addRichListCtrlString(Record.cellDataList[2].drawitems, MakeFullSystemMsg(GetSystemMessage(13712), string(sc)), GTColor().BWhite, false, 0, 0);
	Ranking_RichList.InsertRecord(Record);	
}

function OnClickButton(string btnName)
{
	switch(btnName)
	{
		case "SiegeMercenaryClose_Btn":
			if(Me.IsShowWindow())
			{
				Me.HideWindow();				
			}
			else
			{
				Me.ShowWindow();
			}
			break;
	}	
}

function int GetCastleID()
{
	local SiegeWnd siegeWndScript;

	siegeWndScript = SiegeWnd(GetScript("SiegeWnd"));
	return siegeWndScript.castleIDs[siegeWndScript.SelectedIndex];	
}

function OnShow()
{
	API_RequestMCWCastleSiegeAttackerList(GetCastleID());
	API_RequestMCWCastleSiegeDefenderList(GetCastleID());
	bFlag = true;	
}

function API_RequestMCWCastleSiegeAttackerList(int tmpCastleID)
{
	if(tmpCastleID > 0)
	{
		class'SiegeAPI'.static.RequestMCWCastleSiegeAttackerList(tmpCastleID);
	}	
}

function API_RequestMCWCastleSiegeDefenderList(int tmpCastleID)
{
	if(tmpCastleID > 0)
	{
		class'SiegeAPI'.static.RequestMCWCastleSiegeDefenderList(tmpCastleID);
	}	
}

defaultproperties
{
	m_WindowName="SiegeRankingWnd"
}
