class NumericDispalyUIController extends DeviceInkGameControllerBase
{
	[ category = "Widget Refs" ]
	private editable var m_currentNumberTextWidget : inkTextRef;
	[ category = "Widget Refs" ]
	private editable var m_upArrowWidget : inkWidgetRef;
	[ category = "Widget Refs" ]
	private editable var m_downArrowWidget : inkWidgetRef;
	[ category = "Animations" ]
	private editable var m_idleAnimName : CName;
	default m_idleAnimName = 'idle';
	[ category = "Animations" ]
	private editable var m_goingUpAnimName : CName;
	default m_goingUpAnimName = 'going_up';
	[ category = "Animations" ]
	private editable var m_goingDownAnimName : CName;
	default m_goingDownAnimName = 'going_down';
	private var m_idleAnim : inkAnimProxy;
	private var m_goingDownAnim : inkAnimProxy;
	private var m_goingUpAnim : inkAnimProxy;
	private var m_onNumberChangedListener : CallbackHandle;
	private var m_onDirectionChangedListener : CallbackHandle;

	protected override function SetupWidgets()
	{
		super.SetupWidgets();
		if( !( m_isInitialized ) )
		{
			Initialize();
		}
	}

	protected function Initialize()
	{
		var owner : NumericDisplay;
		owner = GetOwner();
		if( owner )
		{
			SetCurrentNumberOnUI( owner.GetBlackboard().GetInt( owner.GetBlackboardDef().CurrentNumber ) );
			PlayDirectionAnim( owner.GetBlackboard().GetInt( owner.GetBlackboardDef().Direction ) );
		}
	}

	protected function SetCurrentNumberOnUI( number : Int32 )
	{
		var numberStr : String;
		numberStr = ( ( number < 0 ) ? ( "-" ) : ( "" ) );
		if( Abs( number ) < 10 )
		{
			numberStr += "0";
		}
		if( Abs( number ) < 100 )
		{
			numberStr += "0";
		}
		numberStr += IntToString( Abs( number ) );
		inkTextRef.SetText( m_currentNumberTextWidget, numberStr );
	}

	protected override function GetOwner() : NumericDisplay
	{
		return ( ( NumericDisplay )( GetOwnerEntity() ) );
	}

	protected override function RegisterBlackboardCallbacks( blackboard : IBlackboard )
	{
		super.RegisterBlackboardCallbacks( blackboard );
		if( blackboard )
		{
			m_onNumberChangedListener = blackboard.RegisterListenerInt( GetOwner().GetBlackboardDef().CurrentNumber, this, 'OnCurrentNumberChanged', true );
			m_onDirectionChangedListener = blackboard.RegisterListenerInt( GetOwner().GetBlackboardDef().Direction, this, 'OnDirectionChanged', true );
		}
	}

	protected override function UnRegisterBlackboardCallbacks( blackboard : IBlackboard )
	{
		if( blackboard )
		{
			blackboard.UnregisterListenerInt( GetOwner().GetBlackboardDef().CurrentNumber, m_onNumberChangedListener );
		}
		super.UnRegisterBlackboardCallbacks( blackboard );
	}

	protected event OnCurrentNumberChanged( number : Int32 )
	{
		SetCurrentNumberOnUI( number );
	}

	protected function StopUpAnim()
	{
		if( m_goingUpAnim )
		{
			m_goingUpAnim.Pause();
			m_goingUpAnim = NULL;
		}
		inkWidgetRef.SetOpacity( m_upArrowWidget, 0.0 );
	}

	protected function StopDownAnim()
	{
		if( m_goingDownAnim )
		{
			m_goingDownAnim.Pause();
			m_goingDownAnim = NULL;
		}
		inkWidgetRef.SetOpacity( m_downArrowWidget, 0.0 );
	}

	protected function StopIdleAnim()
	{
		if( m_idleAnim )
		{
			m_idleAnim.Stop();
			m_idleAnim = NULL;
		}
	}

	protected function PlayUpAnim()
	{
		StopIdleAnim();
		StopDownAnim();
		if( !( m_goingUpAnim && m_goingUpAnim.IsPlaying() ) )
		{
			m_goingUpAnim = PlayLibraryAnimation( m_goingUpAnimName, GetAnimOptionsInfiniteLoop( inkanimLoopType.Cycle ) );
		}
	}

	protected function PlayDownAnim()
	{
		StopIdleAnim();
		StopUpAnim();
		if( !( m_goingDownAnim && m_goingDownAnim.IsPlaying() ) )
		{
			m_goingDownAnim = PlayLibraryAnimation( m_goingDownAnimName, GetAnimOptionsInfiniteLoop( inkanimLoopType.Cycle ) );
		}
	}

	protected function PlayIdleAnim()
	{
		StopUpAnim();
		StopDownAnim();
		if( !( m_idleAnim && m_idleAnim.IsPlaying() ) )
		{
			m_idleAnim = PlayLibraryAnimation( m_idleAnimName, GetAnimOptionsInfiniteLoop( inkanimLoopType.Cycle ) );
		}
	}

	protected function PlayDirectionAnim( direction : Int32 )
	{
		switch( direction )
		{
			case -1:
				PlayDownAnim();
			break;
			case 0:
				PlayIdleAnim();
			break;
			case 1:
				PlayUpAnim();
			break;
		}
	}

	protected event OnDirectionChanged( direction : Int32 )
	{
		PlayDirectionAnim( direction );
	}

}

