class RipperdocFillLabel extends inkLogicController
{
	private editable var m_label : inkTextRef;
	private editable var m_useMargin : Bool;
	private var m_root : weak< inkWidget >;
	private var m_labelAnimator : weak< inkTextValueProgressController >;
	private var m_height : Float;
	private var m_startSize : Vector2;
	private var m_positionAnimation : inkAnimProxy;
	private var m_labelAnimation : inkAnimProxy;
	private var m_labelValue : Float;

	public function Configure( height : Float )
	{
		m_root = GetRootWidget();
		m_height = height;
		m_startSize = m_root.GetSize();
		m_labelValue = 0.0;
		m_labelAnimator = ( ( inkTextValueProgressController )( inkTextRef.GetController( m_label ) ) );
	}

	public function SetLabel( value : Int32, percent : Float, optional duration : Float )
	{
		AnimateLabel( value, duration * 1.25 );
		if( m_useMargin )
		{
			AnimateMargin( ClampF( percent, 0.0, 1.0 ), duration );
		}
		else
		{
			AnimateSize( ClampF( percent, 0.0, 1.0 ), duration );
		}
	}

	public function AnimateLabel( value : Int32, duration : Float )
	{
		if( m_labelAnimation != NULL )
		{
			m_labelAnimation.Stop();
		}
		if( duration == 0.0 )
		{
			m_labelAnimation = NULL;
			inkTextRef.SetText( m_label, IntToString( value ) );
			m_labelValue = ( ( Float )( value ) );
			return;
		}
		m_labelAnimator.SetDelay( 0.0 );
		m_labelAnimator.SetDuration( duration );
		m_labelAnimator.SetBaseValue( m_labelValue );
		m_labelValue = ( ( Float )( value ) );
		m_labelAnimator.SetTargetValue( m_labelValue );
		m_labelAnimation = m_labelAnimator.PlaySetAnimation();
	}

	private function AnimateMargin( percent : Float, duration : Float )
	{
		var margin : inkMargin;
		var animation : inkAnimDef;
		var marginInterpolator : inkAnimMargin;
		if( m_positionAnimation != NULL )
		{
			m_positionAnimation.Stop();
		}
		margin.bottom = percent * m_height;
		if( duration == 0.0 )
		{
			m_positionAnimation = NULL;
			m_root.SetMargin( margin );
			return;
		}
		marginInterpolator = new inkAnimMargin;
		marginInterpolator.SetDuration( duration );
		marginInterpolator.SetStartMargin( m_root.GetMargin() );
		marginInterpolator.SetEndMargin( margin );
		marginInterpolator.SetType( inkanimInterpolationType.Sinusoidal );
		marginInterpolator.SetMode( inkanimInterpolationMode.EasyOut );
		animation = new inkAnimDef;
		animation.AddInterpolator( marginInterpolator );
		m_positionAnimation = m_root.PlayAnimation( animation );
	}

	private function AnimateSize( percent : Float, duration : Float )
	{
		var size : Vector2;
		var animation : inkAnimDef;
		var sizeInterpolator : inkAnimSize;
		if( m_positionAnimation != NULL )
		{
			m_positionAnimation.Stop();
		}
		size = m_root.GetSize();
		size.Y = ( ( 1.0 - percent ) * m_height ) + m_startSize.Y;
		if( duration == 0.0 )
		{
			m_positionAnimation = NULL;
			m_root.SetSize( size );
			return;
		}
		sizeInterpolator = new inkAnimSize;
		sizeInterpolator.SetDuration( duration );
		sizeInterpolator.SetStartSize( m_root.GetSize() );
		sizeInterpolator.SetEndSize( size );
		sizeInterpolator.SetType( inkanimInterpolationType.Sinusoidal );
		sizeInterpolator.SetMode( inkanimInterpolationMode.EasyOut );
		animation = new inkAnimDef;
		animation.AddInterpolator( sizeInterpolator );
		m_positionAnimation = m_root.PlayAnimation( animation );
	}

}

