class PrisonNoticeHUD extends UICommonAPI;

const TIMER_ID_REMAIN_TIME = 1;
const TIMER_DELAY_REMAIN_TIME = 10000;

struct PrisonUIInfo
{
	var bool inPrison;
	var int PrisonType;
	var PrisonUIData prisonData;
	var int serverRemainTime;
	var int uiRemainTime;
	var int currentItemCnt;
};

var PrisonUIInfo _prisonInfo;
var int _remainTimerCount;
var bool _isEnterPacket;
var WindowHandle Me;
var TextBoxHandle prisonNameTextBox;
var TextBoxHandle remainTimeTextBox;
var TextBoxHandle currentCntTextBox;
var TextBoxHandle maxCntTextBox;
var TextBoxHandle cntDivisionTextBox;
var L2UIInventoryObjectSimple inventoryObject;

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

function Initialize()
{
	initControls();
	inventoryObject = AddItemListenerSimple(0);
	inventoryObject.DelegateOnUpdateItem = OnNeedItemUpdated;	
}

function initControls()
{
	local string ownerFullPath;

	ownerFullPath = m_hOwnerWnd.m_WindowNameWithFullPath;
	Me = GetWindowHandle(ownerFullPath);
	prisonNameTextBox = GetTextBoxHandle(ownerFullPath $ ".PrisonName");
	remainTimeTextBox = GetTextBoxHandle(ownerFullPath $ ".PrisonTimer_txt");
	currentCntTextBox = GetTextBoxHandle(ownerFullPath $ ".PrisonCounterRemain_txt");
	maxCntTextBox = GetTextBoxHandle(ownerFullPath $ ".PrisonCounterGoal_txt");
	cntDivisionTextBox = GetTextBoxHandle(ownerFullPath $ ".Slash_txt");	
}

function UpdateUIControls()
{
	UpdateInfoControls();	
}

function UpdateRemainTimeInfo()
{
	local int timerTime;

	timerTime = _remainTimerCount * 10;
	_prisonInfo.uiRemainTime = _prisonInfo.serverRemainTime - timerTime;
	if(_prisonInfo.uiRemainTime <= 0)
	{
		KillRemainTimer();
	}	
}

function UpdateInfoControls()
{
	local Color currentCntColor;

	if(_prisonInfo.inPrison == false || _prisonInfo.PrisonType == 0)
	{
		return;
	}
	prisonNameTextBox.SetText(GetSystemString(GetPrisonTitleStringId(_prisonInfo.PrisonType)));
	remainTimeTextBox.SetText(GetRemainTimeText(_prisonInfo.uiRemainTime));
	if(_prisonInfo.prisonData.NeedItem.Id == 0)
	{
		currentCntTextBox.HideWindow();
		maxCntTextBox.HideWindow();
		cntDivisionTextBox.HideWindow();		
	}
	else
	{
		currentCntTextBox.SetText(string(_prisonInfo.currentItemCnt));
		maxCntTextBox.SetText(string(_prisonInfo.prisonData.NeedItem.Amount));
		currentCntTextBox.ShowWindow();
		maxCntTextBox.ShowWindow();
		cntDivisionTextBox.ShowWindow();
		if(_prisonInfo.currentItemCnt >= _prisonInfo.prisonData.NeedItem.Amount)
		{
			currentCntColor = GetColor(238, 170, 34, 255);			
		}
		else
		{
			currentCntColor = GetColor(221, 221, 221, 255);
		}
		currentCntTextBox.SetTextColor(currentCntColor);
	}	
}

function ResetPrisonInfo()
{
	_prisonInfo.inPrison = false;
	_prisonInfo.PrisonType = 0;
	_prisonInfo.prisonData.PrisonType = 0;
	_prisonInfo.serverRemainTime = 0;
	_prisonInfo.uiRemainTime = 0;
	_prisonInfo.currentItemCnt = 0;	
}

function CloseAndResetInfo()
{
	KillRemainTimer();
	ResetPrisonInfo();
	CloseNoticeHUD();
	_isEnterPacket = false;
	_remainTimerCount = 0;
	class'PrisonWnd'.static.Inst().ClosePrisonWnd();	
}

function StartRemainTimer()
{
	KillRemainTimer();
	Me.SetTimer(1, 10000);	
}

function KillRemainTimer()
{
	Me.KillTimer(1);	
}

function OpenNoticeHUD()
{
	if(_prisonInfo.inPrison == true && GetGameStateName() != "COLLECTIONSTATE")
	{
		Me.ShowWindow();
	}	
}

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

function CheckAndCloseWindows()
{
	local int i;
	local array<string> toCloseWindows;

	toCloseWindows[toCloseWindows.Length] = "TeleportWnd";
	toCloseWindows[toCloseWindows.Length] = "DethroneWnd";
	toCloseWindows[toCloseWindows.Length] = "TeleportBookMarkWnd";
	toCloseWindows[toCloseWindows.Length] = "OlympiadWnd";
	toCloseWindows[toCloseWindows.Length] = "TimeZoneWnd";

	for(i = 0; i < toCloseWindows.Length; i++)
	{
		if(class'UIAPI_WINDOW'.static.IsShowWindow(toCloseWindows[i]))
		{
			class'UIAPI_WINDOW'.static.HideWindow(toCloseWindows[i]);
		}
	}	
}

function UpdatePrisonInfo()
{
	local PrisonUIData prisonData;

	if(_prisonInfo.PrisonType > 0)
	{
		GetPrisonData(_prisonInfo.PrisonType, prisonData);
		_prisonInfo.prisonData = prisonData;
		inventoryObject.setId(GetItemID(prisonData.NeedItem.Id));		
	}
	else
	{
		ResetPrisonInfo();
		inventoryObject.setId();
	}	
}

function UpdatePrisonNeedItemCntInfo()
{
	local int needItemId;

	needItemId = _prisonInfo.prisonData.NeedItem.Id;
	if(_prisonInfo.inPrison == true && needItemId > 0)
	{
		_prisonInfo.currentItemCnt = int(GetInventoryItemCount(GetItemID(needItemId)));		
	}
	else
	{
		_prisonInfo.currentItemCnt = 0;
	}	
}

function int GetPrisonTitleStringId(int PrisonType)
{
	switch(PrisonType)
	{
		case 1:
			return 14213;
		case 2:
			return 14214;
		case 3:
			return 14215;
	}
	return 0;
}

function PrisonUIInfo GetInPrisonInfo()
{
	return _prisonInfo;	
}

function int GetInPrisonType()
{
	return _prisonInfo.PrisonType;	
}

function string GetRemainTimeText(int Time)
{
	local L2Util util;

	util = L2Util(GetScript("L2Util"));
	if(Time <= 0)
	{
		return MakeFullSystemMsg(GetSystemMessage(3390), string(0));		
	}
	else if(Time < 60)
	{
		return MakeFullSystemMsg(GetSystemMessage(4360), string(1));			
	}
	else
	{
		return util.getTimeStringBySec(Time, true, true);
	}
}

function Rq_C_EX_PRISON_USER_INFO()
{
	local array<byte> stream;
	local UIPacket._C_EX_PRISON_USER_INFO packet;

	if(! class'UIPacket'.static.Encode_C_EX_PRISON_USER_INFO(stream, packet))
	{
		return;
	}
	class'UIPacket'.static.RequestUIPacket(class'UIPacket'.const.C_EX_PRISON_USER_INFO, stream);	
}

function Rs_S_EX_PRISON_USER_ENTER()
{
	local UIPacket._S_EX_PRISON_USER_ENTER packet;

	if(! class'UIPacket'.static.Decode_S_EX_PRISON_USER_ENTER(packet))
	{
		return;
	}
	_isEnterPacket = true;
	CheckAndCloseWindows();
	Rq_C_EX_PRISON_USER_INFO();	
}

function Rs_S_EX_PRISON_USER_EXIT()
{
	local UIPacket._S_EX_PRISON_USER_EXIT packet;

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

function Rs_S_EX_PRISON_USER_INFO()
{
	local UIPacket._S_EX_PRISON_USER_INFO packet;

	if(! class'UIPacket'.static.Decode_S_EX_PRISON_USER_INFO(packet))
	{
		return;
	}
	if(packet.cPrisonType > 0)
	{
		_prisonInfo.inPrison = true;		
	}
	else
	{
		_prisonInfo.inPrison = false;
	}
	_prisonInfo.PrisonType = packet.cPrisonType;
	_prisonInfo.currentItemCnt = packet.nItemAmount;
	_prisonInfo.serverRemainTime = packet.nRemainTime;
	_prisonInfo.uiRemainTime = packet.nRemainTime;
	UpdatePrisonInfo();
	if(_prisonInfo.inPrison == true)
	{
		_remainTimerCount = 0;
		OpenNoticeHUD();
		if(_prisonInfo.serverRemainTime != 0)
		{
			StartRemainTimer();
		}
		if(_isEnterPacket == true)
		{
			_isEnterPacket = false;
			class'PrisonWnd'.static.Inst().OpenPrisonWnd();
		}
	}	
}

function Nt_EV_Restart()
{
	CloseAndResetInfo();	
}

function Nt_EV_StateChanged()
{
	if(GetGameStateName() == "GAMINGSTATE")
	{
		OpenNoticeHUD();		
	}
	else
	{
		CloseNoticeHUD();
		if(GetGameStateName() != "COLLECTIONSTATE")
		{
			KillRemainTimer();
		}
	}	
}

event OnRegisterEvent()
{
	RegisterEvent(EV_PacketID(class'UIPacket'.const.S_EX_PRISON_USER_ENTER));
	RegisterEvent(EV_PacketID(class'UIPacket'.const.S_EX_PRISON_USER_EXIT));
	RegisterEvent(EV_PacketID(class'UIPacket'.const.S_EX_PRISON_USER_INFO));
	RegisterEvent(EV_Restart);
	RegisterEvent(EV_StateChanged);	
}

event OnEvent(int EventID, string param)
{
	switch(EventID)
	{
		case EV_Restart:
			Nt_EV_Restart();
			break;
		case EV_StateChanged:
			Nt_EV_StateChanged();
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_PRISON_USER_ENTER):
			Rs_S_EX_PRISON_USER_ENTER();
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_PRISON_USER_EXIT):
			Rs_S_EX_PRISON_USER_EXIT();
			break;
		case EV_PacketID(class'UIPacket'.const.S_EX_PRISON_USER_INFO):
			Rs_S_EX_PRISON_USER_INFO();
			break;
	}	
}

event OnTimer(int TimerID)
{
	if(TimerID == 1)
	{
		_remainTimerCount++;
		UpdateRemainTimeInfo();
		UpdateInfoControls();
		class'PrisonWnd'.static.Inst().UpdatePrisonPanelControls();
	}	
}

event OnClickButton(string buttonStr)
{
	switch(buttonStr)
	{
		case "PrisonHud_btn":
			OnPrisonWndBtnClicked();
			break;
	}	
}

event OnNeedItemUpdated(optional array<ItemInfo> iInfo, optional int Index)
{
	if(_prisonInfo.inPrison == true && _prisonInfo.prisonData.NeedItem.Id > 0 && iInfo.Length > 0)
	{
		UpdatePrisonNeedItemCntInfo();
		UpdateInfoControls();
		class'PrisonWnd'.static.Inst().UpdatePrisonPanelControls();
	}	
}

event OnPrisonWndBtnClicked()
{
	class'PrisonWnd'.static.Inst().ToggleOpenPrisonWnd();	
}

event OnShow()
{
	UpdatePrisonInfo();
	UpdateUIControls();	
}

event OnLoad()
{
	Initialize();	
}
