import class DamageIndicatorGameController extends inkHUDGameController
{

	private export function ShouldShowDamage( evt : gameDamageReceivedEvent ) : Bool
	{
		if( evt.hitEvent.attackData.GetAttackType() == gamedataAttackType.Effect )
		{
			return false;
		}
		if( evt.hitEvent.attackData.HasFlag( hitFlag.DisableNPCHitReaction ) || evt.hitEvent.attackData.HasFlag( hitFlag.DisablePlayerHitReaction ) )
		{
			return false;
		}
		return true;
	}

}

import class DamageIndicatorPartLogicController extends BaseDirectionalIndicatorPartLogicController
{
	private editable var m_arrowFrontWidget : inkImageRef;
	private editable var m_arrowBigWidget : inkImageRef;
	private editable var m_damageThreshold : Float;
	default m_damageThreshold = 100;
	private var m_root : weak< inkWidget >;
	private var m_animProxy : inkAnimProxy;
	private var m_damageTaken : Float;
	private var m_continuous : Bool;
	private var m_revengeActive : Bool;

	protected import function SetReadyToRemove();
	protected import function SetShowingDamage( showing : Bool );
	protected import function SetContinuous( continuous : Bool );
	protected import function SetMinimumOpacity( opacity : Float );
	protected import function ResetMinimumOpacity();

	protected event OnInitialize()
	{
		m_root = GetRootWidget();
		Reset();
	}

	protected export function InitPart()
	{
		Reset();
	}

	protected export function AddIncomingDamage( evt : gameDamageReceivedEvent )
	{
		var isRevengeActivatingHit : Bool;
		m_damageTaken += evt.totalDamageReceived;
		isRevengeActivatingHit = evt.hitEvent.attackData.HasFlag( hitFlag.RevengeActivatingHit );
		if( !( m_continuous ) && ( !( m_revengeActive ) || isRevengeActivatingHit ) )
		{
			if( m_animProxy && m_animProxy.IsPlaying() )
			{
				m_animProxy.UnregisterFromCallback( inkanimEventType.OnFinish, this, 'OnOutroComplete' );
				m_animProxy.Stop();
			}
			if( isRevengeActivatingHit )
			{
				if( !( m_revengeActive ) )
				{
					m_revengeActive = true;
					m_root.SetState( 'Revenge' );
					inkImageRef.SetVisible( m_arrowFrontWidget, false );
					inkImageRef.SetVisible( m_arrowBigWidget, true );
					SetMinimumOpacity( 0.60000002 );
				}
			}
			else
			{
				m_root.SetState( 'Damage' );
				ResetMinimumOpacity();
			}
			PlayAnim( 'Outro', 'OnOutroComplete' );
			SetShowingDamage( true );
		}
	}

	protected export function AddAttackAttempt( evt : AIAttackAttemptEvent )
	{
		var progress : Float;
		if( m_animProxy )
		{
			progress = m_animProxy.GetProgression();
		}
		if( evt.continuousMode != gameEContinuousMode.None )
		{
			if( evt.continuousMode == gameEContinuousMode.Start )
			{
				if( !( m_continuous ) )
				{
					if( m_animProxy.IsPlaying() )
					{
						m_animProxy.UnregisterFromCallback( inkanimEventType.OnFinish, this, 'OnOutroComplete' );
						m_animProxy.Stop();
					}
					SetShowingDamage( false );
					SetMinimumOpacity( evt.minimumOpacity );
					ResetRevenge();
					m_root.SetState( 'Hacking' );
					m_continuous = true;
					m_animProxy = PlayLibraryAnimation( 'Intro_Continuous' );
				}
			}
			else if( evt.continuousMode == gameEContinuousMode.Stop )
			{
				if( m_continuous )
				{
					SetMinimumOpacity( evt.minimumOpacity );
				}
				StopContinuousEffect();
			}
			SetContinuous( m_continuous );
		}
		else if( !( m_continuous ) )
		{
			if( ( m_damageTaken == 0.0 ) || ( progress > 0.75 ) )
			{
				m_root.SetState( 'Danger' );
				SetShowingDamage( false );
				SetMinimumOpacity( evt.minimumOpacity );
				ResetRevenge();
				if( m_animProxy && m_animProxy.IsPlaying() )
				{
					m_animProxy.UnregisterFromCallback( inkanimEventType.OnFinish, this, 'OnOutroComplete' );
					m_animProxy.Stop();
					if( evt.isWindUp && ( progress > 0.75 ) )
					{
						PlayAnim( 'Outro_WindUp', 'OnOutroComplete' );
					}
					else
					{
						PlayAnim( 'Outro_Miss_NoDelay', 'OnOutroComplete' );
					}
				}
				else
				{
					if( evt.isWindUp )
					{
						PlayAnim( 'Outro_WindUp', 'OnOutroComplete' );
					}
					else
					{
						PlayAnim( 'Outro_Miss', 'OnOutroComplete' );
					}
				}
			}
		}
	}

	private function StopContinuousEffect()
	{
		if( m_continuous )
		{
			if( m_animProxy.IsPlaying() )
			{
				m_animProxy.Stop();
			}
			m_continuous = false;
			PlayAnim( 'Outro_Continuous', 'OnOutroComplete' );
		}
		else if( !( m_animProxy ) || !( m_animProxy.IsPlaying() ) )
		{
			SetReadyToRemove();
		}
	}

	private function Reset()
	{
		m_damageTaken = 0.0;
		m_continuous = false;
		SetShowingDamage( false );
		SetContinuous( m_continuous );
		ResetRevenge();
	}

	private function ResetRevenge()
	{
		if( m_revengeActive )
		{
			m_revengeActive = false;
			inkImageRef.SetVisible( m_arrowFrontWidget, true );
			inkImageRef.SetVisible( m_arrowBigWidget, false );
		}
	}

	private function PlayAnim( animName : CName, callback : CName )
	{
		m_animProxy = PlayLibraryAnimation( animName );
		m_animProxy.RegisterToCallback( inkanimEventType.OnFinish, this, callback );
	}

	protected event OnOutroComplete( e : inkAnimProxy )
	{
		SetReadyToRemove();
	}

}

