class MenuScenario_PreGameSubMenu extends inkMenuScenario
{
	protected var m_prevScenario : CName;
	protected var m_currSubMenuName : CName;

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		if( m_prevScenario == '' )
		{
			m_prevScenario = prevScenario;
		}
	}

	protected event OnLeaveScenario( nextScenario : CName )
	{
		if( nextScenario == m_prevScenario )
		{
			m_prevScenario = '';
		}
	}

	protected function OpenSubMenu( menuName : CName, optional userData : IScriptable )
	{
		var menuState : weak< inkMenusState >;
		menuState = GetMenusState();
		if( m_currSubMenuName != '' )
		{
			menuState.CloseMenu( m_currSubMenuName );
		}
		m_currSubMenuName = menuName;
		menuState.OpenMenu( m_currSubMenuName, userData );
		OnSubmenuOpen();
	}

	protected virtual function OnSubmenuOpen() {}

	protected function CloseSubMenu()
	{
		var menuState : weak< inkMenusState >;
		menuState = GetMenusState();
		if( m_currSubMenuName != '' )
		{
			menuState.CloseMenu( m_currSubMenuName );
		}
		m_currSubMenuName = '';
	}

	protected event OnRequestPatchNotes()
	{
		var menuState : weak< inkMenusState >;
		menuState = GetMenusState();
		menuState.DispatchEvent( 'singleplayer_menu', 'OnOpenPatchNotes' );
	}

	protected event OnRequetClosePatchNotes()
	{
		var menuState : weak< inkMenusState >;
		menuState = GetMenusState();
		menuState.DispatchEvent( 'singleplayer_menu', 'OnClosePatchNotes' );
	}

	protected event OnRequestShowMainMenuTooltip( userData : IScriptable )
	{
		var menuState : weak< inkMenusState >;
		menuState = GetMenusState();
		menuState.DispatchEvent( 'singleplayer_menu', 'OnShowMainMenuTooltip', userData );
	}

	protected event OnRequestHideMainMenuTooltip()
	{
		var menuState : weak< inkMenusState >;
		menuState = GetMenusState();
		menuState.DispatchEvent( 'singleplayer_menu', 'OnHideMainMenuTooltip' );
	}

	protected event OnRequetCloseExpansionPopup()
	{
		var menuState : weak< inkMenusState >;
		menuState = GetMenusState();
		menuState.DispatchEvent( 'singleplayer_menu', 'OnCloseExpansionPopup' );
	}

	protected event OnRequetPurchaseDisabledError()
	{
		var menuState : weak< inkMenusState >;
		menuState = GetMenusState();
		menuState.DispatchEvent( 'singleplayer_menu', 'OnPurchaseDisabledError' );
	}

	protected event OnBack()
	{
		var menuState : weak< inkMenusState >;
		menuState = GetMenusState();
		if( m_currSubMenuName != '' )
		{
			if( !( menuState.DispatchEvent( m_currSubMenuName, 'OnBack' ) ) )
			{
				CloseSubMenu();
			}
		}
		else
		{
			SwitchToScenario( m_prevScenario );
		}
	}

	protected event OnHandleEngagementScreen( evt : ShowEngagementScreen )
	{
		var menuState : weak< inkMenusState >;
		menuState = GetMenusState();
		if( menuState )
		{
			if( m_currSubMenuName != '' )
			{
				menuState.DispatchEvent( m_currSubMenuName, 'OnBack' );
			}
			menuState.CloseAllMenus();
		}
		CloseSubMenu();
		if( evt.show )
		{
			OnSwitchToEngagementScreen();
		}
		else
		{
			SwitchToScenario( 'MenuScenario_SingleplayerMenu' );
			GetMenusState().OpenMenu( 'singleplayer_menu' );
		}
	}

	protected event OnSwitchToEngagementScreen()
	{
		SwitchToScenario( 'MenuScenario_EngagementScreen' );
		OpenSubMenu( 'engagement_screen' );
	}

	protected event OnHandleInitializeUserScreen( evt : ShowInitializeUserScreen )
	{
		var menuState : weak< inkMenusState >;
		var patchNotesCheckData : PatchNotesCheckData;
		var requestsHandler : weak< inkISystemRequestsHandler >;
		if( evt.show )
		{
			OnSwitchToInitializeUserScreen();
		}
		else
		{
			requestsHandler = GetSystemRequestsHandler();
			OnCloseInitializeUserScreen();
			requestsHandler.UpdateLaunchCounter();
			requestsHandler.RequestTelemetryConsent( false );
			patchNotesCheckData = new PatchNotesCheckData;
			patchNotesCheckData.m_ownExpansion = requestsHandler.IsAdditionalContentEnabled( 'EP1' ) || requestsHandler.IsAdditionalContentEnabled( 'EP1' );
			menuState = GetMenusState();
			menuState.DispatchEvent( 'singleplayer_menu', 'OnCheckPatchNotes', patchNotesCheckData );
			menuState.DispatchEvent( 'singleplayer_menu', 'OnShowOneTimeMessages' );
			if( requestsHandler.ShouldDisplayGog() )
			{
				DisplayGog();
			}
		}
	}

	protected virtual function DisplayGog() {}

	protected event OnSwitchToInitializeUserScreen()
	{
		OpenSubMenu( 'initialize_user_screen' );
	}

	protected event OnCloseInitializeUserScreen()
	{
		var data : SingleplayerMenuData;
		if( m_currSubMenuName == 'initialize_user_screen' )
		{
			CloseSubMenu();
			data = new SingleplayerMenuData;
			data.mainMenuShownFirstTime = true;
			GetMenusState().OpenMenu( 'singleplayer_menu', data );
		}
	}

}

class MenuScenario_EngagementScreen extends MenuScenario_PreGameSubMenu
{

	protected event OnBack() {}
}

class MenuScenario_SingleplayerMenu extends MenuScenario_PreGameSubMenu
{
	private var m_expansionHintShown : Bool;
	default m_expansionHintShown = false;

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		var menuState : weak< inkMenusState >;
		var data : SingleplayerMenuData;
		data = new SingleplayerMenuData;
		menuState = GetMenusState();
		if( prevScenario == '' )
		{
			menuState.ShowMenus( true );
		}
		data.showExpansionHint = m_expansionHintShown;
		menuState.OpenMenu( 'singleplayer_menu', data );
	}

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

	protected event OnLoadGame()
	{
		CloseSubMenu();
		SwitchToScenario( 'MenuScenario_LoadGame' );
	}

	protected event OnSwitchToSettings()
	{
		CloseSubMenu();
		SwitchToScenario( 'MenuScenario_Settings' );
	}

	protected event OnSwitchToCredits()
	{
		CloseSubMenu();
		SwitchToScenario( 'MenuScenario_Credits' );
	}

	protected event OnSwitchToCreditsEp1()
	{
		CloseSubMenu();
		SwitchToScenario( 'MenuScenario_CreditsEp1' );
	}

	protected event OnCreditsPicker()
	{
		CloseSubMenu();
		SwitchToScenario( 'MenuScenario_CreditsPicker' );
	}

	protected event OnBuyGame()
	{
		TrialHelper.ShowUpgradeTrialVersionCommerceDialog();
	}

	protected event OnSwitchToDlc()
	{
		CloseSubMenu();
		SwitchToScenario( 'MenuScenario_DLC' );
	}

	protected override function DisplayGog()
	{
		OnGOGProfile();
	}

	protected event OnGOGProfile()
	{
		var menuWidget : inkWidget;
		var gogContainer : weak< inkCompoundWidget >;
		var gogPopupWidget : inkWidget;
		var gogPopupController : weak< GOGProfileLogicController >;
		menuWidget = GetMenusState().OpenMenu( 'singleplayer_menu' );
		gogContainer = ( ( inkCompoundWidget )( menuWidget.GetController().GetWidget( 'GogPanel/GogContainer' ) ) );
		if( gogContainer )
		{
			if( gogContainer.GetNumChildren() )
			{
				gogContainer.RemoveAllChildren();
			}
			else
			{
				gogPopupWidget = menuWidget.GetController().SpawnFromExternal( gogContainer, R"base\gameplay\gui\fullscreen\main_menu\gog_popup.inkwidget", 'Root' );
				if( gogPopupWidget != NULL )
				{
					gogPopupController = ( ( GOGProfileLogicController )( gogPopupWidget.GetController() ) );
					if( gogPopupController != NULL )
					{
						gogPopupController.SetMenuState( EGOGMenuState.MainMenu );
					}
				}
			}
		}
	}

	protected event OnExpansionHint()
	{
		m_expansionHintShown = true;
	}

	protected event OnCloseSettings()
	{
		if( m_currSubMenuName == 'settings_main' )
		{
			CloseSubMenu();
		}
	}

	protected event OnDebug()
	{
		CloseSubMenu();
		SwitchToScenario( 'MenuScenario_NewGame' );
	}

	protected event OnNewGame()
	{
		CloseSubMenu();
		if( GetSystemRequestsHandler().IsAdditionalContentEnabled( 'EP1' ) )
		{
			SwitchToScenario( 'MenuScenario_ExpansionNewGame' );
		}
		else
		{
			SwitchToScenario( 'MenuScenario_Difficulty' );
		}
	}

	protected override function OnSubmenuOpen()
	{
		GetMenusState().CloseMenu( 'singleplayer_menu' );
	}

	protected event OnMainMenuBack()
	{
		if( m_currSubMenuName != '' )
		{
			CloseSubMenu();
			GetMenusState().OpenMenu( 'singleplayer_menu' );
		}
		else
		{
			SwitchToScenario( m_prevScenario );
		}
	}

}

class MenuScenario_Settings extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		super.OnEnterScenario( prevScenario, userData );
		GetMenusState().OpenMenu( 'settings_main', userData );
	}

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

	protected event OnSwitchToBrightnessSettings()
	{
		OpenSubMenu( 'brightness_settings' );
	}

	protected event OnSwitchToHDRSettings()
	{
		OpenSubMenu( 'hdr_settings' );
	}

	protected event OnSwitchToControllerPanel()
	{
		var menuState : weak< inkMenusState >;
		menuState = GetMenusState();
		OpenSubMenu( menuState.GetControllerMenuName() );
	}

	protected override function OnSubmenuOpen()
	{
		GetMenusState().CloseMenu( 'settings_main' );
	}

	protected event OnSettingsBack()
	{
		if( m_currSubMenuName != '' )
		{
			CloseSubMenu();
			GetMenusState().OpenMenu( 'settings_main' );
		}
		else
		{
			CloseSettings( false );
		}
	}

	protected event OnCloseSettingsScreen()
	{
		CloseSettings( true );
	}

	private function CloseSettings( forceCloseSettings : Bool )
	{
		var menuState : weak< inkMenusState >;
		menuState = GetMenusState();
		if( forceCloseSettings )
		{
			menuState.CloseMenu( 'settings_main' );
			if( m_currSubMenuName != '' )
			{
				if( !( menuState.DispatchEvent( m_currSubMenuName, 'OnBack' ) ) )
				{
					CloseSubMenu();
				}
			}
			else
			{
				SwitchToScenario( m_prevScenario );
			}
		}
		else
		{
			menuState.DispatchEvent( 'settings_main', 'OnBack' );
		}
	}

	protected event OnMainMenuBack()
	{
		SwitchToScenario( m_prevScenario );
	}

}

class MenuScenario_CreditsPicker extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		var menuState : weak< inkMenusState >;
		super.OnEnterScenario( prevScenario, userData );
		menuState = GetMenusState();
		menuState.OpenMenu( 'credits_picker' );
	}

	protected event OnLeaveScenario( nextScenario : CName )
	{
		var menuState : weak< inkMenusState >;
		super.OnLeaveScenario( nextScenario );
		menuState = GetMenusState();
		menuState.CloseMenu( 'credits_picker' );
	}

	protected event OnSwitchToCredits()
	{
		CloseSubMenu();
		SwitchToScenario( 'MenuScenario_Credits' );
	}

	protected event OnSwitchToCreditsEp1()
	{
		CloseSubMenu();
		SwitchToScenario( 'MenuScenario_CreditsEp1' );
	}

}

class MenuScenario_Credits extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		var data : CreditsData;
		data = new CreditsData;
		data.isFinalBoards = false;
		data.showRewardPrompt = false;
		super.OnEnterScenario( prevScenario, userData );
		OpenSubMenu( 'credits', data );
	}

	protected event OnLeaveScenario( nextScenario : CName )
	{
		super.OnLeaveScenario( nextScenario );
	}

	protected event OnBack()
	{
		CloseSubMenu();
		SwitchToScenario( m_prevScenario );
	}

	protected event OnSettingsBack()
	{
		CloseSubMenu();
		SwitchToScenario( m_prevScenario );
	}

}

class MenuScenario_CreditsEp1 extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		var data : CreditsData;
		data = new CreditsData;
		data.isFinalBoards = false;
		data.showRewardPrompt = false;
		super.OnEnterScenario( prevScenario, userData );
		OpenSubMenu( 'credits_ep1', data );
	}

	protected event OnLeaveScenario( nextScenario : CName )
	{
		super.OnLeaveScenario( nextScenario );
	}

	protected event OnBack()
	{
		CloseSubMenu();
		SwitchToScenario( m_prevScenario );
	}

	protected event OnSettingsBack()
	{
		CloseSubMenu();
		SwitchToScenario( m_prevScenario );
	}

}

class MenuScenario_NewGame extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		super.OnEnterScenario( prevScenario, userData );
		GetMenusState().OpenMenu( 'new_game_menu' );
	}

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

}

class MenuScenario_LoadGame extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		super.OnEnterScenario( prevScenario, userData );
		GetMenusState().OpenMenu( 'load_game_menu' );
	}

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

	protected event OnMainMenuBack()
	{
		SwitchToScenario( m_prevScenario );
	}

}

class MenuScenario_MultiplayerMenu extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		var menuState : weak< inkMenusState >;
		menuState = GetMenusState();
		if( prevScenario == '' )
		{
			menuState.ShowMenus( true );
		}
		menuState.OpenMenu( 'multiplayer_menu' );
	}

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

	protected event OnFindServers()
	{
		SwitchToScenario( 'MenuScenario_FindServers' );
	}

	protected event OnPlayRecordedSession()
	{
		SwitchToScenario( 'MenuScenario_PlayRecordedSession' );
	}

	protected event OnBoothMode()
	{
		SwitchToScenario( 'MenuScenario_BoothMode' );
	}

}

class MenuScenario_FindServers extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		super.OnEnterScenario( prevScenario, userData );
		GetMenusState().OpenMenu( 'find_servers' );
	}

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

}

class MenuScenario_PlayRecordedSession extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		super.OnEnterScenario( prevScenario, userData );
		GetMenusState().OpenMenu( 'play_recorded_session' );
	}

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

}

class MenuScenario_BoothMode extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		GetMenusState().OpenMenu( 'booth_mode' );
	}

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

}

class MenuScenario_LifePathSelection extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		var menuState : weak< inkMenusState >;
		super.OnEnterScenario( prevScenario, userData );
		menuState = GetMenusState();
		menuState.OpenMenu( 'character_customization_background' );
		menuState.OpenMenu( 'life_path_selection' );
	}

	protected event OnLeaveScenario( nextScenario : CName )
	{
		var menuState : weak< inkMenusState >;
		super.OnLeaveScenario( nextScenario );
		menuState = GetMenusState();
		if( nextScenario != 'MenuScenario_BodyTypeSelection' )
		{
			menuState.CloseMenu( 'character_customization_background' );
		}
		menuState.CloseMenu( 'life_path_selection' );
	}

	protected event OnAccept()
	{
		SwitchToScenario( 'MenuScenario_BodyTypeSelection' );
	}

}

class MenuScenario_BodyTypeSelection extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		var menuState : weak< inkMenusState >;
		super.OnEnterScenario( prevScenario, userData );
		menuState = GetMenusState();
		menuState.OpenMenu( 'gender_selection' );
	}

	protected event OnLeaveScenario( nextScenario : CName )
	{
		var menuState : weak< inkMenusState >;
		super.OnLeaveScenario( nextScenario );
		menuState = GetMenusState();
		menuState.CloseMenu( 'gender_selection' );
	}

	protected event OnAccept()
	{
		SwitchToScenario( 'MenuScenario_CharacterCustomization' );
	}

}

class MenuScenario_CharacterCustomization extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		var menuState : weak< inkMenusState >;
		var morphMenuUserData : MorphMenuUserData;
		morphMenuUserData = new MorphMenuUserData;
		morphMenuUserData.m_optionsListInitialized = prevScenario == 'MenuScenario_StatsAdjustment';
		super.OnEnterScenario( prevScenario, userData );
		menuState = GetMenusState();
		menuState.OpenMenu( 'player_puppet' );
		menuState.OpenMenu( 'character_customization', morphMenuUserData );
	}

	protected event OnLeaveScenario( nextScenario : CName )
	{
		var menuState : weak< inkMenusState >;
		super.OnLeaveScenario( nextScenario );
		menuState = GetMenusState();
		if( nextScenario != 'MenuScenario_StatsAdjustment' )
		{
			menuState.CloseMenu( 'player_puppet' );
		}
		menuState.CloseMenu( 'character_customization' );
	}

	protected event OnAccept()
	{
		SwitchToScenario( 'MenuScenario_StatsAdjustment' );
	}

}

class MenuScenario_StatsAdjustment extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		var menuState : weak< inkMenusState >;
		var statsMenuUserData : StatsMenuUserData;
		statsMenuUserData = new StatsMenuUserData;
		statsMenuUserData.m_menuVisited = prevScenario == 'MenuScenario_Summary';
		super.OnEnterScenario( prevScenario, userData );
		menuState = GetMenusState();
		menuState.OpenMenu( 'player_puppet' );
		menuState.OpenMenu( 'statistics_adjustment', statsMenuUserData );
	}

	protected event OnLeaveScenario( nextScenario : CName )
	{
		var menuState : weak< inkMenusState >;
		super.OnLeaveScenario( nextScenario );
		menuState = GetMenusState();
		menuState.CloseMenu( 'statistics_adjustment' );
	}

	protected event OnAccept()
	{
		SwitchToScenario( 'MenuScenario_Summary' );
	}

}

class MenuScenario_Summary extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		var menuState : weak< inkMenusState >;
		super.OnEnterScenario( prevScenario, userData );
		menuState = GetMenusState();
		menuState.OpenMenu( 'player_puppet' );
		menuState.OpenMenu( 'character_customization_summary' );
	}

	protected event OnLeaveScenario( nextScenario : CName )
	{
		var menuState : weak< inkMenusState >;
		super.OnLeaveScenario( nextScenario );
		menuState = GetMenusState();
		menuState.CloseMenu( 'character_customization_summary' );
	}

	protected event OnAccept()
	{
		GetMenusState().ShowMenus( false );
	}

}

class MenuScenario_Difficulty extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		var menuState : weak< inkMenusState >;
		super.OnEnterScenario( prevScenario, userData );
		menuState = GetMenusState();
		menuState.OpenMenu( 'singleplayer_menu_difficulty' );
	}

	protected event OnLeaveScenario( nextScenario : CName )
	{
		var menuState : weak< inkMenusState >;
		super.OnLeaveScenario( nextScenario );
		menuState = GetMenusState();
		menuState.CloseMenu( 'character_customization_background' );
		menuState.CloseMenu( 'singleplayer_menu_difficulty' );
	}

	protected event OnAccept()
	{
		SwitchToScenario( 'MenuScenario_LifePathSelection' );
	}

}

class MenuScenario_ExpansionNewGame extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		var menuState : weak< inkMenusState >;
		super.OnEnterScenario( prevScenario, userData );
		menuState = GetMenusState();
		menuState.OpenMenu( 'expansion_new_game_menu' );
	}

	protected event OnLeaveScenario( nextScenario : CName )
	{
		var menuState : weak< inkMenusState >;
		super.OnLeaveScenario( nextScenario );
		menuState = GetMenusState();
		menuState.CloseMenu( 'character_customization_background' );
		menuState.CloseMenu( 'expansion_new_game_menu' );
	}

	protected event OnAccept()
	{
		SwitchToScenario( 'MenuScenario_Difficulty' );
	}

}

class MenuScenario_DLC extends MenuScenario_PreGameSubMenu
{

	protected event OnEnterScenario( prevScenario : CName, userData : IScriptable )
	{
		super.OnEnterScenario( prevScenario, userData );
		OpenSubMenu( 'dlc_menu' );
	}

	protected event OnLeaveScenario( nextScenario : CName )
	{
		super.OnLeaveScenario( nextScenario );
	}

	protected event OnSettingsBack()
	{
		CloseSubMenu();
		SwitchToScenario( m_prevScenario );
	}

}

