class BrightnessSettingsVarListener extends ConfigVarListener
{

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

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


	private var m_ctrl : weak< BrightnessSettingsGameController >;
}

class BrightnessSettingsGameController extends gameuiMenuGameController
{
	private var s_brightnessGroup : CName;
	private editable var m_settingsOptionsList : inkCompoundRef;
	private var m_menuEventDispatcher : weak< inkMenuEventDispatcher >;
	private var m_settings : UserSettings;
	private var m_settingsListener : BrightnessSettingsVarListener;
	private var m_SettingsElements : array< weak< SettingsSelectorController > >;
	private var m_isPreGame : Bool;

	protected event OnInitialize()
	{
		s_brightnessGroup = '/video/display';
		m_settings = GetSystemRequestsHandler().GetUserSettings();
		m_isPreGame = GetSystemRequestsHandler().IsPreGame();
		m_settingsListener = new BrightnessSettingsVarListener;
		m_settingsListener.RegisterController( this );
		m_settingsListener.Register( s_brightnessGroup );
		RegisterToGlobalInputCallback( 'OnPostOnRelease', this, 'OnButtonRelease' );
		PopulateSettings();
	}

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

	private function PopulateSettings()
	{
		var option : ConfigVar;
		var selector : weak< SettingsSelectorController >;
		m_SettingsElements.Clear();
		inkCompoundRef.RemoveAllChildren( m_settingsOptionsList );
		option = m_settings.GetVar( s_brightnessGroup, 'Gamma' );
		if( ( ( m_isPreGame ) ? ( option.IsInPreGame() ) : ( option.IsInGame() ) ) )
		{
			selector = ( ( SettingsSelectorController )( SpawnFromExternal( inkCompoundRef.Get( m_settingsOptionsList ), R"base\gameplay\gui\fullscreen\settings\settings_main.inkwidget", 'settingsSelectorFloat' ).GetController() ) );
			if( selector )
			{
				selector.Setup( option, m_isPreGame );
				m_SettingsElements.PushBack( selector );
			}
		}
	}

	public export function OnVarModified( groupPath : CName, varName : CName, varType : ConfigVarType, reason : ConfigChangeReason )
	{
		var i : Int32;
		var size : Int32;
		var item : SettingsSelectorController;
		size = m_SettingsElements.Size();
		for( i = 0; i < size; i += 1 )
		{
			item = m_SettingsElements[ i ];
			if( item.GetGroupPath() == groupPath && item.GetVarName() == varName )
			{
				m_SettingsElements[ i ].Refresh();
			}
		}
	}

	protected event OnSetMenuEventDispatcher( menuEventDispatcher : weak< inkMenuEventDispatcher > )
	{
		m_menuEventDispatcher = menuEventDispatcher;
		m_menuEventDispatcher.RegisterToEvent( 'OnBack', this, 'OnBack' );
	}

}

