class CharacterCreationBackstorySelectionMenu extends BaseCharacterCreationController
{
	editable var m_nomad : inkWidgetRef;
	editable var m_streetRat : inkWidgetRef;
	editable var m_corpo : inkWidgetRef;
	private var m_animationProxy : inkAnimProxy;
	private var m_clickTarget : String;
	private var m_nomadTarget : String;
	private var m_streetTarget : String;
	private var m_corpoTarget : String;

	protected event OnInitialize()
	{
		if( !( GetSystemRequestsHandler().IsAdditionalContentEnabled( 'EP1' ) ) )
		{
			GetCharacterCustomizationSystem().InitializeState();
		}
		super.OnInitialize();
		m_nomadTarget = "N";
		m_streetTarget = "S";
		m_corpoTarget = "C";
		SetupLifePathButtons();
		GetTelemetrySystem().LogInitialChoiceSetStatege( telemetryInitalChoiceStage.LifePath );
		OnIntro();
	}

	protected event OnUninitialize()
	{
		super.OnUninitialize();
		OnOutro();
	}

	private function SetupLifePathButtons()
	{
		SetupLifePathButton( inkWidgetRef.Get( m_nomad ), 'LocKey#1586', 'lifepath_nomad', R"base\movies\misc\character_creation\nomad.bk2", 'LocKey#1799', 'OnPressNomad', 'OnReleaseNomad' );
		SetupLifePathButton( inkWidgetRef.Get( m_streetRat ), 'LocKey#1587', 'lifepath_street', R"base\movies\misc\character_creation\street_kid.bk2", 'LocKey#1801', 'OnPressStreet', 'OnReleaseStreet' );
		SetupLifePathButton( inkWidgetRef.Get( m_corpo ), 'LocKey#1585', 'lifepath_corpo', R"base\movies\misc\character_creation\corpo.bk2", 'LocKey#1800', 'OnPressCorpo', 'OnReleaseCorpo' );
	}

	private function SetupLifePathButton( widget : weak< inkWidget >, desc : CName, imagePath : CName, videoPath : ResRef, label : CName, onPressEvent : CName, onReleaseEvent : CName )
	{
		var logController : weak< characterCreationLifePathBtn >;
		if( widget )
		{
			logController = ( ( characterCreationLifePathBtn )( widget.GetController() ) );
			if( logController )
			{
				logController.SetDescription( desc, imagePath, videoPath, label );
				logController.RegisterToCallback( 'OnPress', this, onPressEvent );
				logController.RegisterToCallback( 'OnRelease', this, onReleaseEvent );
			}
		}
	}

	protected event OnReleaseNomad( e : inkPointerEvent )
	{
		var lifePath : TweakDBID;
		if( e.IsAction( 'click' ) && m_clickTarget == m_nomadTarget )
		{
			GameInstance.GetAudioSystem( GetPlayerControlledObject().GetGame() ).Play( 'ui_hacking_access_granted' );
			lifePath = T"LifePaths.Nomad";
			m_characterCustomizationState.SetLifePath( lifePath );
			GetTelemetrySystem().LogInitialChoiceLifePathSelected( lifePath );
			m_eventDispatcher.SpawnEvent( 'OnAccept' );
			m_clickTarget = "";
		}
	}

	protected event OnReleaseStreet( e : inkPointerEvent )
	{
		var lifePath : TweakDBID;
		if( e.IsAction( 'click' ) && m_clickTarget == m_streetTarget )
		{
			GameInstance.GetAudioSystem( GetPlayerControlledObject().GetGame() ).Play( 'ui_hacking_access_granted' );
			lifePath = T"LifePaths.StreetKid";
			m_characterCustomizationState.SetLifePath( lifePath );
			GetTelemetrySystem().LogInitialChoiceLifePathSelected( lifePath );
			m_eventDispatcher.SpawnEvent( 'OnAccept' );
			m_clickTarget = "";
		}
	}

	protected event OnReleaseCorpo( e : inkPointerEvent )
	{
		var lifePath : TweakDBID;
		if( e.IsAction( 'click' ) && m_clickTarget == m_corpoTarget )
		{
			GameInstance.GetAudioSystem( GetPlayerControlledObject().GetGame() ).Play( 'ui_hacking_access_granted' );
			lifePath = T"LifePaths.Corporate";
			m_characterCustomizationState.SetLifePath( lifePath );
			GetTelemetrySystem().LogInitialChoiceLifePathSelected( lifePath );
			m_eventDispatcher.SpawnEvent( 'OnAccept' );
			m_clickTarget = "";
		}
	}

	protected event OnPressNomad( e : inkPointerEvent )
	{
		m_clickTarget = m_nomadTarget;
	}

	protected event OnPressStreet( e : inkPointerEvent )
	{
		m_clickTarget = m_streetTarget;
	}

	protected event OnPressCorpo( e : inkPointerEvent )
	{
		m_clickTarget = m_corpoTarget;
	}

	protected override function PriorMenu()
	{
		if( !( GetSystemRequestsHandler().IsAdditionalContentEnabled( 'EP1' ) ) )
		{
			GetCharacterCustomizationSystem().ClearState();
		}
		GameInstance.GetAudioSystem( GetPlayerControlledObject().GetGame() ).Play( 'ui_main_menu_loop_start' );
		GameInstance.GetAudioSystem( GetPlayerControlledObject().GetGame() ).Play( 'mus_game_menus_01_main_menu' );
		super.PriorMenu();
	}

	public function PlayAnim( animName : CName, optional callBack : CName )
	{
		if( m_animationProxy && m_animationProxy.IsPlaying() )
		{
			m_animationProxy.Stop();
		}
		m_animationProxy = PlayLibraryAnimation( animName );
		if( callBack != '' )
		{
			m_animationProxy.RegisterToCallback( inkanimEventType.OnFinish, this, callBack );
		}
	}

	private function OnIntro()
	{
		PlayAnim( 'intro', 'OnIntroComplete' );
	}

	private function OnOutro()
	{
		PlayAnim( 'outro' );
	}

	protected event OnIntroComplete( anim : inkAnimProxy )
	{
		PlaySound( 'CharacterCreationLoading', 'OnOpen' );
		GameInstance.GetAudioSystem( GetPlayerControlledObject().GetGame() ).Play( 'ui_main_menu_loop_stop' );
		GameInstance.GetAudioSystem( GetPlayerControlledObject().GetGame() ).Play( 'mus_game_menus_02_character_creation' );
	}

}

class CharacterCreationGenderSelectionMenu extends BaseCharacterCreationController
{
	editable var m_streetRat_male : inkWidgetRef;
	editable var m_streetRat_female : inkWidgetRef;
	private var m_clickTarget : inkWidgetRef;
	private var m_animationProxy : inkAnimProxy;
	private var m_maleAnimProxy : inkAnimProxy;
	private var m_femaleAnimProxy : inkAnimProxy;

	protected event OnInitialize()
	{
		super.OnInitialize();
		inkWidgetRef.RegisterToCallback( m_streetRat_male, 'OnRelease', this, 'OnReleaseMale' );
		inkWidgetRef.RegisterToCallback( m_streetRat_female, 'OnRelease', this, 'OnReleaseFemale' );
		inkWidgetRef.RegisterToCallback( m_streetRat_male, 'OnPress', this, 'OnPressMale' );
		inkWidgetRef.RegisterToCallback( m_streetRat_female, 'OnPress', this, 'OnPressFemale' );
		inkWidgetRef.RegisterToCallback( m_streetRat_male, 'OnHoverOver', this, 'OnHoverOverMale' );
		inkWidgetRef.RegisterToCallback( m_streetRat_female, 'OnHoverOver', this, 'OnHoverOverFemale' );
		inkWidgetRef.RegisterToCallback( m_streetRat_male, 'OnHoverOut', this, 'OnHoverOutMale' );
		inkWidgetRef.RegisterToCallback( m_streetRat_female, 'OnHoverOut', this, 'OnHoverOutFemale' );
		if( m_characterCustomizationState.IsExpansionStandalone() )
		{
			SetEP1AttributePreset();
		}
		else
		{
			SetAttributePreset( m_characterCustomizationState.GetLifePath() );
		}
		GetTelemetrySystem().LogInitialChoiceSetStatege( telemetryInitalChoiceStage.Gender );
		OnIntro();
	}

	protected event OnUninitialize()
	{
		super.OnUninitialize();
		OnOutro();
	}

	protected event OnPuppetReadyToBeDisplayed( evt : gameuiPuppetPreview_ReadyToBeDisplayed )
	{
		if( evt.isMale )
		{
			PlayLibraryAnimation( 'male_preview_intro' );
		}
		else
		{
			PlayLibraryAnimation( 'female_preview_intro' );
		}
	}

	protected event OnHoverOverMale( e : inkPointerEvent )
	{
		PlayAnim( 'male_highlight_intro', m_maleAnimProxy );
	}

	protected event OnHoverOutMale( e : inkPointerEvent )
	{
		PlayAnim( 'male_highlight_outro', m_maleAnimProxy );
	}

	protected event OnHoverOverFemale( e : inkPointerEvent )
	{
		PlayAnim( 'female_highlight_intro', m_femaleAnimProxy );
	}

	protected event OnHoverOutFemale( e : inkPointerEvent )
	{
		PlayAnim( 'female_highlight_outro', m_femaleAnimProxy );
	}

	protected event OnReleaseMale( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) && ( m_clickTarget == m_streetRat_male ) )
		{
			PlaySound( 'Button', 'OnPress' );
			m_characterCustomizationState.SetIsBodyGenderMale( true );
			m_characterCustomizationState.SetIsBrainGenderMale( true );
			GetTelemetrySystem().LogInitialChoiceBodyGenderSelected( true );
			m_eventDispatcher.SpawnEvent( 'OnAccept' );
		}
	}

	protected event OnReleaseFemale( e : inkPointerEvent )
	{
		if( e.IsAction( 'click' ) && ( m_clickTarget == m_streetRat_female ) )
		{
			PlaySound( 'Button', 'OnPress' );
			m_characterCustomizationState.SetIsBodyGenderMale( false );
			m_characterCustomizationState.SetIsBrainGenderMale( false );
			GetTelemetrySystem().LogInitialChoiceBodyGenderSelected( false );
			m_eventDispatcher.SpawnEvent( 'OnAccept' );
		}
	}

	protected event OnPressMale( e : inkPointerEvent )
	{
		m_clickTarget = m_streetRat_male;
	}

	protected event OnPressFemale( e : inkPointerEvent )
	{
		m_clickTarget = m_streetRat_female;
	}

	public function PlayAnim( animName : CName, optional animProxy : inkAnimProxy, optional callBack : CName )
	{
		var currentAnimProxy : inkAnimProxy;
		currentAnimProxy = ( ( animProxy ) ? ( animProxy ) : ( m_animationProxy ) );
		if( currentAnimProxy.IsPlaying() )
		{
			m_animationProxy.Stop();
		}
		currentAnimProxy = PlayLibraryAnimation( animName );
	}

	private function SetAttributePreset( lifePath : TweakDBID )
	{
		var i : Int32;
		var preset : TweakDBID;
		var UIAttRecords : array< weak< UICharacterCreationAttribute_Record > >;
		m_characterCustomizationState.SetAttributePointsAvailable( 7 );
		if( lifePath == T"LifePaths.Nomad" )
		{
			preset = T"UIAttributePresets.Preset1";
		}
		else if( lifePath == T"LifePaths.StreetKid" )
		{
			preset = T"UIAttributePresets.Preset2";
		}
		else if( lifePath == T"LifePaths.Corporate" )
		{
			preset = T"UIAttributePresets.Preset3";
		}
		TweakDBInterface.GetUICharacterCreationAttributesPresetRecord( preset ).Attributes( UIAttRecords );
		for( i = 0; i < UIAttRecords.Size(); i += 1 )
		{
			m_characterCustomizationState.SetAttribute( UIAttRecords[ i ].Attribute().StatType(), ( ( Uint32 )( UIAttRecords[ i ].Value() ) ) );
		}
	}

	private function SetEP1AttributePreset()
	{
		var i : Int32;
		var buildAttributes : array< weak< BuildAttribute_Record > >;
		TweakDBInterface.GetProgressionBuildRecord( T"ProgressionBuilds.EP1_Standalone_StartingBuild" ).StartingAttributes( buildAttributes );
		for( i = 0; i < buildAttributes.Size(); i += 1 )
		{
			m_characterCustomizationState.SetAttribute( buildAttributes[ i ].Attribute().StatType(), ( ( Uint32 )( buildAttributes[ i ].Level() ) ) );
		}
	}

	private function OnIntro()
	{
		PlayAnim( 'intro', m_animationProxy );
	}

	private function OnOutro()
	{
		PlayAnim( 'outro', m_animationProxy );
	}

}

class CharacterCreationGenderBackstoryPathHeader extends inkLogicController
{
	editable var m_label : inkTextRef;
	editable var m_desc : inkTextRef;
	editable var m_bg : inkWidgetRef;
	private var m_selectedColor : Color;
	private var m_unSelectedColor : Color;
	private var m_textSelectedColor : Color;
	private var m_textUnselectedColor : Color;

	protected event OnInitialize()
	{
		m_unSelectedColor.Red = 39;
		m_unSelectedColor.Green = 51;
		m_unSelectedColor.Blue = 51;
		m_selectedColor.Red = 160;
		m_selectedColor.Green = 190;
		m_selectedColor.Blue = 190;
		m_textSelectedColor = m_unSelectedColor;
		m_textUnselectedColor = m_selectedColor;
	}

	public function Select()
	{
		inkTextRef.SetTintColor( m_label, m_textSelectedColor );
		inkWidgetRef.SetTintColor( m_bg, m_selectedColor );
	}

	public function UnSelect()
	{
		inkTextRef.SetTintColor( m_label, m_textUnselectedColor );
		inkWidgetRef.SetTintColor( m_bg, m_unSelectedColor );
	}

	public function SetData( const title : ref< String >, const desc : ref< String > )
	{
		inkTextRef.SetText( m_label, title );
		inkTextRef.SetText( m_desc, desc );
	}

}

