class RipperdocNewMeterBar extends inkLogicController
{
	private editable var m_bar : inkWidgetRef;
	private editable var m_overchargeHighlight : inkWidgetRef;
	private var m_root : weak< inkWidget >;
	private var m_sizeAnimation : inkAnimProxy;
	private var m_meterWidth : Float;
	private var m_pulse : PulseAnimation;

	protected event OnInitialize()
	{
		inkWidgetRef.SetVisible( m_overchargeHighlight, false );
		m_root = GetRootWidget();
	}

	public function SetState( state : CName )
	{
		inkWidgetRef.SetState( m_bar, state );
		if( inkWidgetRef.IsValid( m_overchargeHighlight ) )
		{
			if( ( ( ( state == 'Unsafe_Default' || state == 'Unsafe_Add' ) || state == 'Unsafe_Remove' ) || state == 'Unsafe_Locked' ) || state == 'Unsafe_Unlocekd' )
			{
				inkWidgetRef.SetState( m_overchargeHighlight, state );
				inkWidgetRef.SetVisible( m_overchargeHighlight, true );
			}
			else
			{
				inkWidgetRef.SetState( m_overchargeHighlight, 'Default' );
				inkWidgetRef.SetVisible( m_overchargeHighlight, false );
			}
		}
	}

	public function SetSizeAnimation( size, sizeOffset, delay, duration : Float )
	{
		var marginInterpolator : inkAnimScale;
		var animation : inkAnimDef;
		var options : inkAnimOptions;
		var startScale : Vector2;
		var endScale : Vector2;
		if( m_sizeAnimation != NULL )
		{
			m_sizeAnimation.Stop();
		}
		startScale = inkWidgetRef.GetScale( m_bar );
		endScale.X = size + sizeOffset;
		endScale.Y = 1.0;
		animation = new inkAnimDef;
		marginInterpolator = new inkAnimScale;
		marginInterpolator.SetType( inkanimInterpolationType.Quintic );
		marginInterpolator.SetMode( inkanimInterpolationMode.EasyOut );
		marginInterpolator.SetDuration( duration );
		marginInterpolator.SetUseRelativeDuration( true );
		marginInterpolator.SetStartScale( startScale );
		marginInterpolator.SetEndScale( endScale );
		animation.AddInterpolator( marginInterpolator );
		options.executionDelay = MaxF( delay, 0.0 );
		m_sizeAnimation = inkWidgetRef.PlayAnimationWithOptions( m_bar, animation, options );
	}

	public function SetSize( size : Float )
	{
		var endScale : Vector2;
		endScale.X = size;
		endScale.Y = 1.0;
		inkWidgetRef.SetScale( m_bar, endScale );
	}

	public function StartPulse( params : PulseAnimationParams )
	{
		if( m_pulse )
		{
			m_pulse.ForceStop();
		}
		m_pulse = new PulseAnimation;
		m_pulse.Configure( inkWidgetRef.Get( m_bar ), params );
		m_pulse.Start( false );
	}

	public function StopPulse()
	{
		inkWidgetRef.StopAllAnimations( m_bar );
	}

	public function GetHeight() : Float
	{
		var size : Vector2;
		size = inkWidgetRef.GetSize( m_bar );
		return size.Y;
	}

	public function GetMargin( margin : String ) : Float
	{
		var margins : inkMargin;
		margins = m_root.GetMargin();
		switch( margin )
		{
			case "left":
				return margins.left;
			case "top":
				return margins.top;
			case "right":
				return margins.right;
			case "bottom":
				return margins.bottom;
			default:
				return 0.0;
		}
	}

}

