class NewPerksCyberwareTooltipController extends AGenericTooltipController
{
	private editable var m_title : inkTextRef;
	private editable var m_subTitle : inkTextRef;
	private editable var m_description : inkTextRef;
	private editable var m_subDescription : inkTextRef;
	private editable var m_videoWidget : inkVideoRef;
	private editable var m_cornerContainer : inkWidgetRef;
	private editable var m_relicCost : inkWidgetRef;
	private const editable var m_bars : array< inkWidgetRef >;
	private editable var m_inputHints : inkWidgetRef;
	private editable var m_buyHint : inkWidgetRef;
	private editable var m_sellHint : inkWidgetRef;
	private var m_currentEntry : NewPerksCyberwareDetailsMenu;
	private var m_swipeOutAnim : inkAnimProxy;
	private var m_swipeInAnim : inkAnimProxy;
	private var m_data : NewPerkTooltipData;
	protected var m_settings : UserSettings;
	protected var m_settingsListener : EspionageTooltipSettingsListener;
	protected var m_groupPath : CName;
	default m_groupPath = '/accessibility/interface';
	protected var m_bigFontEnabled : Bool;
	private editable var m_wrapper : inkWidgetRef;
	private const var c_swipeLeftOut : CName;
	default c_swipeLeftOut = 'espionage_central_swipe_left_out';
	private const var c_swipeLeftIn : CName;
	default c_swipeLeftIn = 'espionage_central_swipe_left_in';
	private const var c_swipeRightOut : CName;
	default c_swipeRightOut = 'espionage_central_swipe_right_out';
	private const var c_swipeRightIn : CName;
	default c_swipeRightIn = 'espionage_central_swipe_right_in';

	protected event OnInitialize()
	{
		super.OnInitialize();
		RegisterToGlobalInputCallback( 'OnPostOnPress', this, 'OnHandlePressInput' );
		m_currentEntry = ( ( NewPerksCyberwareDetailsMenu )( 0 ) );
	}

	protected event OnUninitialize()
	{
		UnregisterFromGlobalInputCallback( 'OnPostOnPress', this, 'OnHandlePressInput' );
	}

	public override function Show()
	{
		super.Show();
		StopSwipeAnims();
	}

	public override function Refresh()
	{
		SetData( m_data );
	}

	public override function SetData( tooltipData : ATooltipData )
	{
		m_data = ( ( NewPerkTooltipData )( tooltipData ) );
		m_data.RefreshRuntimeData();
		UpdateData();
		UpdateState( m_data.perkData );
		UpdateInputHints( m_data, m_data.perkData );
		m_settings = new UserSettings;
		m_settingsListener = new EspionageTooltipSettingsListener;
		m_settingsListener.RegisterController( this );
		m_settingsListener.Register( m_groupPath );
		UpdateTooltipSize();
	}

	protected event OnHandlePressInput( evt : inkPointerEvent )
	{
		if( !( m_Root.IsVisible() ) )
		{
			return 0;
		}
		if( evt.IsAction( 'ep1_cyberware_perk_details_forward' ) )
		{
			StopSwipeAnims();
			m_swipeOutAnim = PlayLibraryAnimation( c_swipeLeftOut );
			m_swipeOutAnim.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnSwipeLeftOutAnimFinished' );
		}
		else if( evt.IsAction( 'ep1_cyberware_perk_details_back' ) )
		{
			StopSwipeAnims();
			m_swipeOutAnim = PlayLibraryAnimation( c_swipeRightOut );
			m_swipeOutAnim.RegisterToCallback( inkanimEventType.OnFinish, this, 'OnSwipeRightOutAnimFinished' );
		}
	}

	protected event OnSwipeLeftOutAnimFinished( proxy : inkAnimProxy )
	{
		var count : Int32;
		count = ( ( Int32 )( NewPerksCyberwareDetailsMenu.COUNT ) );
		m_currentEntry = ( ( NewPerksCyberwareDetailsMenu )( ( ( ( ( Int32 )( m_currentEntry ) ) + 1 ) + count ) % count ) );
		UpdateData();
		m_swipeInAnim = PlayLibraryAnimation( c_swipeRightIn );
	}

	protected event OnSwipeRightOutAnimFinished( proxy : inkAnimProxy )
	{
		var count : Int32;
		count = ( ( Int32 )( NewPerksCyberwareDetailsMenu.COUNT ) );
		m_currentEntry = ( ( NewPerksCyberwareDetailsMenu )( ( ( ( ( Int32 )( m_currentEntry ) ) - 1 ) + count ) % count ) );
		UpdateData();
		m_swipeInAnim = PlayLibraryAnimation( c_swipeLeftIn );
	}

	private function StopSwipeAnims()
	{
		if( m_swipeOutAnim && m_swipeOutAnim.IsPlaying() )
		{
			m_swipeOutAnim.UnregisterFromAllCallbacks( inkanimEventType.OnFinish );
			m_swipeOutAnim.GotoStartAndStop();
		}
		if( m_swipeInAnim && m_swipeInAnim.IsPlaying() )
		{
			m_swipeInAnim.UnregisterFromAllCallbacks( inkanimEventType.OnFinish );
			m_swipeInAnim.GotoEndAndStop();
		}
	}

	private function GetRecordString() : String
	{
		switch( m_currentEntry )
		{
			case NewPerksCyberwareDetailsMenu.MantisBlades:
				return "Espionage_Central_Milestone_MantisBlades";
			case NewPerksCyberwareDetailsMenu.GorillaArms:
				return "Espionage_Central_Milestone_GorillaArms";
			case NewPerksCyberwareDetailsMenu.ProjectileLauncher:
				return "Espionage_Central_Milestone_ProjectileLauncher";
			case NewPerksCyberwareDetailsMenu.Monowire:
				return "Espionage_Central_Milestone_Monowire";
		}
		return "";
	}

	private function GetTitle() : String
	{
		var recordString : String;
		var record : NewPerk_Record;
		recordString = GetRecordString();
		if( recordString == "" )
		{
			return "";
		}
		record = TDB.GetNewPerkRecord( TDBID.Create( "NewPerks." + GetRecordString() ) );
		return record.Loc_name_key();
	}

	private function GetDescription() : String
	{
		var recordString : String;
		var record : NewPerk_Record;
		recordString = GetRecordString();
		if( recordString == "" )
		{
			return "";
		}
		record = TDB.GetNewPerkRecord( TDBID.Create( "NewPerks." + GetRecordString() ) );
		return record.Loc_desc_key();
	}

	private function GetVideo() : ResRef
	{
		var video : ResRef;
		var recordString : String;
		var record : NewPerk_Record;
		recordString = GetRecordString();
		if( recordString == "" )
		{
			return video;
		}
		record = TDB.GetNewPerkRecord( TDBID.Create( "NewPerks." + GetRecordString() ) );
		video = record.BinkPath();
		return video;
	}

	private function UpdateState( perkData : NewPerkDisplayData )
	{
		m_Root.SetState( ( ( perkData.m_level == 0 ) ? ( 'Locked' ) : ( 'Purchased' ) ) );
		inkWidgetRef.SetVisible( m_cornerContainer, ( ( perkData.m_level == 0 ) ? ( false ) : ( true ) ) );
		inkWidgetRef.SetVisible( m_relicCost, ( ( perkData.m_level == 0 ) ? ( true ) : ( false ) ) );
	}

	private function UpdateInputHints( data : BasePerksMenuTooltipData, perkData : BasePerkDisplayData )
	{
		var upgradeable, refundable : Bool;
		upgradeable = data.manager.IsPerkUpgradeable( perkData );
		refundable = data.manager.IsNewPerkRefundable( ( ( NewPerkDisplayData )( perkData ) ) );
		inkWidgetRef.SetVisible( m_inputHints, upgradeable || refundable );
		inkWidgetRef.SetVisible( m_buyHint, upgradeable );
		inkWidgetRef.SetVisible( m_sellHint, refundable );
	}

	private function UpdateData()
	{
		var i : Int32;
		var record : weak< NewPerkLevelUIData_Record >;
		var dataPackage : UILocalizationDataPackage;
		var videoRef : ResRef;
		inkTextRef.SetText( m_title, m_data.perkData.m_localizedName );
		inkTextRef.SetText( m_subTitle, GetTitle() );
		inkTextRef.SetText( m_description, m_data.perkData.m_localizedDescription );
		record = TweakDBInterface.GetNewPerkRecord( TDBID.Create( "NewPerks." + GetRecordString() ) ).UiData();
		if( record != NULL )
		{
			inkTextRef.SetText( m_subDescription, GetLocalizedText( GetDescription() ) + "{__empty__}" );
			dataPackage = UILocalizationDataPackage.FromNewPerkUIDataPackage( record );
			dataPackage.EnableNotReplacedWorkaround();
			if( dataPackage.GetParamsCount() > 0 )
			{
				inkTextRef.SetTextParameters( m_subDescription, dataPackage.GetTextParams() );
			}
		}
		else
		{
			inkTextRef.SetText( m_subDescription, GetDescription() );
		}
		for( i = 0; i < 4; i += 1 )
		{
			inkWidgetRef.SetState( m_bars[ i ], ( ( ( ( Int32 )( m_currentEntry ) ) == i ) ? ( ( ( m_data.perkData.m_level == 0 ) ? ( 'Selected' ) : ( 'SelectedBought' ) ) ) : ( 'Default' ) ) );
		}
		videoRef = GetVideo();
		if( ResRef.IsValid( videoRef ) )
		{
			inkVideoRef.Stop( m_videoWidget );
			inkVideoRef.SetVideoPath( m_videoWidget, videoRef );
			inkVideoRef.SetLoop( m_videoWidget, true );
			inkVideoRef.Play( m_videoWidget );
			inkVideoRef.SetVisible( m_videoWidget, true );
		}
		else
		{
			inkVideoRef.SetVisible( m_videoWidget, false );
		}
	}

	public function OnVarModified( groupPath : CName, varName : CName, varType : ConfigVarType, reason : ConfigChangeReason )
	{
		switch( varName )
		{
			case 'BigFont':
				UpdateTooltipSize();
			break;
			default:
				break;
		}
	}

	private function UpdateTooltipSize()
	{
		var configVar : ConfigVarBool;
		configVar = ( ( ConfigVarBool )( m_settings.GetVar( m_groupPath, 'BigFont' ) ) );
		SetTooltipSize( configVar.GetValue() );
	}

	protected virtual function SetTooltipSize( value : Bool )
	{
		if( value == true )
		{
			inkWidgetRef.SetSize( m_wrapper, 1205.0, 0.0 );
			inkTextRef.SetWrappingAtPosition( m_title, 980.0 );
			inkTextRef.SetWrappingAtPosition( m_subTitle, 1020.0 );
			inkTextRef.SetWrappingAtPosition( m_description, 1100.0 );
			inkTextRef.SetWrappingAtPosition( m_subDescription, 1100.0 );
			m_bigFontEnabled = true;
		}
		else
		{
			inkWidgetRef.SetSize( m_wrapper, 905.0, 0.0 );
			inkTextRef.SetWrappingAtPosition( m_title, 680.0 );
			inkTextRef.SetWrappingAtPosition( m_subTitle, 720.0 );
			inkTextRef.SetWrappingAtPosition( m_description, 800.0 );
			inkTextRef.SetWrappingAtPosition( m_subDescription, 800.0 );
			m_bigFontEnabled = false;
		}
	}

}

class EspionageTooltipSettingsListener extends ConfigVarListener
{
	private var m_ctrl : weak< NewPerksCyberwareTooltipController >;

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

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

}

enum NewPerksCyberwareDetailsMenu
{
	MantisBlades = 0,
	GorillaArms = 1,
	ProjectileLauncher = 2,
	Monowire = 3,
	COUNT = 4,
}

