class DlcDescriptionData extends inkUserData
{
	var m_title : CName;
	var m_description : CName;
	var m_guide : CName;
	var m_imagePart : CName;
	var m_settingVar : ConfigVar;
	var m_isPreGame : Bool;
}

class DlcDescriptionController extends inkLogicController
{
	private editable var m_titleRef : inkTextRef;
	private editable var m_descriptionRef : inkTextRef;
	private editable var m_guideRef : inkTextRef;
	private editable var m_imageRef : inkImageRef;
	private editable var m_settingSelectorRef : inkWidgetRef;
	private var m_settingSelector : weak< SettingsSelectorController >;
	private var m_settingsListener : DLCSettingsVarListener;
	private var m_settingVar : ConfigVar;
	private var m_isPreGame : Bool;

	public function SetData( userData : DlcDescriptionData )
	{
		inkTextRef.SetLocalizedText( m_titleRef, userData.m_title );
		inkTextRef.SetLocalizedText( m_descriptionRef, userData.m_description );
		inkImageRef.SetTexturePart( m_imageRef, userData.m_imagePart );
		m_settingVar = userData.m_settingVar;
		m_isPreGame = userData.m_isPreGame;
		if( userData.m_guide != '' )
		{
			inkTextRef.SetLocalizedText( m_guideRef, userData.m_guide );
		}
		else
		{
			inkTextRef.SetVisible( m_guideRef, false );
		}
		if( m_settingVar.GetName() != 'None' )
		{
			SetupSetting();
		}
	}

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

	private function SetupSetting()
	{
		if( inkWidgetRef.IsValid( m_settingSelectorRef ) )
		{
			m_settingSelector = ( ( SettingsSelectorControllerListInt )( SpawnFromLocal( inkWidgetRef.Get( m_settingSelectorRef ), 'settingsSelectorIntList' ).GetController() ) );
			m_settingSelector.Setup( m_settingVar, m_isPreGame );
			if( m_isPreGame )
			{
				m_settingVar.SetEnabled( true );
				m_settingsListener = new DLCSettingsVarListener;
				m_settingsListener.RegisterController( this );
				m_settingsListener.Register( m_settingVar.GetGroupPath() );
			}
			else
			{
				m_settingVar.SetEnabled( false );
				m_settingSelector.GetRootWidget().SetState( 'Disabled' );
			}
			m_settingSelector.Refresh();
		}
	}

}

class DLCSettingsVarListener extends ConfigVarListener
{
	private var m_ctrl : weak< DlcDescriptionController >;

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

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

}

class DlcMenuGameController extends gameuiMenuGameController
{
	private editable var m_buttonHintsRef : inkWidgetRef;
	private editable var m_containersRef : inkCompoundRef;
	private var m_settings : UserSettings;
	private var dlcSettingsGroup : ConfigGroup;

	protected event OnInitialize()
	{
		m_settings = GetSystemRequestsHandler().GetUserSettings();
		if( m_settings.HasGroup( '/gameplay/dlcContent' ) )
		{
			dlcSettingsGroup = m_settings.GetGroup( '/gameplay/dlcContent' );
		}
		super.OnInitialize();
		SpawnInputHints();
		SpawnDescriptions( 'UI-DLC-ContentPack1600-Edgerunners_Title', 'UI-DLC-ContentPack1600-Edgerunners_Description', 'UI-DLC-ContentPack1600-Edgerunners_Guide', 'dlc4_5_edgerunners' );
		SpawnDescriptions( 'UI-DLC-ContentPack1600-Gigs_Title', 'UI-DLC-ContentPack1600-Gigs_Description', 'UI-DLC-ContentPack1600-Gigs_Guide', 'dlc4_5_sts' );
		SpawnDescriptions( 'UI-DLC-ContentPack1600-Wardrobe_Title', 'UI-DLC-ContentPack1600-Wardrobe_Description', 'UI-DLC-ContentPack1600-Wardrobe_Guide', 'dlc4_5_wardrobe' );
		SpawnDescriptions( 'UI-DLC-ContentPack1600-Guns_Title', 'UI-DLC-ContentPack1600-Guns_Description', 'UI-DLC-ContentPack1600-Guns_Guide', 'dlc4_5_guns' );
		SpawnDescriptions( 'UI-DLC-ContentPack1600-Melee_Title', 'UI-DLC-ContentPack1600-Melee_Description', 'UI-DLC-ContentPack1600-Melee_Guide', 'dlc4_5_melee_weapons' );
		SpawnDescriptions( 'UI-DLC-ContentPack1600-RoachRace_Title', 'UI-DLC-ContentPack1600-RoachRace_Description', 'UI-DLC-ContentPack1600-RoachRace_Guide', 'dlc4_5_roachrace' );
		SpawnDescriptions( 'UI-DLC-ContentPack1600-Nibbles_Title', 'UI-DLC-ContentPack1600-Nibbles_Description', 'UI-DLC-ContentPack1600-Nibbles_Guide', 'dlc4_5_nibbles_photomode' );
		SpawnDescriptions( 'UI-DLC-ContentPack1600-Rims_Title', 'UI-DLC-ContentPack1600-Rims_Description', 'UI-DLC-ContentPack1600-Rims_Guide', 'dlc4_5_neon_rims' );
		SpawnDescriptions( 'UI-DLC-Apartments_Title', 'UI-DLC-Apartments_Description', 'UI-DLC-Apartments_Guide', 'dlc3_apartments' );
		SpawnDescriptions( 'UI-DLC-Hairdresser_Title', 'UI-DLC-Hairdresser_Description', 'UI-DLC-Hairdresser_Guide', 'dlc3_hairdresser' );
		SpawnDescriptions( 'UI-DLC-WeaponScopes_Title', 'UI-DLC-WeaponScopes_Description', 'UI-DLC-WeaponScopes_Guide', 'dlc2_scopes' );
		SpawnDescriptions( 'UI-DLC-MuzzleBrakes_Title', 'UI-DLC-MuzzleBrakes_Description', 'UI-DLC-MuzzleBrakes_Guide', 'dlc2_muzzle' );
		SpawnDescriptions( 'UI-DLC-UMBRA_Title', 'UI-DLC-UMBRA_Description', 'UI-DLC-UMBRA_Guide', 'dlc2_darra' );
		SpawnDescriptions( 'UI-DLC-Guillotine_Title', 'UI-DLC-Guillotine_Description', 'UI-DLC-Guillotine_Guide', 'dlc2_guillotine' );
		SpawnDescriptions( 'UI-DLC-JohnnyPhotoMode_Title', 'UI-DLC-JohnnyPhotoMode_Description', 'UI-DLC-JohnnyPhotoMode_Guide', 'dlc2_johnny_photomode' );
		SpawnDescriptions( 'UI-DLC-JohnnyAltApp_Title', 'UI-DLC-JohnnyAltApp_Description', 'UI-DLC-JohnnyAltApp_Guide', 'dlc_johnny', 'JohnnySilverhandAltApp' );
		SpawnDescriptions( 'UI-DLC-Jackets_Title', 'UI-DLC-Jackets_Description', 'UI-DLC-Jackets_Guide', 'dlc_jackets' );
		SpawnDescriptions( 'UI-DLC-Archer_Title', 'UI-DLC-Archer_Description', 'UI-DLC-Archer_Guide', 'dlc_archer' );
	}

	private function SpawnInputHints()
	{
		var buttonHintsController : weak< ButtonHints >;
		var widget : weak< inkWidget >;
		var path : ResRef;
		if( inkWidgetRef.IsValid( m_buttonHintsRef ) )
		{
			path = R"base\gameplay\gui\common\buttonhints.inkwidget";
			widget = SpawnFromExternal( inkWidgetRef.Get( m_buttonHintsRef ), path, 'Root' );
			buttonHintsController = ( ( ButtonHints )( widget.GetController() ) );
			buttonHintsController.AddButtonHint( 'back', "Common-Access-Close" );
		}
	}

	private function SpawnDescriptions( title : CName, description : CName, guide : CName, imagePart : CName, optional settingVarName : CName )
	{
		var data : DlcDescriptionData;
		data = new DlcDescriptionData;
		data.m_title = title;
		data.m_description = description;
		data.m_guide = guide;
		data.m_imagePart = imagePart;
		if( settingVarName != 'None' && !( dlcSettingsGroup.IsEmpty( true ) ) )
		{
			if( dlcSettingsGroup.HasVar( settingVarName ) )
			{
				data.m_settingVar = dlcSettingsGroup.GetVar( settingVarName );
				data.m_isPreGame = GetSystemRequestsHandler().IsPreGame();
			}
		}
		AsyncSpawnFromLocal( inkCompoundRef.Get( m_containersRef ), 'dlcDescription', this, 'OnDescriptionSpawned', data );
	}

	protected event OnDescriptionSpawned( widget : inkWidget, userData : IScriptable )
	{
		var itemCtrl : weak< DlcDescriptionController >;
		if( widget )
		{
			itemCtrl = ( ( DlcDescriptionController )( widget.GetController() ) );
			itemCtrl.SetData( ( ( DlcDescriptionData )( userData ) ) );
		}
	}

}

