class PhotoModeListController extends ListController
{
	private editable var m_LogoWidget : inkWidgetRef;
	private editable var m_Panel : inkVerticalPanelRef;
	private var m_fadeAnim : inkAnimProxy;
	private var m_isAnimating : Bool;
	private var m_animationTime : Float;
	private var m_animationTarget : Float;
	private var m_elementsAnimationTime : Float;
	private var m_elementsAnimationDelay : Float;
	private var m_currentElementAnimation : Int32;

	public function SetReversedUI( isReversed : Bool )
	{
		var listElement : weak< inkWidget >;
		var photoModeListItem : PhotoModeMenuListItem;
		var i : Int32;
		for( i = 0; i < Size(); i += 1 )
		{
			listElement = GetItemAt( i );
			photoModeListItem = ( ( PhotoModeMenuListItem )( listElement.GetControllerByType( 'PhotoModeMenuListItem' ) ) );
			photoModeListItem.SetReversedUI( isReversed );
		}
	}

	private function OnVisbilityChanged( visible : Bool )
	{
		var listElement : weak< inkWidget >;
		var i : Int32;
		var controller : PhotoModeMenuListItem;
		for( i = 0; i < Size(); i += 1 )
		{
			listElement = GetItemAt( i );
			if( listElement )
			{
				controller = ( ( PhotoModeMenuListItem )( listElement.GetControllerByType( 'PhotoModeMenuListItem' ) ) );
				if( controller )
				{
					controller.OnVisbilityChanged( visible );
				}
			}
		}
	}

	private function PlayFadeAnimation( fadeIn : Bool )
	{
		m_elementsAnimationTime = 0.0;
		m_currentElementAnimation = 0;
		if( fadeIn )
		{
			GetRootWidget().SetVisible( true );
			SetAllItemsOpacity( 0.0 );
			PlayLibraryAnimation( 'list_container_in' );
			OnVisbilityChanged( true );
		}
		else
		{
			PlayLibraryAnimation( 'list_container_out' );
		}
	}

	private function PlayFadeElementAnimation( fadeIn : Bool )
	{
		var listElement : weak< inkWidget >;
		var photoModeListItem : PhotoModeMenuListItem;
		listElement = GetItemAt( m_currentElementAnimation );
		photoModeListItem = ( ( PhotoModeMenuListItem )( listElement.GetControllerByType( 'PhotoModeMenuListItem' ) ) );
		if( fadeIn )
		{
			listElement.SetOpacity( 1.0 );
			photoModeListItem.PlayLibraryAnimation( 'option_in' );
		}
		else
		{
			photoModeListItem.PlayLibraryAnimation( 'option_out' );
		}
		m_elementsAnimationTime = m_elementsAnimationDelay;
		m_currentElementAnimation += 1;
		if( m_currentElementAnimation >= Size() )
		{
			m_currentElementAnimation = -1;
			if( !( fadeIn ) )
			{
				GetRootWidget().SetVisible( false );
				OnVisbilityChanged( false );
			}
		}
	}

	private function SetAllItemsOpacity( opacity : Float )
	{
		var listElement : weak< inkWidget >;
		var i : Int32;
		for( i = 0; i < Size(); i += 1 )
		{
			listElement = GetItemAt( i );
			listElement.SetOpacity( opacity );
		}
	}

	public function ShowAnimated( delay : Float )
	{
		m_animationTime = -( delay );
		m_animationTarget = 1.0;
		m_isAnimating = true;
		m_currentElementAnimation = -1;
	}

	public function HideAnimated( delay : Float )
	{
		m_animationTime = -( delay );
		m_animationTarget = 0.0;
		m_isAnimating = true;
		m_currentElementAnimation = -1;
	}

	public function Update( timeDelta : Float )
	{
		if( m_isAnimating )
		{
			m_animationTime += timeDelta;
			if( m_animationTime >= 0.0 )
			{
				PlayFadeAnimation( m_animationTarget == 1.0 );
				m_isAnimating = false;
			}
		}
		if( m_currentElementAnimation >= 0 )
		{
			m_elementsAnimationTime -= timeDelta;
			if( m_elementsAnimationTime <= 0.0 )
			{
				PlayFadeElementAnimation( m_animationTarget == 1.0 );
			}
		}
	}

	public function PostInitItems()
	{
		m_elementsAnimationTime = 0.0;
		m_elementsAnimationDelay = 0.03;
		m_currentElementAnimation = -1;
		Refresh();
		inkVerticalPanelRef.ReorderChild( m_Panel, inkWidgetRef.Get( m_LogoWidget ), Size() );
		GetRootWidget().SetVisible( false );
	}

	public function HandleInputWithVisibilityCheck( e : inkPointerEvent, optional gameCtrl : weak< inkGameController > )
	{
		var widgetHStack : inkHorizontalPanel;
		var listElement : weak< inkWidget >;
		var photoModeListItem : PhotoModeMenuListItem;
		var gridSelector : weak< PhotoModeGridList >;
		widgetHStack = ( ( inkHorizontalPanel )( GetRootWidget() ) );
		listElement = GetItemAt( GetSelectedIndex() );
		photoModeListItem = ( ( PhotoModeMenuListItem )( listElement.GetControllerByType( 'PhotoModeMenuListItem' ) ) );
		gridSelector = photoModeListItem.GetGridSelector();
		if( widgetHStack )
		{
			if( e.IsAction( 'left_button' ) )
			{
				SelectPriorVisible( GetSelectedIndex() );
			}
			else if( e.IsAction( 'right_button' ) )
			{
				SelectNextVisible( GetSelectedIndex() );
			}
		}
		else
		{
			if( e.IsAction( 'up_button' ) )
			{
				if( ( gridSelector == NULL ) || gridSelector.TrySelectUp() == false )
				{
					SelectPriorVisible( GetSelectedIndex() );
				}
			}
			else if( e.IsAction( 'down_button' ) )
			{
				if( ( gridSelector == NULL ) || gridSelector.TrySelectDown() == false )
				{
					SelectNextVisible( GetSelectedIndex() );
				}
			}
			else if( e.IsAction( 'PhotoMode_Left_Button' ) )
			{
				if( gridSelector )
				{
					gridSelector.TrySelectLeft();
				}
			}
			else if( e.IsAction( 'PhotoMode_Right_Button' ) )
			{
				if( gridSelector )
				{
					gridSelector.TrySelectRight();
				}
			}
		}
	}

	public function GetFirstVisibleIndex() : Int32
	{
		var listElement : weak< inkWidget >;
		var i : Int32;
		for( i = 0; i < Size(); i += 1 )
		{
			listElement = GetItemAt( i );
			if( listElement.IsVisible() )
			{
				return i;
			}
		}
		return 0;
	}

	protected function SelectPriorVisible( currentIndex : Int32 ) : Bool
	{
		var indexToSet : Int32;
		var listElement : weak< inkWidget >;
		indexToSet = currentIndex - 1;
		if( indexToSet >= 0 )
		{
			listElement = GetItemAt( indexToSet );
			if( !( listElement.IsVisible() ) )
			{
				if( SelectPriorVisible( indexToSet ) )
				{
					Prior();
					return true;
				}
				return false;
			}
			else
			{
				Prior();
				return true;
			}
		}
		return false;
	}

	protected function SelectNextVisible( currentIndex : Int32 ) : Bool
	{
		var indexToSet : Int32;
		var listElement : weak< inkWidget >;
		indexToSet = currentIndex + 1;
		if( indexToSet < Size() )
		{
			listElement = GetItemAt( indexToSet );
			if( !( listElement.IsVisible() ) )
			{
				if( SelectNextVisible( indexToSet ) )
				{
					Next();
					return true;
				}
				return false;
			}
			else
			{
				Next();
				return true;
			}
		}
		return false;
	}

}

