class UpdateHDRCalibrationScreenEvt extends Event
{
}

class HDRSettingsVarListener extends ConfigVarListener
{

	public function RegisterController( ctrl : HDRSettingsGameController )
	{
		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< HDRSettingsGameController >;
}

import class HDRSettingsGameController extends gameuiMenuGameController
{
	private var s_maxBrightnessGroup : CName;
	private var s_paperWhiteGroup : CName;
	private var s_toneMappingeGroup : CName;
	private var s_saturationGroup : CName;
	private var s_calibrationImageDay : CName;
	private var s_calibrationImageNight : CName;
	private var s_currentCalibrationImage : CName;
	private editable var m_paperWhiteOptionSelector : inkCompoundRef;
	private editable var m_maxBrightnessOptionSelector : inkCompoundRef;
	private editable var m_toneMappingOptionSelector : inkCompoundRef;
	private editable var m_saturationOptionSelector : inkCompoundRef;
	private editable var m_targetImageWidget : inkWidgetRef;
	private var m_menuEventDispatcher : weak< inkMenuEventDispatcher >;
	private var m_settings : UserSettings;
	private var m_settingsListener : HDRSettingsVarListener;
	private var m_SettingsElements : array< weak< SettingsSelectorController > >;
	private var m_isPreGame : Bool;
	private var m_calibrationImagesCycleAnimDef : inkAnimDef;
	private var m_calibrationImagesCycleProxy : inkAnimProxy;

	protected event OnInitialize()
	{
		var updateHDRCalibrationScreenEvt : UpdateHDRCalibrationScreenEvt;
		s_maxBrightnessGroup = '/video/display';
		s_paperWhiteGroup = '/video/display';
		s_toneMappingeGroup = '/video/display';
		s_saturationGroup = '/video/display';
		s_calibrationImageDay = 'hdr_day';
		s_calibrationImageNight = 'hdr_night';
		m_settings = GetSystemRequestsHandler().GetUserSettings();
		m_isPreGame = GetSystemRequestsHandler().IsPreGame();
		m_settingsListener = new HDRSettingsVarListener;
		m_settingsListener.RegisterController( this );
		m_settingsListener.Register( s_paperWhiteGroup );
		m_settingsListener.Register( s_maxBrightnessGroup );
		m_settingsListener.Register( s_toneMappingeGroup );
		m_settingsListener.Register( s_saturationGroup );
		RegisterToGlobalInputCallback( 'OnPostOnRelease', this, 'OnButtonRelease' );
		m_SettingsElements.Clear();
		SetOptionSelector( 'MaxMonitorBrightness' );
		SetOptionSelector( 'PaperWhiteLevel' );
		SetOptionSelector( 'TonemappingMidpoint' );
		SetOptionSelector( 'Saturation' );
		PrepareHDRCycleAnimations();
		s_currentCalibrationImage = s_calibrationImageNight;
		updateHDRCalibrationScreenEvt = new UpdateHDRCalibrationScreenEvt;
		QueueEvent( updateHDRCalibrationScreenEvt );
	}

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

	protected event OnUpdateHDRCalibrationScreen( evt : UpdateHDRCalibrationScreenEvt )
	{
		SetTexturePart( s_currentCalibrationImage );
	}

	private function SetOptionSelector( optionName : CName )
	{
		var option : ConfigVar;
		var selector : weak< SettingsSelectorController >;
		var selectorWidget : inkCompoundRef;
		if( optionName == 'MaxMonitorBrightness' )
		{
			selectorWidget = m_maxBrightnessOptionSelector;
		}
		else if( optionName == 'PaperWhiteLevel' )
		{
			selectorWidget = m_paperWhiteOptionSelector;
		}
		else if( optionName == 'TonemappingMidpoint' )
		{
			selectorWidget = m_toneMappingOptionSelector;
		}
		else if( optionName == 'Saturation' )
		{
			selectorWidget = m_saturationOptionSelector;
		}
		option = m_settings.GetVar( s_maxBrightnessGroup, optionName );
		if( ( ( m_isPreGame ) ? ( option.IsInPreGame() ) : ( option.IsInGame() ) ) )
		{
			selector = ( ( SettingsSelectorController )( inkCompoundRef.Get( selectorWidget ).GetController() ) );
			if( selector )
			{
				selector.Setup( option, m_isPreGame );
				m_SettingsElements.PushBack( selector );
			}
		}
	}

	public 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();
			}
		}
	}

	private function PrepareHDRCycleAnimations()
	{
		var alphaInterpolator : inkAnimTransparency;
		var options : inkAnimOptions;
		options.loopType = inkanimLoopType.Cycle;
		options.loopInfinite = true;
		m_calibrationImagesCycleAnimDef = new inkAnimDef;
		alphaInterpolator = new inkAnimTransparency;
		alphaInterpolator.SetStartTransparency( 0.002 );
		alphaInterpolator.SetEndTransparency( 0.0025 );
		alphaInterpolator.SetDuration( 5.0 );
		alphaInterpolator.SetStartDelay( 0.25 );
		alphaInterpolator.SetType( inkanimInterpolationType.Linear );
		alphaInterpolator.SetMode( inkanimInterpolationMode.EasyIn );
		m_calibrationImagesCycleAnimDef.AddInterpolator( alphaInterpolator );
		m_calibrationImagesCycleProxy = inkWidgetRef.PlayAnimationWithOptions( m_targetImageWidget, m_calibrationImagesCycleAnimDef, options );
		m_calibrationImagesCycleProxy.RegisterToCallback( inkanimEventType.OnEndLoop, this, 'OnCalibrationImageEndLoop' );
		m_calibrationImagesCycleProxy.RegisterToCallback( inkanimEventType.OnStart, this, 'OnCalibrationImageAnimStart' );
	}

	protected event OnCalibrationImageAnimStart( anim : inkAnimProxy )
	{
		SetHDRCalibrationScreen( true );
	}

	protected event OnCalibrationImageEndLoop( anim : inkAnimProxy )
	{
		if( s_currentCalibrationImage == s_calibrationImageNight )
		{
			s_currentCalibrationImage = s_calibrationImageDay;
		}
		else
		{
			s_currentCalibrationImage = s_calibrationImageNight;
		}
		SetTexturePart( s_currentCalibrationImage );
	}

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

	private import function SetTexturePart( partName : CName );
	private import function SetHDRCalibrationScreen( enabled : Bool );
	private import function SetRenderGameInBackground( enabled : Bool );
}

