/**
 *   출석부 시스템(해외쪽 부터 개발되고, 국내에도 사용 예정)
 *   
 *   관련 빌드 명령어 
 *   //attendance info
 *   //attendance check 1(날짜)
 *   //attendance reset [type1:normal, 2:newbie, 3:dormant]
 *   //attendance reset 1  (타겟 찍고)
 *   //attendance reset_time  (타겟 찍고)
 *   //pccafe on     //pc방 모드
 *   
 **/ 

class AttendCheckWnd extends UICommonAPI;

const TOTAL_ATENDANCE_Number = 28;

var private bool bInitSlot;
var private bool bInitPayments;
var private bool bAttendToday;
var private UIPacket._S_EX_VIP_ATTENDANCE_LIST packet_attendanceList;
var private array<AttendCheckSlot> attendCheckSlots;
var private UIControlDialogAssets buyBtnDialogAsset;
var private RichListCtrlHandle Reward_RichList;
var private WindowHandle Reward_BtnUIControlGroupButtonAsset;
var private L2UITimerObject tObject;
var private L2UITimerObject tObjectReward;
var private int followItemID;
var private WindowHandle DisableWindow;
var private int clickedDay;

event OnRegisterEvent()
{
	RegisterEvent(EV_GameStart);
	RegisterEvent(EV_Restart);
	RegisterEvent(EV_PacketID(class'UIPacket'.const.S_EX_VIP_ATTENDANCE_LIST));
	RegisterEvent(EV_PacketID(class'UIPacket'.const.S_EX_VIP_ATTENDANCE_CHECK));
	RegisterEvent(EV_PacketID(class'UIPacket'.const.S_EX_VIP_ATTENDANCE_REWARD));
	RegisterEvent(EV_PacketID(class'UIPacket'.const.S_EX_VIP_ATTENDANCE_NOTIFY));	
}

event OnLoad()
{
	Reward_RichList = GetRichListCtrlHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".Reward_BtnUIControlGroupButtonAsset.Reward_RichList");
	Reward_RichList.SetSelectedSelTooltip(false);
	Reward_RichList.SetAppearTooltipAtMouseX(true);
	Reward_RichList.SetSortable(false);
	Reward_RichList.SetSelectable(false);
	Reward_BtnUIControlGroupButtonAsset = GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".Reward_BtnUIControlGroupButtonAsset");
	DisableWindow = GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".DisableWindow");
	SetClosingOnESC();
	InitAssets();
	InitSlots();
	InitTimer();
}

event OnShow()
{
	Reward_BtnUIControlGroupButtonAsset.HideWindow();
	buyBtnDialogAsset.Hide();
	getInstanceL2Util().ItemRelationWindowHide(getCurrentWindowName(string(self)));
	InitSlotItems();
	InitClientAttendanceDatas();
	m_hOwnerWnd.SetFocus();	
}

event OnHide()
{
	tObject._Stop();
	tObjectReward._Stop();
}

event OnEvent(int Event_ID, string a_Param)
{
	switch(Event_ID)
	{
		case EV_GameStart:
			InitPeriodString();
			break;
		case EV_Restart:
			InitFlags();
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_VIP_ATTENDANCE_LIST):
			Rt_S_EX_VIP_ATTENDANCE_LIST();
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_VIP_ATTENDANCE_CHECK):
			Rt_S_EX_VIP_ATTENDANCE_CHECK();
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_VIP_ATTENDANCE_REWARD):
			Rt_S_EX_VIP_ATTENDANCE_REWARD();
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_VIP_ATTENDANCE_NOTIFY):
			Rt_S_EX_VIP_ATTENDANCE_NOTIFY();
			break;
	}
}

event OnClickButton(string Name)
{
	switch(Name)
	{
		case "WndClose_BTN":
			m_hOwnerWnd.HideWindow();
			break;
		case "HelpBtn":
			HandleHelpBtnClicked();
			break;
		case "Buy_Btn":
			HandleBuyBtn();
			break;
		case "OkButton":
			Rq_C_EX_VIP_ATTENDANCE_REWARD();
		case "CancleButton":
			HideRewardPopup();
			break;
	}
}

event OnClickButtonWithHandle(ButtonHandle a_ButtonHandle)
{
	if(a_ButtonHandle.GetWindowName() == "Reward_Btn")
	{
		HandleOnClickRewardBtn(a_ButtonHandle);
	}	
}

private function HandleOnClickRewardBtn(ButtonHandle a_ButtonHandle)
{
	local array<string> btnNames;

	Split(a_ButtonHandle.GetParentWindowName(), "_", btnNames);
	if(btnNames.Length < 2)
	{
		return;
	}
	clickedDay = int(btnNames[1]) + 1;
	HandleRewardBtn();
}

event OnReceivedCloseUI()
{
	if(buyBtnDialogAsset.m_hOwnerWnd.IsShowWindow())
	{
		buyBtnDialogAsset.Hide();
		return;
	}
	if(Reward_BtnUIControlGroupButtonAsset.IsShowWindow())
	{
		HideRewardPopup();
		return;
	}
	PlayConsoleSound(IFST_WINDOW_CLOSE);
	m_hOwnerWnd.HideWindow();
}

private function HandleRewardBtn()
{
	local int i;
	local RichListCtrlRowData rowData;
	local ItemInfo iInfo;

	class'UIDATA_ITEM'.static.GetItemInfo(GetItemID(57), iInfo);
	Reward_RichList.DeleteAllItem();

	for(i = packet_attendanceList.cRewardDay; i < clickedDay; i++)
	{
		attendCheckSlots[i]._GetItemInfo(iInfo);
		if(MakeRewardItem(i, iInfo, rowData))
		{
			Reward_RichList.InsertRecord(rowData);
		}
	}
	if(Reward_RichList.GetRecordCount() == 0)
	{
		return;
	}
	DisableWindow.ShowWindow();
	DisableWindow.SetFocus();
	Reward_BtnUIControlGroupButtonAsset.ShowWindow();
	Reward_BtnUIControlGroupButtonAsset.SetFocus();	
}

private function HideRewardPopup()
{
	DisableWindow.HideWindow();
	Reward_BtnUIControlGroupButtonAsset.HideWindow();	
}

private function HandleBuyBtn()
{
	if(! bAttendToday)
	{
		class'L2Util'.static.Inst().showGfxScreenMessage(GetSystemMessage(13848));
		return;
	}
	buyBtnDialogAsset.SetDialogDesc(GetSystemMessage(13846));
	buyBtnDialogAsset.SetUseBuyItem(false);
	buyBtnDialogAsset.SetUseNeedItem(true);
	buyBtnDialogAsset.SetUseNumberInput(false);
	buyBtnDialogAsset._SetUseItemWindow(false);
	buyBtnDialogAsset.DelegateOnClickBuy = OnBuyBtnDialogConfirmOneDay;
	buyBtnDialogAsset.StartNeedItemList(1);
	buyBtnDialogAsset.AddNeedItemClassID(followItemID, attendCheckSlots[packet_attendanceList.cAttendanceDay]._followcost);
	buyBtnDialogAsset.SetItemNum(1);
	buyBtnDialogAsset.Show();	
}

private function delegateOnItemCountEdited(INT64 ItemCount)
{
	buyBtnDialogAsset.StartNeedItemList(1);
	buyBtnDialogAsset.AddNeedItemClassID(57, ItemCount);	
}

private function OnBuyBtnDialogConfirmOneDay()
{
	Rq_C_EX_VIP_ATTENDANCE_CHECK();
	buyBtnDialogAsset.Hide();	
}

private function OnBuyBtnDialogCancel()
{
	buyBtnDialogAsset.Hide();	
}

private function SetRewardDays()
{
	local int i;

	for(i = 0; i < packet_attendanceList.cRewardDay; i++)
	{
		attendCheckSlots[i]._SetRewardDay();
	}	
}

private function SetAttendDays()
{
	local int i;

	for(i = packet_attendanceList.cRewardDay; i < packet_attendanceList.cAttendanceDay; i++)
	{
		attendCheckSlots[i]._SetAttendDay();
	}
	tObject._Stop();
	bAttendToday = packet_attendanceList.nRemainCheckTime == 0;
	if(packet_attendanceList.cRollBookDay <= packet_attendanceList.cAttendanceDay)
	{
		return;
	}
	if(! bAttendToday)
	{
		packet_attendanceList.cFollowBaseDay = packet_attendanceList.cAttendanceDay + 1;
		tObject._maxCount = packet_attendanceList.nRemainCheckTime;
		attendCheckSlots[packet_attendanceList.cAttendanceDay]._SetTimerDay(packet_attendanceList.nRemainCheckTime);
		tObject._Reset();
	}	
}

private function SetRollBookDays()
{
	local int i, followStartDay, rollBookDay;

	rollBookDay = Min(TOTAL_ATENDANCE_Number, packet_attendanceList.cRollBookDay);
	if(bAttendToday)
	{
		followStartDay = packet_attendanceList.cAttendanceDay;		
	}
	else
	{
		followStartDay = packet_attendanceList.cAttendanceDay + 1;
	}
	
	for(i = followStartDay; i < rollBookDay; i++)
	{
		attendCheckSlots[i]._SeRollBookDays(i - followStartDay);
	}
}

private function SetDisables()
{
	local int i;

	for(i = packet_attendanceList.cRollBookDay; i < TOTAL_ATENDANCE_Number; i++)
	{
		attendCheckSlots[i]._SetDisable();
	}
}

private function API_GetAttendanceData(out int followItemID, out string Period, out array<int> followCosts)
{
	GetAttendanceData(followItemID, Period, followCosts);	
}

private function Rq_C_EX_VIP_ATTENDANCE_LIST()
{
	local array<byte> stream;

	class'UIPacket'.static.RequestUIPacket(class'UIPacket'.const.C_EX_VIP_ATTENDANCE_LIST, stream);	
}

private function Rt_S_EX_VIP_ATTENDANCE_LIST()
{
	if(! class'UIPacket'.static.Decode_S_EX_VIP_ATTENDANCE_LIST(packet_attendanceList))
	{
		return;
	}
	Debug("Rt_S_EX_VIP_ATTENDANCE_LIST :" @ string(packet_attendanceList.nMinimumLevel) @ string(packet_attendanceList.nRemainCheckTime) @ string(packet_attendanceList.cRollBookDay) @ string(packet_attendanceList.cAttendanceDay) @ string(packet_attendanceList.cRewardDay) @ string(packet_attendanceList.cFollowBaseDay));
	SetRewardDays();
	SetAttendDays();
	SetRollBookDays();
	SetDisables();
	m_hOwnerWnd.ShowWindow();	
}

private function Rq_C_EX_VIP_ATTENDANCE_CHECK()
{
	local array<byte> stream;
	local UIPacket._C_EX_VIP_ATTENDANCE_CHECK packet;

	if(! IsLevelCondition())
	{
		class'L2Util'.static.Inst().showGfxScreenMessage(GetSystemMessage(6129));
		return;
	}
	packet.cDay = packet_attendanceList.cAttendanceDay + 1;
	if(! class'UIPacket'.static.Encode_C_EX_VIP_ATTENDANCE_CHECK(stream, packet))
	{
		return;
	}
	class'UIPacket'.static.RequestUIPacket(class'UIPacket'.const.C_EX_VIP_ATTENDANCE_CHECK, stream);	
}

private function Rt_S_EX_VIP_ATTENDANCE_CHECK()
{
	local UIPacket._S_EX_VIP_ATTENDANCE_CHECK packet;

	if(! class'UIPacket'.static.Decode_S_EX_VIP_ATTENDANCE_CHECK(packet))
	{
		return;
	}
	if(packet.bResult == 1)
	{
		attendCheckSlots[packet_attendanceList.cAttendanceDay]._SetAttendDay();
		attendCheckSlots[packet_attendanceList.cAttendanceDay]._SetHighLight();
		packet_attendanceList.cAttendanceDay = packet_attendanceList.cAttendanceDay + 1;
		SetRollBookDays();
	}	
}

private function Rq_C_EX_VIP_ATTENDANCE_REWARD()
{
	local array<byte> stream;
	local UIPacket._C_EX_VIP_ATTENDANCE_REWARD packet;

	if(! IsLevelCondition())
	{
		class'L2Util'.static.Inst().showGfxScreenMessage(GetSystemMessage(6188));
		return;
	}
	packet.cDay = clickedDay;
	if(! class'UIPacket'.static.Encode_C_EX_VIP_ATTENDANCE_REWARD(stream, packet))
	{
		return;
	}
	class'UIPacket'.static.RequestUIPacket(class'UIPacket'.const.C_EX_VIP_ATTENDANCE_REWARD, stream);	
}

private function Rt_S_EX_VIP_ATTENDANCE_REWARD()
{
	local UIPacket._S_EX_VIP_ATTENDANCE_REWARD packet;

	if(! class'UIPacket'.static.Decode_S_EX_VIP_ATTENDANCE_REWARD(packet))
	{
		return;
	}
	if(packet.bResult == 1)
	{
		SetRewardDayPlay();
	}	
}

private function SetRewardDayPlay()
{
	local int maxNum;

	attendCheckSlots[packet_attendanceList.cRewardDay]._SetRewardDayPlay();
	maxNum = (clickedDay - packet_attendanceList.cRewardDay) - 1;
	if(maxNum > 0)
	{
		tObjectReward._maxCount = maxNum;
		tObjectReward._Reset();
	}
	packet_attendanceList.cRewardDay = clickedDay;	
}

private function Rt_S_EX_VIP_ATTENDANCE_NOTIFY()
{
	if(! m_hOwnerWnd.IsShowWindow())
	{
		return;
	}
	tObject._Stop();
	attendCheckSlots[packet_attendanceList.cAttendanceDay]._SetAttendDay();
	attendCheckSlots[packet_attendanceList.cAttendanceDay]._SetHighLight();
	Rq_C_EX_VIP_ATTENDANCE_LIST();	
}

private function HandleOnTimer(int Cnt)
{
	attendCheckSlots[packet_attendanceList.cAttendanceDay]._SetRemainTime(tObject._maxCount - Cnt);	
}

private function HandleOnTimerReward(int Cnt)
{
	local int rewardDay;

	rewardDay = (packet_attendanceList.cRewardDay - tObjectReward._maxCount) + Cnt;
	attendCheckSlots[rewardDay]._SetRewardDayPlay();	
}

private function InitFlags()
{
	bInitSlot = false;
	bAttendToday = false;	
}

private function InitClientAttendanceDatas()
{
	local int followcost, costIndex;
	local string o_Period;
	local array<int> o_FollowCosts;
	local int i, rollBookDay;

	API_GetAttendanceData(followItemID, o_Period, o_FollowCosts);
	rollBookDay = Min(TOTAL_ATENDANCE_Number, packet_attendanceList.cRollBookDay);
	
	for(i = packet_attendanceList.cFollowBaseDay; i < rollBookDay; i++)
	{
		costIndex = Min(i - packet_attendanceList.cFollowBaseDay, o_FollowCosts.Length - 1);
		followcost = o_FollowCosts[costIndex];
		attendCheckSlots[i]._SetPay(followItemID, followcost);
	}	
}

private function InitPeriodString()
{
	local string o_Period;
	local array<int> o_FollowCosts;

	API_GetAttendanceData(followItemID, o_Period, o_FollowCosts);
	GetTextBoxHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".EventPeriod00_Txt").SetText((GetSystemString(14436) @ ":") @ o_Period);	
}

private function InitSlots()
{
	local int i;

	attendCheckSlots.Length = TOTAL_ATENDANCE_Number;

	for(i = 0; i < TOTAL_ATENDANCE_Number; i++)
	{
		attendCheckSlots[i] = new class'AttendCheckSlot';
		attendCheckSlots[i]._Init(GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".DaySlot_" $ string(i)));
		attendCheckSlots[i]._SetDayTexture(i + 1);
	}	
}

private function InitSlotItems()
{
	local int i;

	if(bInitSlot)
	{
		return;
	}
	
	for(i = 0; i < TOTAL_ATENDANCE_Number; i++)
	{
		attendCheckSlots[i]._SetItemInfoControl(packet_attendanceList.RewardItems[i].nClassID, packet_attendanceList.RewardItems[i].nAmount);
		attendCheckSlots[i]._SetSlotHighlightControl(packet_attendanceList.RewardItems[i].bHighlight == 1);
	}
	bInitSlot = true;	
}

private function InitTimer()
{
	tObject = class'L2UITimer'.static.Inst()._AddNewTimerObject(1000, 0);
	tObject._DelegateOnTime = HandleOnTimer;
	tObject._Stop();
	tObjectReward = class'L2UITimer'.static.Inst()._AddNewTimerObject(150, 0);
	tObjectReward._DelegateOnTime = HandleOnTimerReward;
	tObjectReward._Stop();	
}

private function InitAssets()
{
	local WindowHandle buyBtnWnd;

	buyBtnWnd = GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".Buy_BtnUIControlGroupButtonAsset");
	buyBtnDialogAsset = class'UIControlDialogAssets'.static.InitScript(buyBtnWnd);
	buyBtnDialogAsset.DelegateOnCancel = OnBuyBtnDialogCancel;
	buyBtnDialogAsset.SetDisableWindow(DisableWindow);
	buyBtnDialogAsset.SetUseBuyItem(false);
	buyBtnDialogAsset.SetUseNeedItem(true);
	buyBtnDialogAsset.SetUseNumberInput(false);	
}

private function HandleHelpBtnClicked()
{
	local string strParam;

	ParamAdd(strParam, "FilePath", GetLocalizedL2TextPathNameUC() $ "g_attendance_help001.htm");
	HelpHtmlWnd(GetScript("HelpHtmlWnd")).HandleShowHelp(strParam);	
}

function _Rq_C_EX_VIP_ATTENDANCE_LIST()
{
	Rq_C_EX_VIP_ATTENDANCE_LIST();	
}

private function bool IsLevelCondition()
{
	local UserInfo uInfo;

	if(! GetPlayerInfo(uInfo))
	{
		return false;
	}
	return uInfo.nLevel >= packet_attendanceList.nMinimumLevel;	
}

private function bool MakeRewardItem(int Day, ItemInfo iInfo, out RichListCtrlRowData oRowData)
{
	local RichListCtrlRowData rowData;

	rowData.cellDataList.Length = 3;
	addRichListCtrlString(rowData.cellDataList[0].drawitems, MakeFullSystemMsg(GetSystemMessage(3418), string(Day + 1)), getInstanceL2Util().BrightWhite, false, 5, 0);
	addRichListCtrlTexture(rowData.cellDataList[1].drawitems, "L2UI_ct1.ItemWindow.ItemWindow_df_slotbox_2x2", 36, 36, 0, 1);
	AddRichListCtrlItem(rowData.cellDataList[1].drawitems, iInfo, 32, 32, -34, 1);
	addRichListCtrlString(rowData.cellDataList[1].drawitems, GetItemNameAll(iInfo, true), getInstanceL2Util().BrightWhite, false, 5, 9);
	addRichListCtrlString(rowData.cellDataList[1].drawitems, iInfo.AdditionalName, getInstanceL2Util().Yellow03, false, 5, 0);
	addRichListCtrlString(rowData.cellDataList[2].drawitems, "x" $ MakeCostStringINT64(iInfo.ItemNum), getInstanceL2Util().White, false, 5);
	oRowData = rowData;
	return true;
}
