class cursorDeviceGameController extends inkGameController
{
	private var m_bbUIData : weak< IBlackboard >;
	private var m_bbWeaponInfo : weak< IBlackboard >;
	private var m_bbWeaponEventId : CallbackHandle;
	private var m_bbPlayerTierEventId : CallbackHandle;
	private var m_interactionBlackboardId : CallbackHandle;
	private var m_upperBodyStateBlackboardId : CallbackHandle;
	private var m_sceneTier : GameplayTier;
	private var m_upperBodyState : gamePSMUpperBodyStates;
	private var m_isUnarmed : Bool;
	private var m_cursorDevice : weak< inkImage >;
	private var m_fadeOutAnimation : inkAnimDef;
	private var m_fadeInAnimation : inkAnimDef;
	private var m_wasLastInteractionWithDevice : Bool;
	private var m_interactionDeviceState : Bool;

	protected event OnInitialize()
	{
		var bbPlayerSM : IBlackboard;
		var playerSMDef : PlayerStateMachineDef;
		var playerPuppet : weak< GameObject >;
		m_wasLastInteractionWithDevice = false;
		m_bbUIData = GetBlackboardSystem().Get( GetAllBlackboardDefs().UIGameData );
		m_interactionBlackboardId = m_bbUIData.RegisterListenerVariant( GetAllBlackboardDefs().UIGameData.InteractionData, this, 'OnInteractionStateChange' );
		m_bbWeaponInfo = GetBlackboardSystem().Get( GetAllBlackboardDefs().UI_ActiveWeaponData );
		m_bbWeaponEventId = m_bbWeaponInfo.RegisterListenerVariant( GetAllBlackboardDefs().UI_ActiveWeaponData.WeaponRecordID, this, 'OnWeaponSwap' );
		m_cursorDevice = ( ( inkImage )( GetWidget( 'cursor_device' ) ) );
		m_cursorDevice.SetOpacity( 0.0 );
		m_isUnarmed = true;
		m_interactionDeviceState = false;
		m_upperBodyState = gamePSMUpperBodyStates.Default;
		m_interactionDeviceState = false;
		playerPuppet = ( ( PlayerPuppet )( GetOwnerEntity() ) );
		playerSMDef = GetAllBlackboardDefs().PlayerStateMachine;
		if( playerSMDef )
		{
			bbPlayerSM = GetPSMBlackboard( playerPuppet );
			if( bbPlayerSM )
			{
				m_upperBodyStateBlackboardId = bbPlayerSM.RegisterListenerInt( playerSMDef.UpperBody, this, 'OnUpperBodyChange' );
			}
		}
		CreateAnimations();
	}

	protected event OnUninitialize()
	{
		var bbPlayerSM : IBlackboard;
		var playerSMDef : PlayerStateMachineDef;
		var playerPuppet : weak< GameObject >;
		if( m_bbUIData )
		{
			m_bbUIData.UnregisterListenerVariant( GetAllBlackboardDefs().UIGameData.InteractionData, m_interactionBlackboardId );
		}
		if( m_bbWeaponInfo )
		{
			m_bbWeaponInfo.UnregisterListenerVariant( GetAllBlackboardDefs().UI_ActiveWeaponData.WeaponRecordID, m_bbWeaponEventId );
		}
		playerPuppet = ( ( PlayerPuppet )( GetOwnerEntity() ) );
		playerSMDef = GetAllBlackboardDefs().PlayerStateMachine;
		if( playerSMDef )
		{
			bbPlayerSM = GetPSMBlackboard( playerPuppet );
			if( bbPlayerSM )
			{
				bbPlayerSM.UnregisterListenerInt( playerSMDef.UpperBody, m_upperBodyStateBlackboardId );
			}
		}
	}

	protected event OnPlayerAttach( playerGameObject : GameObject )
	{
		RegisterPSMListeners( playerGameObject );
	}

	protected event OnPlayerDetach( playerGameObject : GameObject )
	{
		UnregisterPSMListeners( playerGameObject );
	}

	protected function RegisterPSMListeners( playerPuppet : GameObject )
	{
		var bbSceneTier : IBlackboard;
		bbSceneTier = GetPSMBlackboard( playerPuppet );
		if( bbSceneTier )
		{
			m_bbPlayerTierEventId = bbSceneTier.RegisterListenerInt( GetAllBlackboardDefs().PlayerStateMachine.SceneTier, this, 'OnSceneTierChange' );
		}
	}

	protected function UnregisterPSMListeners( playerPuppet : GameObject )
	{
		var bbSceneTier : IBlackboard;
		bbSceneTier = GetPSMBlackboard( playerPuppet );
		if( bbSceneTier )
		{
			bbSceneTier.UnregisterListenerInt( GetAllBlackboardDefs().PlayerStateMachine.SceneTier, m_bbPlayerTierEventId );
		}
	}

	protected event OnWeaponSwap( value : Variant )
	{
		m_isUnarmed = ( ( TweakDBID )value ) == TDBID.None();
	}

	protected event OnSceneTierChange( argTier : Int32 )
	{
		m_sceneTier = ( ( GameplayTier )( argTier ) );
	}

	protected event OnUpperBodyChange( state : Int32 )
	{
		m_upperBodyState = ( ( gamePSMUpperBodyStates )( state ) );
		UpdateIsInteractingWithDevice();
	}

	protected event OnInteractionStateChange( value : Variant )
	{
		var interactionData : bbUIInteractionData;
		interactionData = ( ( bbUIInteractionData )value );
		m_interactionDeviceState = interactionData.terminalInteractionActive;
		UpdateIsInteractingWithDevice();
	}

	private function UpdateIsInteractingWithDevice()
	{
		var isInteractingWithDevice : Bool;
		isInteractingWithDevice = m_interactionDeviceState && m_upperBodyState != gamePSMUpperBodyStates.Aim;
		if( isInteractingWithDevice != m_wasLastInteractionWithDevice )
		{
			if( isInteractingWithDevice && ( m_isUnarmed || m_sceneTier != GameplayTier.Tier1_FullGameplay ) )
			{
				m_cursorDevice.StopAllAnimations();
				m_cursorDevice.SetOpacity( 1.0 );
			}
			else if( isInteractingWithDevice )
			{
				m_cursorDevice.StopAllAnimations();
				m_cursorDevice.PlayAnimation( m_fadeInAnimation );
			}
			else if( m_wasLastInteractionWithDevice )
			{
				m_cursorDevice.StopAllAnimations();
				m_cursorDevice.PlayAnimation( m_fadeOutAnimation );
			}
		}
		m_wasLastInteractionWithDevice = isInteractingWithDevice;
	}

	private function CreateAnimations()
	{
		var fadeOutInterp : inkAnimTransparency;
		var fadeInInterp : inkAnimTransparency;
		m_fadeInAnimation = new inkAnimDef;
		fadeInInterp = new inkAnimTransparency;
		fadeInInterp.SetStartDelay( 0.75 );
		fadeInInterp.SetStartTransparency( 0.0 );
		fadeInInterp.SetEndTransparency( 0.85000002 );
		fadeInInterp.SetDuration( 0.2 );
		fadeInInterp.SetType( inkanimInterpolationType.Quadratic );
		fadeInInterp.SetMode( inkanimInterpolationMode.EasyIn );
		m_fadeInAnimation.AddInterpolator( fadeInInterp );
		m_fadeOutAnimation = new inkAnimDef;
		fadeOutInterp = new inkAnimTransparency;
		fadeOutInterp.SetStartTransparency( 1.0 );
		fadeOutInterp.SetEndTransparency( 0.0 );
		fadeOutInterp.SetDuration( 0.1 );
		fadeOutInterp.SetType( inkanimInterpolationType.Quadratic );
		fadeOutInterp.SetMode( inkanimInterpolationMode.EasyOut );
		m_fadeOutAnimation.AddInterpolator( fadeOutInterp );
	}

}

