class DethroneTab04_Mission extends UICommonAPI;

var string m_WindowName;
var WindowHandle Me;
var TextureHandle ServerRichListFrame;
var RichListCtrlHandle Tab_RichList;
var TextureHandle ListBg_tex;
var ButtonHandle Refresh_btn;
var WindowHandle DisableWndList;
var TextBoxHandle List_Empty;

function OnRegisterEvent()
{
	RegisterEvent(EV_ProtocolBegin + class'UIPacket'.const.S_EX_DETHRONE_DAILY_MISSION_INFO);
	RegisterEvent(EV_ProtocolBegin + class'UIPacket'.const.S_EX_DETHRONE_DAILY_MISSION_GET_REWARD);
}

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

function Initialize()
{
	m_WindowName = getCurrentWindowName(string(self));
	Me = GetWindowHandle(m_WindowName);
	ServerRichListFrame = GetTextureHandle(m_WindowName $ ".ServerRichListFrame");
	Tab_RichList = GetRichListCtrlHandle(m_WindowName $ ".Tab_RichList");
	ListBg_tex = GetTextureHandle(m_WindowName $ ".ListBg_tex");
	Refresh_btn = GetButtonHandle(m_WindowName $ ".Refresh_btn");
	DisableWndList = GetWindowHandle(m_WindowName $ ".DisableWndList");
	List_Empty = GetTextBoxHandle(m_WindowName $ ".DisableWndList.List_Empty");
	Tab_RichList.SetSelectedSelTooltip(false);
	Tab_RichList.SetAppearTooltipAtMouseX(true);
}

event OnShow()
{
	Debug("Onshow " @ m_WindowName);
	if(GetWindowHandle("DethroneWnd").IsShowWindow())
	{
		API_C_EX_DETHRONE_DAILY_MISSION_INFO();
	}
}

function Load()
{}

function OnClickButton(string Name)
{
	switch(Name)
	{
		case "Refresh_btn":
			OnRefresh_btnClick();
			break;
		case "rewardBtn":
			onRewardBtnClick();
			break;
	}
}

function onRewardBtnClick()
{
	local RichListCtrlRowData rowData;

	Debug("rewardBtn --> " @ string(Tab_RichList.GetSelectedIndex()));
	Tab_RichList.GetRec(Tab_RichList.GetSelectedIndex(), rowData);
	Debug("rowData.cellDataList[2].szData" @ rowData.cellDataList[2].szData @ string(rowData.nReserved1));
	if(rowData.cellDataList[2].szData == "True")
	{
		API_C_EX_DETHRONE_DAILY_MISSION_GET_REWARD(int(rowData.nReserved1));
		OnRefresh_btnClick();
	}
}

function OnRefresh_btnClick()
{
	API_C_EX_DETHRONE_DAILY_MISSION_INFO();
	DethroneWnd(GetScript("DethroneWnd")).setDisableWnd();
}

function AddDethronePointStateListItem(int nMissionID, string MissionName, int Progress, int progressTotal, bool bReward)
{
	local RichListCtrlRowData rowData;
	local float statusPercent;

	rowData.cellDataList.Length = 3;
	addRichListCtrlString(rowData.cellDataList[0].drawitems, MissionName, GTColor().White, false, 0, 0);
	statusPercent = (Progress / progressTotal) * 100;
	if(statusPercent >= 100)
	{
		AddRichListCtrlStatusInfo(rowData.cellDataList[1].drawitems, 200, 15, 15, 6, true, statusPercent, 0.0f, (string(Progress) $ " / ") $ string(progressTotal), 3);		
	}
	else
	{
		AddRichListCtrlStatusInfo(rowData.cellDataList[1].drawitems, 200, 15, 15, 6, true, statusPercent, 0.0f, (string(Progress) $ " / ") $ string(progressTotal), 2);
	}
	if(bReward)
	{
		addRichListCtrlButton(rowData.cellDataList[2].drawitems, "rewardBtn", 0, 0, "L2UI_EPIC.CollectionSystemWnd.CollectionSystemWnd_List_AdditionalReward", "L2UI_EPIC.CollectionSystemWnd.CollectionSystemWnd_List_AdditionalReward", "L2UI_EPIC.CollectionSystemWnd.CollectionSystemWnd_List_AdditionalReward", 34, 38, 34, 38);		
	}
	else
	{
		addRichListCtrlButton(rowData.cellDataList[2].drawitems, "rewardBtn", 0, 0, "L2UI_EPIC.DethroneWnd.List_Icon_RewardDisable", "L2UI_EPIC.DethroneWnd.List_Icon_RewardDisable", "L2UI_EPIC.DethroneWnd.List_Icon_RewardDisable", 34, 38, 34, 38);
	}
	rowData.cellDataList[2].drawitems[rowData.cellDataList[2].drawitems.Length - 1].TooltipDesc = GetSystemString(7529);
	rowData.cellDataList[0].szData = MissionName;
	rowData.cellDataList[1].szData = string(Progress);
	rowData.cellDataList[2].szData = string(bReward);
	rowData.nReserved1 = nMissionID;
	Tab_RichList.InsertRecord(rowData);
}

function ModifyDethronePointStateListItem(int indexRecord, bool bReward)
{
	local RichListCtrlRowData rowData;

	Tab_RichList.GetRec(indexRecord, rowData);
	rowData.cellDataList[2].drawitems.Length = 0;
	addRichListCtrlButton(rowData.cellDataList[2].drawitems, "rewardBtn", 0, 0, "L2UI_EPIC.DethroneWnd.List_Icon_RewardDisable", "L2UI_EPIC.DethroneWnd.List_Icon_RewardDisable", "L2UI_EPIC.DethroneWnd.List_Icon_RewardDisable", 34, 38, 34, 38);
	rowData.cellDataList[2].szData = string(bReward);
	Tab_RichList.ModifyRecord(indexRecord, rowData);
}

function OnEvent(int Event_ID, string param)
{
	switch(Event_ID)
	{
		case EV_PacketID(class'UIPacket'.const.S_EX_DETHRONE_DAILY_MISSION_INFO):
			ParsePacket_S_EX_DETHRONE_DAILY_MISSION_INFO();
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_DETHRONE_DAILY_MISSION_GET_REWARD):
			ParsePacket_S_EX_DETHRONE_DAILY_MISSION_GET_REWARD();
			break;
	}
}

function ParsePacket_S_EX_DETHRONE_DAILY_MISSION_GET_REWARD()
{
	local UIPacket._S_EX_DETHRONE_DAILY_MISSION_GET_REWARD packet;
	local int Index;

	if(! class'UIPacket'.static.Decode_S_EX_DETHRONE_DAILY_MISSION_GET_REWARD(packet))
	{
		return;
	}
	Debug(" -->  Decode_S_EX_DETHRONE_DAILY_MISSION_GET_REWARD :  " @ string(packet.bSuccess) @ string(packet.nID) @ string(packet.nPersonalDethronePoint) @ string(packet.nServerDethronePoint));
	if(packet.bSuccess > 0)
	{
		AddSystemMessageString(MakeFullSystemMsg(GetSystemMessage(13428), string(packet.nPersonalDethronePoint), string(packet.nServerDethronePoint)));
		getInstanceL2Util().showGfxScreenMessage(MakeFullSystemMsg(GetSystemMessage(13428), string(packet.nPersonalDethronePoint), string(packet.nServerDethronePoint)));
	}
	Index = getListIndexByMissionID(packet.nID);
	if(Index > -1)
	{
		ModifyDethronePointStateListItem(Index, false);
	}
	deleteNoticeCheck();
}

function deleteNoticeCheck()
{
	local int i;
	local RichListCtrlRowData rowData;

	for(i = 0; i < Tab_RichList.GetRecordCount(); i++)
	{
		Tab_RichList.GetRec(i, rowData);
		if(rowData.cellDataList[2].szData == "True")
		{
			return;
		}
	}
	NoticeWnd(GetScript("NoticeWnd")).removeNoticeDethroneMissionNotice();
}

function int getListIndexByMissionID(int nMissionID)
{
	local int i;
	local RichListCtrlRowData rowData;

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

function ParsePacket_S_EX_DETHRONE_DAILY_MISSION_INFO()
{
	local int i;
	local UIPacket._S_EX_DETHRONE_DAILY_MISSION_INFO packet;
	local DethroneDailyMissionData missionData;

	if(! class'UIPacket'.static.Decode_S_EX_DETHRONE_DAILY_MISSION_INFO(packet))
	{
		return;
	}
	Debug(" -->  Decode_S_EX_DETHRONE_DAILY_MISSION_INFO :  len->" @ string(packet.missionInfoList.Length));
	Tab_RichList.DeleteAllItem();

	for(i = 0; i < packet.missionInfoList.Length; i++)
	{
		Debug("" @ string(packet.missionInfoList[i].nID));
		GetDethroneDailyMissionData(packet.missionInfoList[i].nID, missionData);
		AddDethronePointStateListItem(packet.missionInfoList[i].nID, missionData.Name, packet.missionInfoList[i].nCount, missionData.GoalCount, numToBool(packet.missionInfoList[i].bHasReward));
	}
	Tab_RichList.SetFocus();
	deleteNoticeCheck();
}

function API_C_EX_DETHRONE_DAILY_MISSION_INFO()
{
	local array<byte> stream;

	class'UIPacket'.static.RequestUIPacket(class'UIPacket'.const.C_EX_DETHRONE_DAILY_MISSION_INFO, stream);
	Debug("----> Api Call : C_EX_DETHRONE_DAILY_MISSION_INFO ");
}

function API_C_EX_DETHRONE_DAILY_MISSION_GET_REWARD(int nMissionID)
{
	local array<byte> stream;
	local UIPacket._C_EX_DETHRONE_DAILY_MISSION_GET_REWARD packet;

	packet.nID = nMissionID;
	if(! class'UIPacket'.static.Encode_C_EX_DETHRONE_DAILY_MISSION_GET_REWARD(stream, packet))
	{
		return;
	}
	class'UIPacket'.static.RequestUIPacket(class'UIPacket'.const.C_EX_DETHRONE_DAILY_MISSION_GET_REWARD, stream);
	Debug("----> Api Call : C_EX_DETHRONE_DAILY_MISSION_GET_REWARD " @ string(nMissionID));
}
