import class ControllerSettingsGameController extends gameuiMenuGameController
{
	[ category = "Tabs" ]
	private editable var m_generalInputPanel : inkWidgetRef;
	[ category = "Tabs" ]
	private editable var m_vehicleInputPanel : inkWidgetRef;
	[ category = "Tabs" ]
	private editable var m_vehicleCombatInputPanel : inkWidgetRef;
	[ category = "Tabs" ]
	private editable var m_selectorWidget : inkWidgetRef;
	[ category = "Input Modes" ]
	private editable var m_schemeLegacyRef : inkWidgetRef;
	[ category = "Input Modes" ]
	private editable var m_schemeAgileRef : inkWidgetRef;
	[ category = "Input Modes" ]
	private editable var m_schemeAlternativeRef : inkWidgetRef;
	[ category = "Input Modes" ]
	private editable var m_inputSettingSelectorRef : inkWidgetRef;
	[ category = "Settings" ]
	private editable var m_inputSettingGroupName : CName;
	default m_inputSettingGroupName = '/controls/controller';
	[ category = "Settings" ]
	private editable var m_inputSettingVarName : CName;
	default m_inputSettingVarName = 'InputSchemesMode';
	private editable var m_buttonHintsManagerRef : inkWidgetRef;
	private var m_buttonHintsController : weak< ButtonHints >;
	private var m_selectorCtrl : weak< ListController >;
	private var m_inputPanel : array< inkWidgetRef >;
	private var m_currentTab : Int32;
	private var m_inputSettingSelector : weak< SettingsSelectorControllerListString >;
	private var m_inputSettingsListener : InputSettingsVarListener;
	private var m_settings : UserSettings;
	private var m_inputSettingGroup : ConfigGroup;
	private var m_inputSettingVar : ConfigVar;

	protected event OnInitialize()
	{
		super.OnInitialize();
		RegisterToGlobalInputCallback( 'OnPostOnRelease', this, 'OnButtonRelease' );
		m_buttonHintsController = ( ( ButtonHints )( SpawnFromExternal( inkWidgetRef.Get( m_buttonHintsManagerRef ), R"base\gameplay\gui\common\buttonhints.inkwidget", 'Root' ).GetController() ) );
		m_buttonHintsController.AddButtonHint( 'back', GetLocalizedText( "Common-Access-Close" ) );
		m_selectorCtrl = ( ( ListController )( inkWidgetRef.GetController( m_selectorWidget ) ) );
		m_selectorCtrl.RegisterToCallback( 'OnItemActivated', this, 'OnMenuChanged' );
		m_settings = GetSystemRequestsHandler().GetUserSettings();
		if( m_settings.HasGroup( m_inputSettingGroupName ) )
		{
			m_inputSettingGroup = m_settings.GetGroup( m_inputSettingGroupName );
			if( m_inputSettingGroup.HasVar( m_inputSettingVarName ) )
			{
				m_inputSettingVar = m_inputSettingGroup.GetVar( m_inputSettingVarName );
			}
		}
		m_inputSettingSelector = ( ( SettingsSelectorControllerListString )( inkWidgetRef.GetController( m_inputSettingSelectorRef ) ) );
		m_inputSettingSelector.Setup( m_inputSettingVar, GetSystemRequestsHandler().IsPreGame() );
		m_inputSettingVar.SetEnabled( true );
		m_inputSettingsListener = new InputSettingsVarListener;
		m_inputSettingsListener.RegisterController( this );
		m_inputSettingsListener.Register( m_inputSettingVar.GetGroupPath() );
		RefreshInputsVisiblity();
		PopulatePanelsList();
	}

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

	protected event OnMenuChanged( index : Int32, target : ListItemController )
	{
		PlaySound( 'Button', 'OnPress' );
		inkWidgetRef.SetVisible( m_inputPanel[ m_currentTab ], false );
		inkWidgetRef.SetVisible( m_inputPanel[ index ], true );
		m_currentTab = index;
		inkWidgetRef.SetVisible( m_inputSettingSelectorRef, m_currentTab == 0 );
		if( m_currentTab == 0 )
		{
			SetCursorOverWidget( inkWidgetRef.Get( m_inputSettingSelectorRef ), 0.0, true );
		}
		ForceResetCursorType();
	}

	protected event OnButtonRelease( evt : inkPointerEvent )
	{
		var currentToggledIndex : Int32;
		var listSize : Int32;
		listSize = m_selectorCtrl.Size();
		if( evt.IsAction( 'prior_menu' ) )
		{
			currentToggledIndex = m_selectorCtrl.GetToggledIndex();
			if( currentToggledIndex < 1 )
			{
				m_selectorCtrl.SetToggledIndex( listSize - 1 );
			}
			else
			{
				m_selectorCtrl.SetToggledIndex( currentToggledIndex - 1 );
			}
		}
		else if( evt.IsAction( 'next_menu' ) )
		{
			currentToggledIndex = m_selectorCtrl.GetToggledIndex();
			if( currentToggledIndex >= ( m_selectorCtrl.Size() - 1 ) )
			{
				m_selectorCtrl.SetToggledIndex( 0 );
			}
			else
			{
				m_selectorCtrl.SetToggledIndex( currentToggledIndex + 1 );
			}
		}
	}

	private function RefreshInputsVisiblity()
	{
		var settingIndex : Int32;
		settingIndex = ( ( ConfigVarListString )( m_inputSettingVar ) ).GetIndex();
		switch( settingIndex )
		{
			case 0:
				inkWidgetRef.SetVisible( m_schemeLegacyRef, true );
			inkWidgetRef.SetVisible( m_schemeAgileRef, false );
			inkWidgetRef.SetVisible( m_schemeAlternativeRef, false );
			break;
			case 1:
				inkWidgetRef.SetVisible( m_schemeLegacyRef, false );
			inkWidgetRef.SetVisible( m_schemeAgileRef, true );
			inkWidgetRef.SetVisible( m_schemeAlternativeRef, false );
			break;
			case 2:
				inkWidgetRef.SetVisible( m_schemeLegacyRef, false );
			inkWidgetRef.SetVisible( m_schemeAgileRef, false );
			inkWidgetRef.SetVisible( m_schemeAlternativeRef, true );
			break;
		}
	}

	private function PopulatePanelsList()
	{
		var newData : ListItemData;
		newData = new ListItemData;
		newData.label = GetLocalizedTextByKey( 'UI-Settings-ButtonMappings-Categories-General' );
		m_selectorCtrl.PushData( newData );
		m_inputPanel.PushBack( m_generalInputPanel );
		inkWidgetRef.SetVisible( m_inputPanel[ 0 ], false );
		newData = new ListItemData;
		newData.label = GetLocalizedTextByKey( 'UI-Settings-ButtonMappings-Categories-Vehicle' );
		m_selectorCtrl.PushData( newData );
		m_inputPanel.PushBack( m_vehicleInputPanel );
		inkWidgetRef.SetVisible( m_inputPanel[ 1 ], false );
		newData = new ListItemData;
		newData.label = GetLocalizedTextByKey( 'Story-base-journal-codex-tutorials-Vehiclecombat_title' );
		m_selectorCtrl.PushData( newData );
		m_inputPanel.PushBack( m_vehicleCombatInputPanel );
		inkWidgetRef.SetVisible( m_inputPanel[ 2 ], false );
		m_selectorCtrl.Refresh();
		m_selectorCtrl.SetToggledIndex( 0 );
		m_currentTab = 0;
	}

	public function OnVarModified( groupPath : CName, varName : CName, varType : ConfigVarType, reason : ConfigChangeReason )
	{
		m_inputSettingSelector.Refresh();
		RefreshInputsVisiblity();
	}

}

class InputSettingsVarListener extends ConfigVarListener
{
	private var m_ctrl : weak< ControllerSettingsGameController >;

	public function RegisterController( ctrl : ControllerSettingsGameController )
	{
		m_ctrl = ctrl;
	}

	public override function OnVarModified( groupPath : CName, varName : CName, varType : ConfigVarType, reason : ConfigChangeReason )
	{
		m_ctrl.OnVarModified( groupPath, varName, varType, reason );
	}

}

import class gameuiStadiaControllersGameController extends gameuiMenuGameController
{
	private editable var m_buttonHintsManagerRef : inkWidgetRef;
	private var m_buttonHintsController : weak< ButtonHints >;

	protected event OnInitialize()
	{
		super.OnInitialize();
		m_buttonHintsController = ( ( ButtonHints )( SpawnFromExternal( inkWidgetRef.Get( m_buttonHintsManagerRef ), R"base\gameplay\gui\common\buttonhints.inkwidget", 'Root' ).GetController() ) );
		m_buttonHintsController.AddButtonHint( 'back', GetLocalizedText( "Common-Access-Close" ) );
	}

}

