class RipperdocSelectorController extends inkLogicController
{
	private editable var m_label : inkTextRef;
	private editable var m_leftArrowAnchor : inkWidgetRef;
	private editable var m_rightArrowAnchor : inkWidgetRef;
	private const editable var m_indicatorAnchors : array< inkWidgetRef >;
	private var m_leftArrow : weak< inkButtonController >;
	private var m_rightArrow : weak< inkButtonController >;
	private var m_indicatorIndex : Int32;
	private var m_indicatorShowAnim : inkAnimProxy;
	private var m_indicatorHideAnim : inkAnimProxy;
	private var m_isInTutorial : Bool;
	private var m_names : array< String >;

	public function Configure( names : array< String > )
	{
		m_leftArrow = ( ( inkButtonController )( inkWidgetRef.GetControllerByType( m_leftArrowAnchor, 'inkButtonController' ) ) );
		m_rightArrow = ( ( inkButtonController )( inkWidgetRef.GetControllerByType( m_rightArrowAnchor, 'inkButtonController' ) ) );
		m_leftArrow.RegisterToCallback( 'OnButtonStateChanged', this, 'OnLeftArrow' );
		m_rightArrow.RegisterToCallback( 'OnButtonStateChanged', this, 'OnRightArrow' );
		m_names = names;
		RegisterToGlobalInputCallback( 'OnPostOnRelease', this, 'OnReleaseInput' );
	}

	public function SetIsInTutorial( isInTutorial : Bool )
	{
		m_isInTutorial = isInTutorial;
	}

	protected event OnUninitialize()
	{
		UnregisterFromGlobalInputCallback( 'OnPostOnRelease', this, 'OnReleaseInput' );
	}

	protected event OnReleaseInput( e : inkPointerEvent )
	{
		if( !( m_isInTutorial ) )
		{
			if( e.IsAction( 'option_switch_prev_settings' ) )
			{
				SwitchIndicator( false );
			}
			else if( e.IsAction( 'option_switch_next_settings' ) )
			{
				SwitchIndicator( true );
			}
		}
	}

	private function OnLeftArrow( controller : weak< inkButtonController >, oldState : inkEButtonState, newState : inkEButtonState )
	{
		if( newState == inkEButtonState.Press )
		{
			SwitchIndicator( false );
		}
	}

	private function OnRightArrow( controller : weak< inkButtonController >, oldState : inkEButtonState, newState : inkEButtonState )
	{
		if( newState == inkEButtonState.Press )
		{
			SwitchIndicator( true );
		}
	}

	public function Show( index : Int32 )
	{
		SetIndicator( index, true );
	}

	public function Hide()
	{
		SetIndicator( m_indicatorIndex, false );
	}

	private function SwitchIndicator( toNext : Bool )
	{
		var selectorEvent : RipperdocSelectorChangeEvent;
		SetIndicator( m_indicatorIndex, false );
		SetIndicator( WrapIndex( m_indicatorIndex + ( ( toNext ) ? ( 1 ) : ( -1 ) ) ), true );
		selectorEvent = new RipperdocSelectorChangeEvent;
		selectorEvent.Index = m_indicatorIndex;
		selectorEvent.SlidingRight = toNext;
		QueueEvent( selectorEvent );
	}

	private function SetIndicator( index : Int32, toActive : Bool )
	{
		var i : Int32;
		for( i = 0; i < m_indicatorAnchors.Size(); i += 1 )
		{
			inkWidgetRef.Get( m_indicatorAnchors[ i ] ).SetState( 'Default' );
		}
		if( ( index >= 0 ) && ( index < m_indicatorAnchors.Size() ) )
		{
			if( toActive )
			{
				m_indicatorIndex = index;
				inkTextRef.SetText( m_label, m_names[ index ] );
			}
			inkWidgetRef.Get( m_indicatorAnchors[ index ] ).SetState( ( ( toActive ) ? ( 'Selected' ) : ( 'Default' ) ) );
		}
	}

	private function WrapIndex( index : Int32 ) : Int32
	{
		if( index >= m_indicatorAnchors.Size() )
		{
			return 0;
		}
		else if( index < 0 )
		{
			return m_indicatorAnchors.Size() - 1;
		}
		return index;
	}

	public function GetIndicatorIndex() : Int32
	{
		return m_indicatorIndex;
	}

}

class RipperdocSelectorChangeEvent extends Event
{
	var Index : Int32;
	var SlidingRight : Bool;
}

