class TimerGameController extends inkHUDGameController
{
	private editable var m_value : inkTextRef;
	private var m_rootWidget : weak< inkWidget >;
	private var m_timerBB : weak< IBlackboard >;
	private var m_timerDef : UIGameDataDef;
	private var m_activeBBID : CallbackHandle;
	private var m_progressBBID : CallbackHandle;

	protected event OnInitialize()
	{
		m_rootWidget = GetRootWidget();
		m_rootWidget.SetVisible( false );
		SetupBB();
	}

	protected event OnUninitialize()
	{
		UnregisterFromBB();
	}

	private function SetupBB()
	{
		m_timerDef = GetAllBlackboardDefs().UIGameData;
		m_timerBB = GetBlackboardSystem().Get( m_timerDef );
		if( m_timerBB )
		{
			m_activeBBID = m_timerBB.RegisterDelayedListenerFloat( m_timerDef.QuestTimerInitialDuration, this, 'OnTimerActiveUpdated' );
			m_progressBBID = m_timerBB.RegisterDelayedListenerFloat( m_timerDef.QuestTimerCurrentDuration, this, 'OnTimerProgressUpdated' );
		}
	}

	private function UnregisterFromBB()
	{
		if( m_timerBB )
		{
			m_timerBB.UnregisterDelayedListener( m_timerDef.QuestTimerInitialDuration, m_activeBBID );
			m_timerBB.UnregisterDelayedListener( m_timerDef.QuestTimerCurrentDuration, m_progressBBID );
		}
	}

	protected event OnTimerActiveUpdated( value : Float )
	{
		UpdateTimerActive( value );
	}

	protected event OnTimerProgressUpdated( value : Float )
	{
		UpdateTimerProgress( value );
	}

	public function UpdateTimerActive( value : Float )
	{
		var active : Bool;
		if( value > 0.0 )
		{
			active = true;
		}
		if( active )
		{
			Intro();
		}
		else
		{
			Outro();
		}
	}

	public function UpdateTimerProgress( time : Float )
	{
		var minutes : Int32;
		var seconds : Int32;
		var res : String;
		minutes = FloorF( time / 60.0 );
		seconds = FloorF( time - ( ( Float )( ( 60 * minutes ) ) ) );
		if( minutes <= 9 )
		{
			res += "0";
		}
		res += ( IntToString( minutes ) + ":" );
		if( seconds <= 9 )
		{
			res += "0";
		}
		res += IntToString( seconds );
		inkTextRef.SetText( m_value, res );
	}

	private function Intro()
	{
		m_rootWidget.SetVisible( true );
	}

	private function Outro()
	{
		Hide();
	}

	public function Hide()
	{
		m_rootWidget.SetVisible( false );
	}

}

