//================================================================================
// TimeZoneWnd.
//================================================================================
class TimeZoneWnd extends UICommonAPI;

const TIMER_CLICK = 99902;
const TIMER_DELAYC = 3000;

struct RestrictFieldInfo
{
	var int ResetCycle;
	var int FieldId;
	var int MinLevel;
	var int MaxLevel;
	var int RemainTimeBase;
	var array<int> ItemIDs;
	var array<INT64> ItemCounts;
	var int RemainTime;
	var int RemainTimeMax;
	var int RemainRefillTime;
	var int RefillTimeMax;
	var bool bFieldActivated;
	var int bUserBound;
	var int bCanReEnter;
	var int nIsInZonePCCafeUserOnly;
	var int bWorldInZone;
	var bool CanUseEntranceTicket;
	var int EnteranceCount;
};

var WindowHandle Me;
var WindowHandle ScrollAreaWnd;
var WindowHandle disableWnd;
var WindowHandle UIControlDialogAsset;
var WindowHandle UIControlGroupButtonAsset;
var TextBoxHandle Noti_text;
var ButtonHandle ReFresh_btn;
var ButtonHandle Close_Btn;
var L2Util util;
var int currentSelectedFieldID;
var bool bResultFieldChargeRefresh;
var int nIsPCCafeUser;
var int lastClickPlusButtonPosition;
var UserInfo myInfo;
var bool bShowFirst;
var bool bFirstSetting;
var array<RestrictFieldInfo> RestrictFieldInfos;
var array<RestrictFieldInfo> RestrictFieldInfosWorld;
var INT64 beforeAdenaCount;
var UIControlTilelist scrollTileList;
var UIControlGroupButtonAssets TopGroupButtonAsset;
var UIControlNeedItemList needItemListRenderer0Script;
var UIControlNeedItemList needItemListRenderer1Script;
var UIControlNeedItemList needItemListRenderer2Script;
var UIControlNeedItemList needItemListRenderer3Script;
var UIControlPageNavi pageNavi;
var int nShowTileIndex;

event OnRegisterEvent()
{
	RegisterEvent(EV_TimeRestrictFieldListStart);
	RegisterEvent(EV_TimeRestrictFieldInfo);
	RegisterEvent(EV_TimeRestrictFieldListEnd);
	RegisterEvent(EV_TimeRestrictFieldChargeResult);
	RegisterEvent(EV_ProtocolBegin + class'UIPacket'.const.S_EX_TIME_RESTRICT_FIELD_ENTER_INFO);
	RegisterEvent(EV_ProtocolBegin + class'UIPacket'.const.S_EX_TIME_RESTRICT_FIELD_HOST_USER_ENTER);
	RegisterEvent(EV_ProtocolBegin + class'UIPacket'.const.S_EX_TIME_RESTRICT_FIELD_HOST_USER_LEAVE);
	RegisterEvent(EV_TimeRestrictFieldUserAlarm);
	RegisterEvent(EV_TimeRestrictFieldEnterResult);
	RegisterEvent(EV_TimeRestrictFieldExit);
	RegisterEvent(EV_UpdateUserInfo);
	RegisterEvent(EV_AdenaInvenCount);
	RegisterEvent(EV_GameStart);
}

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

function Initialize()
{
	Me = GetWindowHandle("TimeZoneWnd");
	ScrollAreaWnd = GetWindowHandle("TimeZoneWnd.ScrollAreaWnd");
	disableWnd = GetWindowHandle("TimeZoneWnd.DisableWnd");
	UIControlDialogAsset = GetWindowHandle("TimeZoneWnd.DisableWnd.UIControlDialogAsset");
	UIControlGroupButtonAsset = GetWindowHandle("TimeZoneWnd.UIControlGroupButtonAsset");
	Noti_text = GetTextBoxHandle("TimeZoneWnd.Noti_text");
	ReFresh_btn = GetButtonHandle("TimeZoneWnd.Refresh_Btn");
	Close_Btn = GetButtonHandle("TimeZoneWnd.Close_Btn");
	util = L2Util(GetScript("L2Util"));
	scrollTileList = class'UIControlTilelist'.static.InitScript(GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".ScrollAreaWnd"), 4, 1);
	scrollTileList.DelegateOnItemRenderer = HandleDelegateOnItemRenderer;
	scrollTileList.DelegateOnClick = HandleDelegateOnClick;
	scrollTileList.DelegateOnRendererClick = HandleDelegateOnClickItemRenderer;
	scrollTileList.DelegateOnScroll = HandleOnScroll;
	scrollTileList._SetUsePage(true);
	scrollTileList._SetUseSelect(true);
	scrollTileList._SetUseOver(true);
	InitPageNavi();
	InitNeedItem();
	initGroupButton();
	initDialogAssetes();
}

function InitPageNavi()
{
	local WindowHandle pageNaviControl;

	pageNaviControl = GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".PageNavi_Control");
	pageNaviControl.SetScript("UIControlPageNavi");
	pageNavi = UIControlPageNavi(pageNaviControl.GetScript());
	pageNavi.Init(m_hOwnerWnd.m_WindowNameWithFullPath $ ".PageNavi_Control");
	pageNavi.DelegateOnClickButton = pageNaviButtonClicked;
	pageNavi.SetNoUseGo(true);	
}

function pageNaviButtonClicked(string strName)
{
	Debug("strName:" @ strName);
	if(strName == "ControlNavi_PrevBtn")
	{
		OnPrev_BtnClick();		
	}
	else if(strName == "ControlNavi_NextBtn")
	{
		OnNext_BtnClick();
	}	
}

function initGroupButton()
{
	TopGroupButtonAsset = class'UIControlGroupButtonAssets'.static._InitScript(GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".UIControlGroupButtonAsset"));
	TopGroupButtonAsset._SetStartInfo("L2UI_NewTex.WindowTab.FlatBrown_Tab_Left_Unselected", "L2UI_NewTex.WindowTab.FlatBrown_Tab_Left_Selected", "L2UI_NewTex.WindowTab.FlatBrown_Tab_Left_Over", true);
	TopGroupButtonAsset._GetGroupButtonsInstance().DelegateOnClickButton = DelegateOnClickButton;
	TopGroupButtonAsset._setDelayTime(500);	
}

function DelegateOnClickButton(string parentWndName, string strName, int Index)
{
	ItemListInfoEnd(true);	
}

function InitNeedItem()
{
	needItemListRenderer0Script = new class'UIControlNeedItemList';
	needItemListRenderer0Script.SetRichListControler(GetRichListCtrlHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".ScrollAreaWnd.ScrollArea.itemRenderer00.TimeZoneCostIcon_RichList"));
	needItemListRenderer1Script = new class'UIControlNeedItemList';
	needItemListRenderer1Script.SetRichListControler(GetRichListCtrlHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".ScrollAreaWnd.ScrollArea.itemRenderer01.TimeZoneCostIcon_RichList"));
	needItemListRenderer2Script = new class'UIControlNeedItemList';
	needItemListRenderer2Script.SetRichListControler(GetRichListCtrlHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".ScrollAreaWnd.ScrollArea.itemRenderer02.TimeZoneCostIcon_RichList"));
	needItemListRenderer3Script = new class'UIControlNeedItemList';
	needItemListRenderer3Script.SetRichListControler(GetRichListCtrlHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".ScrollAreaWnd.ScrollArea.itemRenderer03.TimeZoneCostIcon_RichList"));	
}

function initDialogAssetes()
{
	local UIControlDialogAssets popupExpandScript;

	popupExpandScript = class'UIControlDialogAssets'.static.InitScript(GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".UIControlDialogAsset"));
	disableWnd = GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".DisableWnd");
	popupExpandScript.SetDisableWindow(disableWnd);
	class'UIAPI_WINDOW'.static.SetAlwaysOnTop(m_hOwnerWnd.m_WindowNameWithFullPath $ ".DisableWnd", false);	
}

function UIControlDialogAssets GetDialogAssetScript()
{
	local WindowHandle poopExpandWnd;

	poopExpandWnd = GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".UIControlDialogAsset");
	return UIControlDialogAssets(poopExpandWnd.GetScript());	
}

function ShowDialogAskEnter(RestrictFieldInfo fieldInfo)
{
	local int i;
	local TimeRestrictFieldUIData fieldUIData;
	local string addMessage;

	DialogHide();
	API_GetTimeRestrictFieldInfo(fieldInfo.FieldId, fieldUIData);
	currentSelectedFieldID = fieldInfo.FieldId;
	if(fieldUIData.Type != "timezone")
	{
		addMessage = "<br><br>" $ GetSystemString(13500);
		addMessage = addMessage $ "<br>" $ GetSystemString(13035);		
	}
	else
	{
		addMessage = "<br><br>" $ GetSystemString(14170);
		addMessage = addMessage $ "<br>" $ GetSystemString(14171);
	}
	GetDialogAssetScript().SetDialogDesc(htmlSetHtmlStart(htmlAddText(MakeFullSystemMsg(GetSystemMessage(13009), fieldUIData.FieldName), "hs10", getColorHexString(GTColor().White)) $ htmlAddText(addMessage, "", getColorHexString(GTColor().Green))),,, true);
	GetDialogAssetScript().SetUseNeedItem(true);
	GetDialogAssetScript().StartNeedItemList(1);

	for(i = 0; i < fieldInfo.ItemIDs.Length; i++)
	{
		if(fieldInfo.ItemIDs[i] == MSIT_PCCAFE_POINT)
		{
			GetDialogAssetScript().AddNeedPoint(GetSystemString(1277), GetPcCafeItemIconPackageName(), fieldInfo.ItemCounts[i], getInstanceUIData().getCurrentPcCafePoint());
			// [Explicit Continue]
			continue;
		}
		GetDialogAssetScript().AddNeedItemClassID(fieldInfo.ItemIDs[i], fieldInfo.ItemCounts[i]);
	}
	if(fieldInfo.ItemIDs.Length == 0)
	{
		GetDialogAssetScript().SetItemNum(0);
		GetDialogAssetScript().OKButton.EnableWindow();		
	}
	else
	{
		GetDialogAssetScript().SetItemNum(1);
	}
	GetDialogAssetScript().Show();
	GetDialogAssetScript().DelegateOnClickBuy = onClickDialog;
	GetDialogAssetScript().DelegateOnCancel = OnClickCancelDialog;
}

function ShowTimeZoneExitDialog()
{
	local WindowHandle disablwWnd;

	DialogHide();
	disablwWnd = GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".DisableWnd");
	disablwWnd.ShowWindow();
	disablwWnd.SetFocus();
	DialogShow(DialogModalType_Modalless, DialogType_OKCancel, GetSystemMessage(13704), string(self));
	class'DialogBox'.static.Inst().AnchorToOwner(0, 0);
	class'DialogBox'.static.Inst().DelegateOnOK = OnTimeZoneExitDialogConfirm;
	class'DialogBox'.static.Inst().DelegateOnHide = OnTimeZoneExitDialogHide;
	class'DialogBox'.static.Inst().SetDefaultAction(EDefaultCancel);
}

event OnClickTimeZoneExit()
{
	ShowTimeZoneExitDialog();
}

event OnTimeZoneExitDialogConfirm()
{
	Rq_C_EX_TIME_RESTRICT_FIELD_USER_LEAVE();
}

event OnTimeZoneExitDialogHide()
{
	GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".DisableWnd").HideWindow();
}

function onClickDialog()
{
	GetDialogAssetScript().Hide();
	if(IsPlayerOnWorldRaidServer())
	{
		API_C_EX_TIME_RESTRICT_FIELD_HOST_USER_LEAVE(NoticeHUD(GetScript("NoticeHud")).getTimeZoneCurrentFieldID(), currentSelectedFieldID);		
	}
	else
	{
		API_RequestEnterTimeRestrictField(currentSelectedFieldID);
	}
}

function OnClickCancelDialog()
{
	GetDialogAssetScript().Hide();
}

event OnShow()
{
	bShowFirst = true;
	beforeAdenaCount = GetAdena();
	GetPlayerInfo(myInfo);
	if(disableWnd.IsShowWindow())
	{
		disableWnd.HideWindow();
	}
	API_RequestTimeRestrictFieldList();
	SideBar(GetScript("SideBar")).ToggleByWindowName("TimeZoneWnd", true);
	lastClickPlusButtonPosition = -1;
	Me.SetFocus();
}

event OnHide()
{
	currentSelectedFieldID = -1;
	nShowTileIndex = -1;
	scrollTileList._SetSelect(-1);
	class'UIAPI_WINDOW'.static.HideWindow("TimeZoneSubWnd");
	DialogHide();
	SideBar(GetScript("SideBar")).ToggleByWindowName("TimeZoneWnd", false);	
}

function setCategoryTab()
{
	if(IsAdenServer())
	{
		TopGroupButtonAsset._GetGroupButtonsInstance()._setButtonText(0, GetSystemString(441));
		TopGroupButtonAsset._GetGroupButtonsInstance()._setButtonText(1, GetSystemString(14093));
		TopGroupButtonAsset._GetGroupButtonsInstance()._setButtonValue(0, 0);
		TopGroupButtonAsset._GetGroupButtonsInstance()._setButtonValue(1, 1);
		TopGroupButtonAsset._GetGroupButtonsInstance()._setButtonTexture(1, "L2UI_NewTex.WindowTab.FlatBlue_Tab_Right_Unselected", "L2UI_NewTex.WindowTab.FlatBlue_Tab_Right_Selected", "L2UI_NewTex.WindowTab.FlatBlue_Tab_Right_Unselected_Over");
		TopGroupButtonAsset._GetGroupButtonsInstance()._setShowButtonNum(2);
		TopGroupButtonAsset._GetGroupButtonsInstance()._setAutoWidth(1060, 0);
		TopGroupButtonAsset._GetGroupButtonsInstance()._setTopOrder(0);		
	}
	else
	{
		TopGroupButtonAsset._GetGroupButtonsInstance()._setButtonText(0, GetSystemString(2611));
		TopGroupButtonAsset._GetGroupButtonsInstance()._setButtonValue(0, 0);
		TopGroupButtonAsset._GetGroupButtonsInstance()._setButtonTexture(0, "L2UI_NewTex.WindowTab.FlatBrown_Tab_One_Unselected", "L2UI_NewTex.WindowTab.FlatBrown_Tab_One_Selected", "L2UI_NewTex.WindowTab.FlatBrown_Tab_One_Over");
		TopGroupButtonAsset._GetGroupButtonsInstance()._setShowButtonNum(1);
		TopGroupButtonAsset._GetGroupButtonsInstance()._setAutoWidth(1058, 0);
		TopGroupButtonAsset._GetGroupButtonsInstance()._setTopOrder(0);
	}
}

function OnClickButton(string Name)
{
	switch(Name)
	{
		case "Refresh_Btn":
			OnReFresh_btnClick();
			break;
		case "Close_Btn":
			OnClose_BtnClick();
			break;
		case "Prev_Btn":
			OnPrev_BtnClick(true);
			break;
		case "Next_Btn":
			OnNext_BtnClick(true);
			break;
	}
}

function OnReFresh_btnClick()
{
	API_RequestTimeRestrictFieldList();	
}

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

function OnPrev_BtnClick(optional bool bUpdate)
{
	scrollTileList._PrevPage();
	prevNextButtonState();
}

function OnNext_BtnClick(optional bool bUpdate)
{
	scrollTileList._NextPage();
	prevNextButtonState();
}

function prevNextButtonState()
{
	local int Page;

	Page = scrollTileList._Page();
	if(Page <= 0)
	{
		GetButtonHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".Prev_Btn").DisableWindow();		
	}
	else
	{
		GetButtonHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".Prev_Btn").EnableWindow();
	}
	if(Page >= scrollTileList._PageMax())
	{
		GetButtonHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".Next_Btn").DisableWindow();		
	}
	else
	{
		GetButtonHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".Next_Btn").EnableWindow();
	}	
}

event OnEvent(int Event_ID, string param)
{
	switch (Event_ID)
	{
		case EV_TimeRestrictFieldListStart:
			ClearAll();
			break;
		case EV_TimeRestrictFieldInfo:
			HandleItemList(param);
			break;
		case EV_TimeRestrictFieldListEnd:
			ItemListInfoEnd();
			break;
		case EV_TimeRestrictFieldChargeResult:
			HandleResultFieldCharge(param);
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_TIME_RESTRICT_FIELD_ENTER_INFO):
			HandleTimeRestrictFieldEnterInfo();
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_TIME_RESTRICT_FIELD_HOST_USER_LEAVE):
			Handle_S_EX_TIME_RESTRICT_FIELD_HOST_USER_LEAVE();
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_TIME_RESTRICT_FIELD_HOST_USER_ENTER):
			Handle_S_EX_TIME_RESTRICT_FIELD_HOST_USER_ENTER();
			break;
		case EV_TimeRestrictFieldUserAlarm:
			HandleResultFieldAlarm(param);
			break;
		case EV_TimeRestrictFieldEnterResult:
			HandleEnterResult(param);
			break;
		case EV_TimeRestrictFieldExit:
			Me.HideWindow();
			break;
		case EV_AdenaInvenCount:
			if(Me.IsShowWindow())
			{
				class'L2UITimer'.static.Inst()._AddTimer(1)._DelegateOnTime = OnTimeAdenCount;
			}
			break;
		case EV_UpdateUserInfo:
			if (Me.IsShowWindow() && getInstanceUIData().isLevelUP() || getInstanceUIData().IsLevelDown())
			{
				HandleUserInfo();
			}
			break;
		case EV_GameStart:
			setCategoryTab();
			nShowTileIndex = -1;
			bFirstSetting = true;
			break;
	}
}

function OnTimeAdenCount(int Count)
{
	if(GetAdena() != beforeAdenaCount)
	{
		bResultFieldChargeRefresh = true;
		scrollTileList._Refresh();
		bResultFieldChargeRefresh = false;
	}
	beforeAdenaCount = GetAdena();	
}

function HandleUserInfo()
{
	GetPlayerInfo(myInfo);
	GetDialogAssetScript().Hide();
	API_RequestTimeRestrictFieldList();
	scrollTileList._Refresh();	
}

function updateFieldRemainTime(int nFieldID, int nRemainTime)
{
	local int i;

	for(i = 0; i < RestrictFieldInfos.Length; i++)
	{
		if(RestrictFieldInfos[i].FieldId == nFieldID)
		{
			RestrictFieldInfos[i].RemainTime = nRemainTime;
			return;
		}
	}

	for(i = 0; i < RestrictFieldInfosWorld.Length; i++)
	{
		if(RestrictFieldInfosWorld[i].FieldId == nFieldID)
		{
			RestrictFieldInfosWorld[i].RemainTime = nRemainTime;
			return;
		}
	}	
}

function int getIndexByFieldID(int nFieldID)
{
	local int i;

	if(TopGroupButtonAsset._GetGroupButtonsInstance()._getSelectButtonIndex() == 0)
	{
		for(i = 0; i < RestrictFieldInfos.Length; i++)
		{
			if(RestrictFieldInfos[i].FieldId == nFieldID)
			{
				return i;
			}
		}		
	}
	else
	{
		for(i = 0; i < RestrictFieldInfosWorld.Length; i++)
		{
			if(RestrictFieldInfosWorld[i].FieldId == nFieldID)
			{
				return i;
			}
		}
	}
	return -1;	
}

function setFindCurrentTop(int nFieldID)
{
	local int i;

	for(i = 0; i < RestrictFieldInfos.Length; i++)
	{
		if(RestrictFieldInfos[i].FieldId == nFieldID)
		{
			TopGroupButtonAsset._GetGroupButtonsInstance()._setTopOrder(0, true);
			return;
		}
	}

	for(i = 0; i < RestrictFieldInfosWorld.Length; i++)
	{
		if(RestrictFieldInfosWorld[i].FieldId == nFieldID)
		{
			TopGroupButtonAsset._GetGroupButtonsInstance()._setTopOrder(1, true);
			return;
		}
	}
}

function HandleResultFieldAlarm(string param)
{
	local int FieldId, RemainTime;

	ParseInt(param, "FieldID", FieldId);
	ParseInt(param, "RemainTime", RemainTime);
	Debug("--> HandleResultFieldAlarm : " @ param);
	updateFieldRemainTime(FieldId, RemainTime);
	scrollTileList._Refresh();	
}

function HandleEnterResult(string param)
{
	local int bEnterSuccess;

	ParseInt(param, "bEnterSuccess", bEnterSuccess);
	if(bEnterSuccess == 1)
	{
		Me.HideWindow();
	}
	else
	{
		GetDialogAssetScript().Hide();
	}
}

function updateFieldRefillTime(int nFieldID, int nRemainTime, int nResultRefillTime)
{
	local int i;

	for(i = 0; i < RestrictFieldInfos.Length; i++)
	{
		if(RestrictFieldInfos[i].FieldId == nFieldID)
		{
			RestrictFieldInfos[i].RemainTime = nRemainTime;
			RestrictFieldInfos[i].RemainRefillTime = nResultRefillTime;
			return;
		}
	}

	for(i = 0; i < RestrictFieldInfosWorld.Length; i++)
	{
		if(RestrictFieldInfosWorld[i].FieldId == nFieldID)
		{
			RestrictFieldInfosWorld[i].RemainTime = nRemainTime;
			RestrictFieldInfosWorld[i].RemainRefillTime = nResultRefillTime;
			return;
		}
	}	
}

function updateFieldEnterInfo(int nFieldID, bool CanUseEntranceTicket, int EnteranceCount)
{
	local int i;

	for(i = 0; i < RestrictFieldInfos.Length; i++)
	{
		if(RestrictFieldInfos[i].FieldId == nFieldID)
		{
			RestrictFieldInfos[i].CanUseEntranceTicket = CanUseEntranceTicket;
			RestrictFieldInfos[i].EnteranceCount = EnteranceCount;
			return;
		}
	}

	for(i = 0; i < RestrictFieldInfosWorld.Length; i++)
	{
		if(RestrictFieldInfosWorld[i].FieldId == nFieldID)
		{
			RestrictFieldInfosWorld[i].CanUseEntranceTicket = CanUseEntranceTicket;
			RestrictFieldInfosWorld[i].EnteranceCount = EnteranceCount;
			return;
		}
	}	
}

function HandleResultFieldCharge(string param)
{
	local int FieldId, RemainTime, ResultRefillTime, resultChargeTime;
	local TimeRestrictFieldUIData fieldUIData;
	local string gfxScreenMsg;

	ParseInt(param, "FieldID", FieldId);
	ParseInt(param, "RemainTime", RemainTime);
	ParseInt(param, "ResultRefillTime", ResultRefillTime);
	ParseInt(param, "ResultChargeTime", resultChargeTime);
	updateFieldRefillTime(FieldId, RemainTime, ResultRefillTime);
	bResultFieldChargeRefresh = true;
	scrollTileList._Refresh();
	bResultFieldChargeRefresh = false;
	API_GetTimeRestrictFieldInfo(FieldId, fieldUIData);
	gfxScreenMsg = MakeFullSystemMsg(GetSystemMessage(13008), fieldUIData.FieldName, string(resultChargeTime / 60));
	getInstanceL2Util().showGfxScreenMessage(gfxScreenMsg);
	Debug("--> HandleResultFieldCharge" @ param @ string(FieldId) @ string(RemainTime) @ string(ResultRefillTime) @ string(resultChargeTime));	
}

function Handle_S_EX_TIME_RESTRICT_FIELD_HOST_USER_LEAVE()
{
	local UIPacket._S_EX_TIME_RESTRICT_FIELD_HOST_USER_LEAVE packet;

	if(! class'UIPacket'.static.Decode_S_EX_TIME_RESTRICT_FIELD_HOST_USER_LEAVE(packet))
	{
		return;
	}
	Debug("--> Decode_S_EX_TIME_RESTRICT_FIELD_HOST_USER_LEAVE");
	Debug("packet.nResult" @ string(packet.nResult));
	Debug("packet.nLeaveFieldID" @ string(packet.nLeaveFieldID));
	Debug("packet.nNextEnterFieldID" @ string(packet.nNextEnterFieldID));
	if(packet.nResult > 0)
	{
		API_C_EX_TIME_RESTRICT_FIELD_HOST_USER_ENTER(packet.nNextEnterFieldID);
	}	
}

function Handle_S_EX_TIME_RESTRICT_FIELD_HOST_USER_ENTER()
{
	local UIPacket._S_EX_TIME_RESTRICT_FIELD_HOST_USER_ENTER packet;

	if(! class'UIPacket'.static.Decode_S_EX_TIME_RESTRICT_FIELD_HOST_USER_ENTER(packet))
	{
		return;
	}
	Debug("--> Decode_S_EX_TIME_RESTRICT_FIELD_HOST_USER_ENTER");
	Debug("packet.nResult" @ string(packet.nResult));
	Debug("packet.nEnteredFieldID" @ string(packet.nEnteredFieldID));
	if(packet.nResult > 0)
	{
	}
	Me.HideWindow();	
}

function HandleTimeRestrictFieldEnterInfo()
{
	local UIPacket._S_EX_TIME_RESTRICT_FIELD_ENTER_INFO packet;
	local int i;

	if(! class'UIPacket'.static.Decode_S_EX_TIME_RESTRICT_FIELD_ENTER_INFO(packet))
	{
		return;
	}

	for(i = 0; i < packet.vEnterGroupFieldIDList.Length; i++)
	{
		Debug("--> HandleTimeRestrictFieldEnterInfo FieldID: " @ string(packet.vEnterGroupFieldIDList[i]));
		updateFieldEnterInfo(packet.vEnterGroupFieldIDList[i], numToBool(packet.bCanUseEntranceTicket), packet.nEntranceCount);
	}
	bResultFieldChargeRefresh = true;
	scrollTileList._Refresh();
	bResultFieldChargeRefresh = false;
	getInstanceL2Util().showGfxScreenMessage(GetSystemMessage(13694));
	Debug("--> HandleTimeRestrictFieldEnterInfo" @ string(packet.bCanUseEntranceTicket) @ string(packet.nEntranceCount));
}

function ClearAll()
{
	RestrictFieldInfos.Length = 0;
	RestrictFieldInfosWorld.Length = 0;
	GetPlayerInfo(myInfo);
}

function HandleItemList(string param)
{
	local RestrictFieldInfo fieldInfo;
	local int i, tmpBFieldActivated, Count, nCanUseEntranceTicket;

	ParseInt(param, "ResetCycle", fieldInfo.ResetCycle);
	ParseInt(param, "FieldId", fieldInfo.FieldId);
	ParseInt(param, "MinLevel", fieldInfo.MinLevel);
	ParseInt(param, "MaxLevel", fieldInfo.MaxLevel);
	ParseInt(param, "RemainTimeBase", fieldInfo.RemainTimeBase);
	ParseInt(param, "RequiredItemCnt", Count);
	ParseInt(param, "bIsInZonePCCafeUserOnly", fieldInfo.nIsInZonePCCafeUserOnly);
	ParseInt(param, "bIsPCCafeUser", nIsPCCafeUser);
	ParseInt(param, "bWorldInZone", fieldInfo.bWorldInZone);
	fieldInfo.ItemIDs.Length = Count;
	fieldInfo.ItemCounts.Length = Count;

	for (i = 0; i < Count; i++)
	{
		ParseInt(param, "ItemID" $ string(i), fieldInfo.ItemIDs[i]);
		ParseINT64(param, "ItemAmount" $ string(i), fieldInfo.ItemCounts[i]);
	}

	ParseInt(param, "RemainTime", fieldInfo.RemainTime);
	ParseInt(param, "RemainTimeMax", fieldInfo.RemainTimeMax);
	ParseInt(param, "RemainRefillTime", fieldInfo.RemainRefillTime);
	ParseInt(param, "RefillTimeMax", fieldInfo.RefillTimeMax);
	ParseInt(param, "FieldActivated", tmpBFieldActivated);
	ParseInt(param, "bUserBound", fieldInfo.bUserBound);
	ParseInt(param, "bCanReEnter", fieldInfo.bCanReEnter);
	ParseInt(param, "bCanUseEntranceTicket", nCanUseEntranceTicket);
	ParseInt(param, "EntranceCount", fieldInfo.EnteranceCount);
	fieldInfo.CanUseEntranceTicket = numToBool(nCanUseEntranceTicket);
	fieldInfo.bFieldActivated = tmpBFieldActivated > 0;
	GetPlayerInfo(myInfo);
	if(fieldInfo.bWorldInZone > 0)
	{
		RestrictFieldInfosWorld.Length = RestrictFieldInfosWorld.Length + 1;
		RestrictFieldInfosWorld[RestrictFieldInfosWorld.Length - 1] = fieldInfo;		
	}
	else if(myInfo.nLevel <= fieldInfo.MaxLevel)
	{
		RestrictFieldInfos.Length = RestrictFieldInfos.Length + 1;
		RestrictFieldInfos[RestrictFieldInfos.Length - 1] = fieldInfo;
	}
}

function ItemListInfoEnd(optional bool bDirectRun)
{
	RestrictFieldInfos.Sort(SortCompare);
	RestrictFieldInfosWorld.Sort(SortCompare);
	GetPlayerInfo(myInfo);
	if(bShowFirst && bDirectRun == false)
	{
		setFindCurrentTop(NoticeHUD(GetScript("NoticeHud")).getTimeZoneCurrentFieldID());
	}
	if(TopGroupButtonAsset._GetGroupButtonsInstance()._getSelectButtonIndex() == 0)
	{
		scrollTileList._SetTileListItemNumTotal(RestrictFieldInfos.Length);
		if(bDirectRun)
		{
			nShowTileIndex = -1;			
		}
		else
		{
			nShowTileIndex = getIndexByFieldID(NoticeHUD(GetScript("NoticeHud")).getTimeZoneCurrentFieldID());
		}
		pageNavi.SetTotalPage(scrollTileList._PageMax() + 1);
		scrollTileList._SetSelect(nShowTileIndex, true);
		if(bShowFirst == false || bFirstSetting)
		{
			pageNavi.Go(1);
		}
		pageNavi.BtnCheck();		
	}
	else
	{
		scrollTileList._SetTileListItemNumTotal(RestrictFieldInfosWorld.Length);
		if(bDirectRun)
		{
			nShowTileIndex = -1;			
		}
		else
		{
			nShowTileIndex = getIndexByFieldID(NoticeHUD(GetScript("NoticeHud")).getTimeZoneCurrentFieldID());
		}
		pageNavi.SetTotalPage(scrollTileList._PageMax() + 1);
		scrollTileList._SetSelect(nShowTileIndex, true);
		if(bShowFirst == false || bFirstSetting)
		{
			pageNavi.Go(1);
		}
		pageNavi.BtnCheck();
	}
	bShowFirst = false;
	bFirstSetting = false;	
}

function HandleDelegateOnItemRenderer(string itemRendererID, int rendererIndex, int Position)
{
	local int i, diceIndex, currentZoneId;
	local TimeRestrictFieldUIData fieldUIData;
	local UIControlNeedItemList currentNeedItemListRendererScript;
	local array<RestrictFieldInfo> currentRestrictFieldInfos;
	local bool canEnter, canEnterNeedItem;
	local ButtonHandle enterBtn, ExitBtn;

	enterBtn = GetButtonHandle(itemRendererID $ ".TimeZoneEnter_Btn");
	ExitBtn = GetButtonHandle(itemRendererID $ ".TimeZoneExit_Btn");
	currentZoneId = NoticeHUD(GetScript("NoticeHud")).getTimeZoneCurrentFieldID();
	if(class'UIAPI_WINDOW'.static.IsShowWindow("TimeZoneWnd") == false)
	{
		return;
	}
	if(class'UIAPI_WINDOW'.static.IsShowWindow("TimeZoneSubWnd") && bResultFieldChargeRefresh == false)
	{
		class'UIAPI_WINDOW'.static.HideWindow("TimeZoneSubWnd");
	}
	diceIndex = int(float(Position) % float(scrollTileList._GetItemRendererNum()));
	canEnter = true;
	if(Position >= scrollTileList._GetItemNumTotal())
	{
		GetWindowHandle(itemRendererID).HideWindow();		
	}
	else
	{
		GetWindowHandle(itemRendererID).ShowWindow();
		if(TopGroupButtonAsset._GetGroupButtonsInstance()._getSelectButtonIndex() == 0)
		{
			currentRestrictFieldInfos = RestrictFieldInfos;
			enterBtn.SetTexture("L2UI_NewTex.Button.SimpleBtnBrown_DF", "L2UI_NewTex.Button.SimpleBtnBrown_Down", "L2UI_NewTex.Button.SimpleBtnBrown_Over");			
		}
		else
		{
			currentRestrictFieldInfos = RestrictFieldInfosWorld;
			enterBtn.SetTexture("L2UI_NewTex.Button.SimpleBtnBlue_DF", "L2UI_NewTex.Button.SimpleBtnBlue_Down", "L2UI_NewTex.Button.SimpleBtnBlue_Over");
		}
		API_GetTimeRestrictFieldInfo(currentRestrictFieldInfos[Position].FieldId, fieldUIData);
		switch(rendererIndex)
		{
			case 0:
				currentNeedItemListRendererScript = needItemListRenderer0Script;
				break;
			case 1:
				currentNeedItemListRendererScript = needItemListRenderer1Script;
				break;
			case 2:
				currentNeedItemListRendererScript = needItemListRenderer2Script;
				break;
			case 3:
				currentNeedItemListRendererScript = needItemListRenderer3Script;
				break;
		}
		currentNeedItemListRendererScript.StartNeedItemList(1);

		for(i = 0; i < currentRestrictFieldInfos[Position].ItemIDs.Length; i++)
		{
			if(currentRestrictFieldInfos[Position].ItemIDs[i] == MSIT_PCCAFE_POINT)
			{
				currentNeedItemListRendererScript.AddNeedPoint(GetSystemString(1277), GetPcCafeItemIconPackageName(), currentRestrictFieldInfos[Position].ItemCounts[i], getInstanceUIData().getCurrentPcCafePoint());
				// [Explicit Continue]
				continue;
			}
			currentNeedItemListRendererScript.AddNeedItemClassID(currentRestrictFieldInfos[Position].ItemIDs[i], currentRestrictFieldInfos[Position].ItemCounts[i]);
		}
		currentNeedItemListRendererScript.SetBuyNum(1);
		if(fieldUIData.IsEvent)
		{
			GetTextureHandle(itemRendererID $ ".EventRibbon_Tex").ShowWindow();			
		}
		else
		{
			GetTextureHandle(itemRendererID $ ".EventRibbon_Tex").HideWindow();
		}
		switch(fieldUIData.PvpType)
		{
			case 0:
				GetTextBoxHandle(itemRendererID $ ".ZonePlayType_Text").SetText(GetSystemString(14097));
				GetTextBoxHandle(itemRendererID $ ".ZonePlayType_Text").SetTextColor(GTColor().White);
				break;
			case 1:
				GetTextBoxHandle(itemRendererID $ ".ZonePlayType_Text").SetText(GetSystemString(14096));
				GetTextBoxHandle(itemRendererID $ ".ZonePlayType_Text").SetTextColor(GTColor().Red);
				break;
			default:
				GetTextBoxHandle(itemRendererID $ ".ZonePlayType_Text").SetText(GetSystemString(14098));
				GetTextBoxHandle(itemRendererID $ ".ZonePlayType_Text").SetTextColor(GTColor().Green);
				break;
		}
		GetTextureHandle(itemRendererID $ ".ZoneImg_Tex").SetTexture(fieldUIData.FieldImage);
		GetTextureHandle(itemRendererID $ ".ZoneImg_Tex").SetTooltipCustomType(MakeTooltipSimpleText(fieldUIData.Desc, 300));
		canEnter = GetBoolListCondition(currentRestrictFieldInfos[Position].MinLevel, currentRestrictFieldInfos[Position].MaxLevel, currentRestrictFieldInfos[Position].RemainTime, currentRestrictFieldInfos[Position].bFieldActivated, fieldUIData.Type, currentRestrictFieldInfos[Position].bUserBound, currentRestrictFieldInfos[Position].bCanReEnter, currentRestrictFieldInfos[Position].nIsInZonePCCafeUserOnly);
		if(currentNeedItemListRendererScript.NeedItemRichListCtrl.GetRecordCount() == 0)
		{
			canEnterNeedItem = true;			
		}
		else
		{
			canEnterNeedItem = currentNeedItemListRendererScript.GetCanBuy();
		}
		canEnter = canEnter && canEnterNeedItem;
		if(canEnter)
		{
			enterBtn.EnableWindow();
			GetWindowHandle(itemRendererID $ ".TimeZoneDisable_wnd").HideWindow();
			enterBtn.SetButtonName(13033);			
		}
		else
		{
			enterBtn.DisableWindow();
			enterBtn.SetButtonName(14114);
			GetWindowHandle(itemRendererID $ ".TimeZoneDisable_wnd").SetTooltipType("text");
			GetWindowHandle(itemRendererID $ ".TimeZoneDisable_wnd").SetTooltipCustomType(MakeTooltipSimpleText(fieldUIData.Desc, 300));
			if(nIsPCCafeUser == 0 && currentRestrictFieldInfos[Position].nIsInZonePCCafeUserOnly == 1)
			{
				GetWindowHandle(itemRendererID $ ".TimeZoneDisable_wnd").ShowWindow();
				GetTextBoxHandle(itemRendererID $ ".TimeZoneDisable_wnd.DisableDescrip_txt").SetText(GetSystemString(14099));				
			}
			else if(currentRestrictFieldInfos[Position].bFieldActivated == false)
			{
				GetWindowHandle(itemRendererID $ ".TimeZoneDisable_wnd").ShowWindow();
				GetTextBoxHandle(itemRendererID $ ".TimeZoneDisable_wnd.DisableDescrip_txt").SetText(fieldUIData.TimeInfo);
				Debug("fieldUIData.TimeInfo" @ fieldUIData.TimeInfo);				
			}
			else if(myInfo.nLevel < currentRestrictFieldInfos[Position].MinLevel)
			{
				GetWindowHandle(itemRendererID $ ".TimeZoneDisable_wnd").ShowWindow();
				GetTextBoxHandle(itemRendererID $ ".TimeZoneDisable_wnd.DisableDescrip_txt").SetText(MakeFullSystemMsg(GetSystemMessage(6170), string(currentRestrictFieldInfos[Position].MinLevel)));						
			}
			else
			{
				GetWindowHandle(itemRendererID $ ".TimeZoneDisable_wnd").HideWindow();
			}
		}
		switch(currentRestrictFieldInfos[Position].ResetCycle)
		{
			case 0:
				GetTextBoxHandle(itemRendererID $ ".ZoneOpenTitle_Text").SetText(GetSystemString(3580));
				GetTextBoxHandle(itemRendererID $ ".ZoneOpenTitle_Text").SetTextColor(GetColor(255, 161, 161, 255));
				break;
			case 1:
				GetTextBoxHandle(itemRendererID $ ".ZoneOpenTitle_Text").SetText(GetSystemString(3579));
				GetTextBoxHandle(itemRendererID $ ".ZoneOpenTitle_Text").SetTextColor(GetColor(255, 255, 187, 255));
				break;
		}
		GetTextBoxHandle(itemRendererID $ ".ZoneNameTitle_Text").SetText(makeShortStringByPixel(fieldUIData.FieldName, 220, "..", "HS11"));
		GetTextBoxHandle(itemRendererID $ ".ZoneLV_Text").SetText(MakeLevelMinMax(currentRestrictFieldInfos[Position].MinLevel, currentRestrictFieldInfos[Position].MaxLevel));
		GetButtonHandle(itemRendererID $ ".TimeZoneHelp_Btn").SetTooltipCustomType(MakeTooltipSimpleText(fieldUIData.Desc, 300));
		if(fieldUIData.Type == "timezone")
		{
			GetTextBoxHandle(itemRendererID $ ".TimeZoneTimeNowTitle_Txt").SetText(GetSystemString(14100));
			GetTextBoxHandle(itemRendererID $ ".TimeZoneChargeTimeTitle_Txt").SetText(GetSystemString(13027));
			GetTextBoxHandle(itemRendererID $ ".TimeZoneTimeNow_Txt").SetText(util.GetTimeStringBySec6(currentRestrictFieldInfos[Position].RemainTime));
			GetTextBoxHandle(itemRendererID $ ".TimeZoneChargeTime_Txt").SetText(util.GetTimeStringBySec6(currentRestrictFieldInfos[Position].RemainRefillTime));
			if(currentRestrictFieldInfos[Position].RemainTime == 0)
			{
				GetTextBoxHandle(itemRendererID $ ".TimeZoneTimeNow_Txt").SetTextColor(util.Gray);				
			}
			else
			{
				GetTextBoxHandle(itemRendererID $ ".TimeZoneTimeNow_Txt").SetTextColor(util.BrightWhite);
			}
			if(currentRestrictFieldInfos[Position].RemainRefillTime == 0)
			{
				GetTextBoxHandle(itemRendererID $ ".TimeZoneChargeTime_Txt").SetTextColor(util.Gray);				
			}
			else
			{
				GetTextBoxHandle(itemRendererID $ ".TimeZoneChargeTime_Txt").SetTextColor(util.BrightWhite);
			}
			GetTextureHandle(itemRendererID $ ".ZoneTypeIcon_Tex").SetTexture("L2UI_NewTex.Icon.TimeZone");
			GetTextureHandle(itemRendererID $ ".ZoneTypeIcon_Tex").SetTooltipText(GetSystemString(14094));			
		}
		else
		{
			GetTextBoxHandle(itemRendererID $ ".TimeZoneTimeNowTitle_Txt").SetText(GetSystemString(14101));
			GetTextBoxHandle(itemRendererID $ ".TimeZoneChargeTimeTitle_Txt").SetText(GetSystemString(14113));
			GetTextureHandle(itemRendererID $ ".ZoneTypeIcon_Tex").SetTexture("L2UI_NewTex.Icon.InstanceZone");
			GetTextureHandle(itemRendererID $ ".ZoneTypeIcon_Tex").SetTooltipText(GetSystemString(2668));
			if(currentRestrictFieldInfos[Position].CanUseEntranceTicket)
			{
				GetTextBoxHandle(itemRendererID $ ".TimeZoneChargeTime_Txt").SetText(GetSystemString(459));
				GetTextBoxHandle(itemRendererID $ ".TimeZoneChargeTime_Txt").SetTextColor(util.BrightWhite);				
			}
			else
			{
				GetTextBoxHandle(itemRendererID $ ".TimeZoneChargeTime_Txt").SetText(GetSystemString(13214));
				GetTextBoxHandle(itemRendererID $ ".TimeZoneChargeTime_Txt").SetTextColor(util.Gray);
			}
			GetTextBoxHandle(itemRendererID $ ".TimeZoneTimeNow_Txt").SetText(string(currentRestrictFieldInfos[Position].EnteranceCount) @ GetSystemString(3295));
			if(currentRestrictFieldInfos[Position].EnteranceCount == 0)
			{
				GetTextBoxHandle(itemRendererID $ ".TimeZoneTimeNow_Txt").SetTextColor(util.Gray);				
			}
			else
			{
				GetTextBoxHandle(itemRendererID $ ".TimeZoneTimeNow_Txt").SetTextColor(util.BrightWhite);
			}
		}
		if(currentRestrictFieldInfos[Position].nIsInZonePCCafeUserOnly == 1)
		{
			GetTextureHandle(itemRendererID $ ".PCZone_Tex").ShowWindow();
			GetTextureHandle(itemRendererID $ ".PCZone_Tex").SetTooltipType("text");
			GetTextureHandle(itemRendererID $ ".PCZone_Tex").SetTooltipText(GetSystemString(14099));			
		}
		else
		{
			GetTextureHandle(itemRendererID $ ".PCZone_Tex").HideWindow();
		}
		if(currentZoneId > 0)
		{
			if(TopGroupButtonAsset._GetGroupButtonsInstance()._getSelectButtonIndex() == 0)
			{
				enterBtn.DisableWindow();
			}
			else if(TopGroupButtonAsset._GetGroupButtonsInstance()._getSelectButtonIndex() == 1)
			{
				if(IsPlayerOnWorldRaidServer() == false)
				{
					enterBtn.DisableWindow();
				}
			}
			if(fieldUIData.Type == "timezone" && currentZoneId == fieldUIData.FieldId)
			{
				enterBtn.HideWindow();
				ExitBtn.ShowWindow();				
			}
			else
			{
				enterBtn.ShowWindow();
				ExitBtn.HideWindow();
			}			
		}
		else
		{
			enterBtn.ShowWindow();
			ExitBtn.HideWindow();
		}
	}
}

function HandleOnScroll()
{
	pageNavi.setPageText(scrollTileList._Page() + 1);
	prevNextButtonState();
}

function HandleDelegateOnClickItemRenderer(string rendererPath, int rendererIndex, int itemIndex);

function HandleDelegateOnClick(string BTNID, int rendererIndex, int Position)
{
	local array<RestrictFieldInfo> currentRestrictFieldInfos;
	local TimeRestrictFieldUIData fieldUIData;

	if(TopGroupButtonAsset._GetGroupButtonsInstance()._getSelectButtonIndex() == 0)
	{
		currentRestrictFieldInfos = RestrictFieldInfos;		
	}
	else
	{
		currentRestrictFieldInfos = RestrictFieldInfosWorld;
	}
	API_GetTimeRestrictFieldInfo(currentRestrictFieldInfos[Position].FieldId, fieldUIData);
	Debug("HandleDelegateOnClick" @ BTNID @ string(rendererIndex) @ string(Position));
	if(BTNID == "TimeZoneEnter_Btn")
	{
		class'UIAPI_WINDOW'.static.HideWindow("TimeZoneSubWnd");
		class'UIAPI_WINDOW'.static.SetAlwaysOnTop("TimeZoneSubWnd", false);
		ShowDialogAskEnter(currentRestrictFieldInfos[Position]);		
	}
	else if(BTNID == "TimeZoneTimePlus_Btn")
	{
		if(lastClickPlusButtonPosition == Position)
		{
			if(class'UIAPI_WINDOW'.static.IsShowWindow("TimeZoneSubWnd"))
			{
				class'UIAPI_WINDOW'.static.HideWindow("TimeZoneSubWnd");
				class'UIAPI_WINDOW'.static.SetAlwaysOnTop("TimeZoneSubWnd", false);					
			}		
		}
		else
		{
			TimeZoneSubWnd(GetScript("TimeZoneSubWnd")).SetShowSubWindow(currentRestrictFieldInfos[Position].FieldId, GetButtonHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".ScrollAreaWnd.ScrollArea.itemRenderer" $ getInstanceL2Util().makeZeroString(2, rendererIndex) $ ".TimeZoneTimePlus_Btn"));
			class'UIAPI_WINDOW'.static.SetAlwaysOnTop("TimeZoneSubWnd", true);
			GetWindowHandle("TimeZoneSubWnd").SetAnchor(m_hOwnerWnd.m_WindowNameWithFullPath $ ".ScrollAreaWnd.ScrollArea.itemRenderer" $ getInstanceL2Util().makeZeroString(2, rendererIndex) $ ".TimeZoneTimePlus_Btn", "TopRight", "TopRight", 0, 20);
		}
		lastClickPlusButtonPosition = Position;			
	}
	else if(BTNID == "TimeZoneExit_Btn")
	{
		class'UIAPI_WINDOW'.static.HideWindow("TimeZoneSubWnd");
		class'UIAPI_WINDOW'.static.SetAlwaysOnTop("TimeZoneSubWnd", false);
		ShowTimeZoneExitDialog();
	}
}

function bool GetBoolListCondition(int MinLevel, int MaxLevel, int RemainTime, bool bActive, optional string Type, optional int bUserBound, optional int bCanReEnter, optional int nPccafeContent)
{
	if(Type == "timezone")
	{
		if(!bActive)
		{
			return false;
		}
		if(RemainTime == 0)
		{
			return false;
		}
	}
	else
	{
		if(bUserBound == 1)
		{
			if(bCanReEnter == 0)
			{
				return false;
			}
		}
	}
	if(!GetPlayerInfo(myInfo))
	{
		return false;
	}
	if(myInfo.nLevel < MinLevel)
	{
		return false;
	}
	if(myInfo.nLevel > MaxLevel)
	{
		return false;
	}
	if(nIsPCCafeUser <= 0 && nPccafeContent > 0)
	{
		return false;
	}
	return true;
}

function string MakeMin(int Min)
{
	if(Min <= 0)
	{
		return MakeFullSystemMsg(GetSystemMessage(3390), "0");
	}
	if(Min < 60)
	{
		return MakeFullSystemMsg(GetSystemMessage(3408), MakeFullSystemMsg(GetSystemMessage(3390), "1"));
	}
	else
	{
		return MakeFullSystemMsg(GetSystemMessage(3390), string(Min / 60));
	}
}

function string MakeLevelMinMax(int Min, int Max)
{
	if (Max == 999)
	{
		return GetSystemString(88) @string(Min) @GetSystemString(859);
	}
	return string(Min) $ "~" $ string(Max);
}

function Color GetColor(int R, int G, int B, int A)
{
	local Color tColor;

	tColor.R = R;
	tColor.G = G;
	tColor.B = B;
	tColor.A = A;
	return tColor;
}

function API_RequestEnterTimeRestrictField(int FieldId)
{
	RequestEnterTimeRestrictField(FieldId);
}

function API_RequestTimeRestrictFieldList()
{
	RequestTimeRestrictFieldList();
}

function API_GetTimeRestrictFieldInfo(int FieldId, out TimeRestrictFieldUIData fieldUIData)
{
	GetTimeRestrictFieldInfo(FieldId, fieldUIData);
}

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

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

function API_C_EX_TIME_RESTRICT_FIELD_HOST_USER_LEAVE(int nLeaveFieldID, int nNextEnterFieldID)
{
	local array<byte> stream;
	local UIPacket._C_EX_TIME_RESTRICT_FIELD_HOST_USER_LEAVE packet;

	packet.nLeaveFieldID = nLeaveFieldID;
	packet.nNextEnterFieldID = nNextEnterFieldID;
	if(! class'UIPacket'.static.Encode_C_EX_TIME_RESTRICT_FIELD_HOST_USER_LEAVE(stream, packet))
	{
		return;
	}
	class'UIPacket'.static.RequestUIPacket(class'UIPacket'.const.C_EX_TIME_RESTRICT_FIELD_HOST_USER_LEAVE, stream);
	Debug("API C_EX_TIME_RESTRICT_FIELD_HOST_USER_LEAVE :" @ string(nLeaveFieldID) @ string(nNextEnterFieldID));	
}

function API_C_EX_TIME_RESTRICT_FIELD_HOST_USER_ENTER(int nEnterFieldID)
{
	local array<byte> stream;
	local UIPacket._C_EX_TIME_RESTRICT_FIELD_HOST_USER_ENTER packet;

	packet.nEnterFieldID = nEnterFieldID;
	if(! class'UIPacket'.static.Encode_C_EX_TIME_RESTRICT_FIELD_HOST_USER_ENTER(stream, packet))
	{
		return;
	}
	class'UIPacket'.static.RequestUIPacket(class'UIPacket'.const.C_EX_TIME_RESTRICT_FIELD_HOST_USER_ENTER, stream);
	Debug("API C_EX_TIME_RESTRICT_FIELD_HOST_USER_ENTER :" @ string(nEnterFieldID));	
}

delegate int SortCompare(RestrictFieldInfo A, RestrictFieldInfo B)
{
	local bool canAEnter, canBEnter;
	local int nCanA, nCanB;
	local bool canBuyA, canBuyB;
	local int i;
	local TimeRestrictFieldUIData fieldUIData;

	API_GetTimeRestrictFieldInfo(A.FieldId, fieldUIData);
	canAEnter = GetBoolListCondition(A.MinLevel, A.MaxLevel, A.RemainTime, A.bFieldActivated, fieldUIData.Type, A.bUserBound, A.bCanReEnter, A.nIsInZonePCCafeUserOnly);
	canBuyA = true;

	for(i = 0; i < A.ItemIDs.Length; i++)
	{
		if(A.ItemIDs[i] == MSIT_PCCAFE_POINT)
		{
			if(A.ItemCounts[i] > getInstanceUIData().getCurrentPcCafePoint())
			{
				canBuyA = false;
			}
			// [Explicit Continue]
			continue;
		}
		if(A.ItemCounts[i] > (getInventoryItemNumByClassID(A.ItemIDs[i])))
		{
			canBuyA = false;
		}
	}
	canAEnter = canAEnter && canBuyA;
	API_GetTimeRestrictFieldInfo(B.FieldId, fieldUIData);
	canBEnter = GetBoolListCondition(B.MinLevel, B.MaxLevel, B.RemainTime, B.bFieldActivated, fieldUIData.Type, B.bUserBound, B.bCanReEnter, B.nIsInZonePCCafeUserOnly);
	canBuyB = true;

	for(i = 0; i < B.ItemIDs.Length; i++)
	{
		if(B.ItemIDs[i] == MSIT_PCCAFE_POINT)
		{
			if(B.ItemCounts[i] > getInstanceUIData().getCurrentPcCafePoint())
			{
				canBuyB = false;
			}
			// [Explicit Continue]
			continue;
		}
		if(B.ItemCounts[i] > (getInventoryItemNumByClassID(B.ItemIDs[i])))
		{
			canBuyB = false;
		}
	}
	canBEnter = canBEnter && canBuyB;
	if(A.bFieldActivated == false)
	{
		nCanA = 4;		
	}
	else
	{
		if(A.MinLevel > myInfo.nLevel)
		{
			nCanA = 3;			
		}
		else
		{
			if((A.nIsInZonePCCafeUserOnly == 1) && nIsPCCafeUser == 0)
			{
				nCanA = 2;				
			}
			else
			{
				if(canAEnter)
				{
					nCanA = 0;					
				}
				else
				{
					nCanA = 1;
				}
			}
		}
	}
	if(B.bFieldActivated == false)
	{
		nCanB = 4;		
	}
	else
	{
		if(B.MinLevel > myInfo.nLevel)
		{
			nCanB = 3;			
		}
		else
		{
			if(B.nIsInZonePCCafeUserOnly == 1 && nIsPCCafeUser == 0)
			{
				nCanB = 2;				
			}
			else
			{
				if(canBEnter)
				{
					nCanB = 0;					
				}
				else
				{
					nCanB = 1;
				}
			}
		}
	}
	if(nCanA > nCanB)
	{
		return -1;
	}
	return 0;
}

function OnReceivedCloseUI()
{
	PlayConsoleSound(IFST_WINDOW_CLOSE);
	if(GetWindowHandle(m_hOwnerWnd.m_WindowNameWithFullPath $ ".UIControlDialogAsset").IsShowWindow())
	{
		GetDialogAssetScript().Hide();		
	}
	else
	{
		m_hOwnerWnd.HideWindow();
	}
}
