class OnscreenMessageGameController extends inkHUDGameController
{
	private var m_root : weak< inkWidget >;
	private var m_blackboard : weak< IBlackboard >;
	private var m_blackboardDef : UI_NotificationsDef;
	private var m_screenMessageUpdateCallbackId : CallbackHandle;
	private var m_screenMessage : SimpleScreenMessage;
	private editable var m_mainTextWidget : inkTextRef;
	private var m_blinkingAnim : inkAnimDef;
	private var m_showAnim : inkAnimDef;
	private var m_hideAnim : inkAnimDef;
	private var m_animProxyShow : inkAnimProxy;
	private var m_animProxyHide : inkAnimProxy;
	private var m_animProxyTimeout : inkAnimProxy;

	protected event OnInitialize()
	{
		var variant : Variant;
		m_root = GetRootWidget();
		m_root.SetVisible( false );
		m_blackboardDef = GetAllBlackboardDefs().UI_Notifications;
		m_blackboard = GetBlackboardSystem().Get( m_blackboardDef );
		m_screenMessageUpdateCallbackId = m_blackboard.RegisterDelayedListenerVariant( m_blackboardDef.OnscreenMessage, this, 'OnScreenMessageUpdate' );
		variant = m_blackboard.GetVariant( m_blackboardDef.OnscreenMessage );
		if( variant.IsValid() )
		{
			m_screenMessage = ( ( SimpleScreenMessage )variant );
		}
		CreateAnimations();
	}

	protected event OnUnitialize()
	{
		m_blackboard.UnregisterDelayedListener( m_blackboardDef.OnscreenMessage, m_screenMessageUpdateCallbackId );
	}

	protected event OnScreenMessageUpdate( value : Variant )
	{
		m_screenMessage = ( ( SimpleScreenMessage )value );
		UpdateWidgets();
	}

	private function UpdateWidgets()
	{
		CleanupAnimProxies();
		m_root.StopAllAnimations();
		if( m_screenMessage.isShown )
		{
			inkTextRef.SetLetterCase( m_mainTextWidget, textLetterCase.UpperCase );
			inkTextRef.SetText( m_mainTextWidget, GetLocalizedText( m_screenMessage.message ) );
			m_root.SetVisible( true );
			m_animProxyShow = PlayLibraryAnimation( 'CInematic_Subtitle' );
			SetTimeout( m_screenMessage.duration );
		}
		else
		{
			m_root.SetVisible( false );
		}
	}

	private function CleanupAnimProxies()
	{
		if( m_animProxyShow )
		{
			m_animProxyShow.UnregisterFromAllCallbacks( inkanimEventType.OnFinish );
			m_animProxyShow.Stop();
			m_animProxyShow = NULL;
		}
		if( m_animProxyHide )
		{
			m_animProxyHide.UnregisterFromAllCallbacks( inkanimEventType.OnFinish );
			m_animProxyHide.Stop();
			m_animProxyHide = NULL;
		}
	}

	private function SetTimeout( value : Float )
	{
		var interpol : inkAnimTransparency;
		var timeoutAnim : inkAnimDef;
		if( m_animProxyTimeout )
		{
			m_animProxyTimeout.UnregisterFromAllCallbacks( inkanimEventType.OnFinish );
			m_animProxyTimeout.Stop();
			m_animProxyTimeout = NULL;
		}
		if( value > 0.0 )
		{
			timeoutAnim = new inkAnimDef;
			interpol = new inkAnimTransparency;
			interpol.SetDuration( value );
			interpol.SetStartTransparency( 1.0 );
			interpol.SetEndTransparency( 1.0 );
			interpol.SetIsAdditive( true );
			timeoutAnim.AddInterpolator( interpol );
			m_animProxyTimeout = m_root.PlayAnimation( timeoutAnim );
			m_animProxyTimeout.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnTimeout' );
		}
	}

	protected event OnTimeout( anim : inkAnimProxy )
	{
		if( anim.IsFinished() )
		{
			m_blackboard.SetVariant( m_blackboardDef.OnscreenMessage, NoScreenMessage() );
		}
		UpdateWidgets();
	}

	protected event OnShown( anim : inkAnimProxy )
	{
		if( anim.IsFinished() )
		{
			TriggerBlinkAnimation();
		}
	}

	protected event OnBlinkAnimation( anim : inkAnimProxy )
	{
		if( anim.IsFinished() )
		{
			TriggerBlinkAnimation();
		}
	}

	protected event OnHidden( anim : inkAnimProxy )
	{
		m_root.SetVisible( false );
	}

	private function TriggerBlinkAnimation()
	{
		var proxy : inkAnimProxy;
		proxy = m_root.PlayAnimation( m_blinkingAnim );
		proxy.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnBlinkAnimation' );
	}

	private function CreateAnimations()
	{
		var alphaBlinkInInterpol : inkAnimTransparency;
		var alphaBlinkOutInterpol : inkAnimTransparency;
		var alphaShowInterpol : inkAnimTransparency;
		var alphaHideInterpol : inkAnimTransparency;
		m_blinkingAnim = new inkAnimDef;
		alphaBlinkOutInterpol = new inkAnimTransparency;
		alphaBlinkOutInterpol.SetStartTransparency( 1.0 );
		alphaBlinkOutInterpol.SetEndTransparency( 0.40000001 );
		alphaBlinkOutInterpol.SetDuration( 0.5 );
		alphaBlinkOutInterpol.SetType( inkanimInterpolationType.Linear );
		alphaBlinkOutInterpol.SetMode( inkanimInterpolationMode.EasyOut );
		alphaBlinkInInterpol = new inkAnimTransparency;
		alphaBlinkInInterpol.SetStartTransparency( 0.40000001 );
		alphaBlinkInInterpol.SetEndTransparency( 1.0 );
		alphaBlinkInInterpol.SetDuration( 0.5 );
		alphaBlinkInInterpol.SetStartDelay( 0.5 );
		alphaBlinkInInterpol.SetType( inkanimInterpolationType.Linear );
		alphaBlinkInInterpol.SetMode( inkanimInterpolationMode.EasyOut );
		m_blinkingAnim.AddInterpolator( alphaBlinkOutInterpol );
		m_blinkingAnim.AddInterpolator( alphaBlinkInInterpol );
		m_showAnim = new inkAnimDef;
		alphaShowInterpol = new inkAnimTransparency;
		alphaShowInterpol.SetStartTransparency( 0.0 );
		alphaShowInterpol.SetEndTransparency( 1.0 );
		alphaShowInterpol.SetDuration( 0.5 );
		alphaShowInterpol.SetType( inkanimInterpolationType.Exponential );
		alphaShowInterpol.SetMode( inkanimInterpolationMode.EasyOut );
		m_showAnim.AddInterpolator( alphaShowInterpol );
		m_hideAnim = new inkAnimDef;
		alphaHideInterpol = new inkAnimTransparency;
		alphaHideInterpol.SetStartTransparency( 1.0 );
		alphaHideInterpol.SetEndTransparency( 0.0 );
		alphaHideInterpol.SetDuration( 1.0 );
		alphaBlinkInInterpol.SetStartDelay( 0.1 );
		alphaHideInterpol.SetType( inkanimInterpolationType.Exponential );
		alphaHideInterpol.SetMode( inkanimInterpolationMode.EasyOut );
		m_hideAnim.AddInterpolator( alphaHideInterpol );
	}

}

