class NPCInitTask extends AIbehaviortaskStackScript
{
	editable var m_preventSkippingDeathAnimation : Bool;
	default m_preventSkippingDeathAnimation = true;

	public export function OnActivate( context : ScriptExecutionContext )
	{
		var puppet : NPCPuppet;
		SendSetScriptExecutionContextEvent( context );
		if( NPCWasDeadOnInit( context ) )
		{
			ScriptExecutionContext.SetArgumentBool( context, 'WasDeadOnInit', true );
			NPCPuppet.ChangeHighLevelState( ScriptExecutionContext.GetOwner( context ), gamedataNPCHighLevelState.Dead );
			puppet = GetNPCPuppet( context );
			if( puppet )
			{
				if( m_preventSkippingDeathAnimation )
				{
					puppet.SetSkipDeathAnimation( false );
				}
			}
			puppet.DisableCollision();
			SendSignal( context, 'downed', 'death', EAIGateSignalFlags.AIGSF_OverridesSelf, 10.0 );
		}
		else if( NPCWasAlertedOnInit( context ) )
		{
			SendSignal( context, 'autonomous', 'alerted', EAIGateSignalFlags.AIGSF_Undefined, 3.0 );
		}
		else
		{
			NPCPuppet.ChangeHighLevelState( ScriptExecutionContext.GetOwner( context ), gamedataNPCHighLevelState.Relaxed );
		}
	}

	private function NPCWasDeadOnInit( context : ref< ScriptExecutionContext > ) : Bool
	{
		if( GameInstance.GetStatPoolsSystem( ScriptExecutionContext.GetOwner( context ).GetGame() ).HasStatPoolValueReachedMin( ScriptExecutionContext.GetOwner( context ).GetEntityID(), gamedataStatPoolType.Health ) )
		{
			if( StatusEffectSystem.ObjectHasStatusEffect( ScriptExecutionContext.GetOwner( context ), T"WorkspotStatus.Death" ) )
			{
				return false;
			}
			return true;
		}
		if( HasHLS( context, gamedataNPCHighLevelState.Dead ) )
		{
			return true;
		}
		return false;
	}

	private function NPCWasAlertedOnInit( context : ref< ScriptExecutionContext > ) : Bool
	{
		return ScriptExecutionContext.GetArgumentBool( context, 'IsInAlerted' );
	}

	private function HasHLS( context : ref< ScriptExecutionContext >, state : gamedataNPCHighLevelState ) : Bool
	{
		return state == ( ( gamedataNPCHighLevelState )( GetPuppet( context ).GetPuppetStateBlackboard().GetInt( GetAllBlackboardDefs().PuppetState.HighLevel ) ) );
	}

	private function SendSignal( context : ref< ScriptExecutionContext >, tag1 : CName, tag2 : CName, flag : EAIGateSignalFlags, priority : Float )
	{
		var signal : AIGateSignal;
		var signalId : Uint32;
		signal.priority = priority;
		signal.lifeTime = 0.5;
		AIGateSignal.AddTag( signal, tag1 );
		AIGateSignal.AddTag( signal, tag2 );
		if( flag != EAIGateSignalFlags.AIGSF_Undefined )
		{
			AIGateSignal.AddFlag( signal, flag );
		}
		signalId = GetPuppet( context ).GetSignalHandlerComponent().AddSignal( signal );
		GetPuppet( context ).GetSignalHandlerComponent().RemoveSignal( signalId );
	}

	private function SendSetScriptExecutionContextEvent( context : ref< ScriptExecutionContext > )
	{
		var evnt : SetScriptExecutionContextEvent;
		evnt = new SetScriptExecutionContextEvent;
		evnt.scriptExecutionContext = context;
		ScriptExecutionContext.GetOwner( context ).QueueEvent( evnt );
	}

}

