class NarrationJournalGameController extends inkHUDGameController
{
	private editable var m_entriesContainer : inkCompoundRef;
	private var m_narrationJournalBlackboardId : CallbackHandle;

	protected event OnInitialize()
	{
		var narrationJournalBB : IBlackboard;
		narrationJournalBB = GetBlackboardSystem().Get( GetAllBlackboardDefs().UI_HUDNarrationLog );
		m_narrationJournalBlackboardId = narrationJournalBB.RegisterListenerVariant( GetAllBlackboardDefs().UI_HUDNarrationLog.LastEvent, this, 'OnEventAdded' );
	}

	protected event OnEventAdded( value : Variant )
	{
		var lastEvent : NarrationEvent;
		lastEvent = ( ( NarrationEvent )value );
		AddEntry( lastEvent );
	}

	private function AddEntry( const entry : ref< NarrationEvent > )
	{
		var entryPanel : weak< inkWidget >;
		var controller : weak< LogEntryLogicController >;
		entryPanel = SpawnFromLocal( inkCompoundRef.Get( m_entriesContainer ), 'LogEntry' );
		controller = ( ( LogEntryLogicController )( entryPanel.GetController() ) );
		controller.SetValues( entry );
		controller.RegisterToCallback( 'OnEntryHidden', this, 'OnEntryHidden' );
	}

	protected event OnEntryHidden( entryWidget : weak< inkWidget > )
	{
		inkCompoundRef.RemoveChild( m_entriesContainer, entryWidget );
	}

}

class LogEntryLogicController extends inkLogicController
{
	private var m_root : weak< inkWidget >;
	private editable var m_textWidget : inkTextRef;
	private var m_animProxyTimeout : inkAnimProxy;
	private var m_animProxyFadeOut : inkAnimProxy;

	protected event OnInitialize()
	{
		m_root = GetRootWidget();
	}

	public function SetValues( const entry : ref< NarrationEvent > )
	{
		inkTextRef.SetText( m_textWidget, entry.text );
		inkTextRef.SetTintColor( m_textWidget, entry.color );
		SetTimeout( entry.durationSec );
	}

	private function SetTimeout( value : Float )
	{
		var interpol : inkAnimTransparency;
		var timeoutAnim : inkAnimDef;
		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 )
	{
		m_animProxyFadeOut = PlayLibraryAnimation( 'EntryFadeOut' );
		m_animProxyFadeOut.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnHide' );
	}

	protected event OnHide( anim : inkAnimProxy )
	{
		m_root.SetVisible( false );
		CallCustomCallback( 'OnEntryHidden' );
	}

}

