class DebugHubMenuGameController extends gameuiMenuGameController
{
	private var m_menuCtrl : weak< DebugHubMenuLogicController >;
	private var m_selectorCtrl : weak< hubSelectorController >;
	private var m_menuEventDispatcher : weak< inkMenuEventDispatcher >;
	private var m_player : weak< PlayerPuppet >;
	private var m_PDS : PlayerDevelopmentSystem;
	private var currencyListener : Uint32;
	private var characterCredListener : Uint32;
	private var characterLevelListener : Uint32;
	private var characterCurrentXPListener : Uint32;
	private var characterCredPointsListener : Uint32;
	private var m_Transaction : TransactionSystem;

	protected event OnInitialize()
	{
		m_menuCtrl = ( ( DebugHubMenuLogicController )( GetRootWidget().GetController() ) );
		m_selectorCtrl = m_menuCtrl.GetSelectorController();
		m_selectorCtrl.RegisterToCallback( 'OnSelectionChanged', this, 'OnMenuChanged' );
		RegisterToGlobalInputCallback( 'OnPostOnRelease', this, 'OnButtonRelease' );
	}

	protected event OnUninitialize()
	{
		m_selectorCtrl.UnregisterFromCallback( 'OnSelectionChanged', this, 'OnMenuChanged' );
		UnregisterFromGlobalInputCallback( 'OnPostOnRelease', this, 'OnButtonRelease' );
	}

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

	protected event OnButtonRelease( evt : inkPointerEvent )
	{
		if( evt.IsHandled() )
		{
			return false;
		}
		if( evt.IsAction( 'right_button' ) || evt.IsAction( 'prior_menu' ) )
		{
			m_selectorCtrl.Prior();
		}
		else if( evt.IsAction( 'left_button' ) || evt.IsAction( 'next_menu' ) )
		{
			m_selectorCtrl.Next();
		}
		else
		{
			return false;
		}
		evt.Handle();
	}

	protected event OnMenuChanged( index : Int32, value : String )
	{
		m_menuEventDispatcher.SpawnEvent( m_menuCtrl.GetEventNameByIndex( index ) );
	}

}

class DebugHubMenuLogicController extends inkLogicController
{
	private var m_selectorWidget : weak< inkWidget >;
	private var m_selectorCtrl : weak< hubSelectorController >;
	private var m_menusList : array< CName >;
	private var m_eventsList : array< CName >;
	private var m_defailtMenuName : CName;

	protected event OnInitialize()
	{
		m_selectorWidget = SpawnFromLocal( GetRootWidget(), 'selector' );
		m_selectorCtrl = ( ( hubSelectorController )( m_selectorWidget.GetController() ) );
		m_selectorWidget.SetAnchor( inkEAnchor.TopCenter );
		m_selectorWidget.SetAnchorPoint( Vector2( 0.5, 0.0 ) );
		if( IsClient() )
		{
			AddMenuItem( "CHARACTER SELECTION", 'OnSwitchToCpoCharacterSelection', 'cpo_character_selection' );
			AddMenuItem( "MUPPET LOADOUT SELECTION", 'OnSwitchToCpoMuppetLoadoutSelection', 'cpo_muppet_loadout_selection' );
		}
		AddMenuItem( "BUILDS", 'OnSwitchToBuilds', 'builds_panel' );
	}

	public function GetSelectorController() : hubSelectorController
	{
		return m_selectorCtrl;
	}

	public function SetDefaultMenu( defaultMenu : CName )
	{
		m_defailtMenuName = defaultMenu;
		if( m_selectorCtrl )
		{
			m_selectorCtrl.SetCurrIndex( m_menusList.FindFirst( m_defailtMenuName ) );
		}
	}

	public function GetEventNameByIndex( index : Int32 ) : CName
	{
		if( ( index >= 0 ) && ( index < m_eventsList.Size() ) )
		{
			return m_eventsList[ index ];
		}
		return m_defailtMenuName;
	}

	private function AddMenuItem( const menuLabel : ref< String >, eventName : CName, menuName : CName )
	{
		var menuData : MenuData;
		menuData.label = menuLabel;
		m_selectorCtrl.AddValue( menuLabel );
		m_selectorCtrl.AddMenuTab( menuData );
		m_menusList.PushBack( menuName );
		m_eventsList.PushBack( eventName );
	}

}

class DebugMenuScenario_HubMenu extends MenuScenario_BaseMenu
{
	private var defaultMenu : CName;
	default defaultMenu = 'builds_panel';
	private var cpoDefaultMenu : CName;
	default cpoDefaultMenu = 'builds_panel';

	private function SetDefaultMenu( menuName : CName )
	{
		if( IsClient() )
		{
			cpoDefaultMenu = menuName;
		}
		else
		{
			defaultMenu = menuName;
		}
	}

	private const function GetDefaultMenu() : CName
	{
		return ( ( IsClient() ) ? ( cpoDefaultMenu ) : ( defaultMenu ) );
	}

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		var currentState : inkMenusState;
		var hubMenu : weak< inkWidget >;
		var hubCtrl : DebugHubMenuLogicController;
		GetMenusState().OpenMenu( 'debug_hub_menu' );
		OnOpenBaseMenu( GetDefaultMenu() );
		currentState = GetMenusState();
		hubMenu = currentState.GetMenu( 'debug_hub_menu' );
		hubCtrl = ( ( DebugHubMenuLogicController )( hubMenu.GetController() ) );
		hubCtrl.SetDefaultMenu( GetDefaultMenu() );
	}

	protected event OnLeaveScenario( nextScenario : CName )
	{
		GetMenusState().CloseMenu( 'debug_hub_menu' );
		super.OnLeaveScenario( nextScenario );
	}

	protected event OnOpenBaseMenu( menuName : CName )
	{
		SwitchMenu( menuName );
		SetDefaultMenu( menuName );
	}

	protected event OnSwitchToCyberware()
	{
		OnOpenBaseMenu( 'temp_cyberware_equip' );
	}

	protected event OnSwitchToBuilds()
	{
		OnOpenBaseMenu( 'builds_panel' );
	}

	protected event OnSwitchToFastTravel()
	{
		OnOpenBaseMenu( 'fast_travel' );
	}

	protected event OnBack()
	{
		SwitchToScenario( 'MenuScenario_PauseMenu' );
	}

	protected event OnSwitchToCpoCharacterSelection()
	{
		OnOpenBaseMenu( 'cpo_character_selection' );
	}

	protected event OnSwitchToCpoMuppetLoadoutSelection()
	{
		OnOpenBaseMenu( 'cpo_muppet_loadout_selection' );
	}

}

