import class gameuiMenuItemListGameController extends gameuiSaveHandlingController
{
	protected editable var m_menuList : inkCompoundRef;
	protected var m_menuEventDispatcher : weak< inkMenuEventDispatcher >;
	protected var m_menuListController : weak< ListController >;

	protected import function CanExitGame() : Bool;
	protected import function ExitGame();
	protected import function GotoMainMenu();

	protected event OnInitialize()
	{
		m_menuListController = ( ( ListController )( inkCompoundRef.GetController( m_menuList ) ) );
		m_menuListController.RegisterToCallback( 'OnItemActivated', this, 'OnMenuItemActivated' );
		ShowActionsList();
	}

	protected event OnUninitialize()
	{
		m_menuListController.UnregisterFromCallback( 'OnItemActivated', this, 'OnMenuItemActivated' );
	}

	protected event OnSetMenuEventDispatcher( menuEventDispatcher : weak< inkMenuEventDispatcher > )
	{
		m_menuEventDispatcher = menuEventDispatcher;
	}

	private virtual function PopulateMenuItemList() {}

	protected virtual function HandleMenuItemActivate( data : PauseMenuListItemData ) : Bool
	{
		switch( data.action )
		{
			case PauseMenuAction.OpenSubMenu:
				m_menuEventDispatcher.SpawnEvent( data.eventName );
			return true;
			case PauseMenuAction.QuickSave:
				GetSystemRequestsHandler().QuickSave();
			m_menuEventDispatcher.SpawnEvent( 'OnClosePauseMenu' );
			return true;
			case PauseMenuAction.ExitGame:
				ExitGame();
			return true;
			case PauseMenuAction.ExitToMainMenu:
				GotoMainMenu();
			return true;
		}
		return false;
	}

	private virtual function ShouldAllowExitGameMenuItem() : Bool
	{
		return true;
	}

	protected function AddMenuItem( const label : ref< String >, spawnEvent : CName )
	{
		var data : PauseMenuListItemData;
		data = new PauseMenuListItemData;
		data.label = label;
		data.eventName = spawnEvent;
		data.action = PauseMenuAction.OpenSubMenu;
		m_menuListController.PushData( data );
	}

	protected function AddMenuItem( const label : ref< String >, action : PauseMenuAction )
	{
		var data : PauseMenuListItemData;
		data = new PauseMenuListItemData;
		data.label = label;
		data.action = action;
		m_menuListController.PushData( data );
	}

	protected function Clear()
	{
		m_menuListController.Clear();
	}

	protected virtual function ShowActionsList()
	{
		Clear();
		PopulateMenuItemList();
		if( ShouldAllowExitGameMenuItem() && CanExitGame() )
		{
			AddMenuItem( GetLocalizedText( "UI-Labels-CloseGame" ), PauseMenuAction.ExitGame );
		}
		m_menuListController.Refresh();
	}

	protected event OnMenuItemActivated( index : Int32, target : ListItemController )
	{
		var data : PauseMenuListItemData;
		data = ( ( PauseMenuListItemData )( target.GetData() ) );
		PlaySound( 'Button', 'OnPress' );
		HandleMenuItemActivate( data );
	}

}

