struct AIStackSignalConditionData
{
	var m_callbackId : Uint32;
	var m_lastValue : Bool;
}

class AIStackSignalCondition extends AIbehaviorStackScriptPassiveExpressionDefinition
{
	editable var m_signalName : CName;

	public export function OnActivate( context : ScriptExecutionContext, data : ref< AIStackSignalConditionData > )
	{
		data.m_callbackId = AddBehaviorCallback( context, 'OnHighestPrioritySignalBecameDirty' );
		data.m_lastValue = false;
	}

	public export function OnDeactivate( context : ScriptExecutionContext, data : ref< AIStackSignalConditionData > )
	{
		RemoveBehaviorCallback( context, data.m_callbackId );
	}

	public export function CalculateValue( context : ScriptExecutionContext, data : ref< AIStackSignalConditionData > ) : Variant
	{
		var signalId : Uint32;
		var signal : AIGateSignal;
		if( GetSignalHandler( context ).GetHighestPrioritySignal( signal, signalId ) )
		{
			if( AIGateSignal.HasTag( signal, m_signalName ) )
			{
				data.m_lastValue = true;
				return true;
			}
			data.m_lastValue = false;
			return false;
		}
		else
		{
			return data.m_lastValue;
		}
		return false;
	}

	protected final function GetSignalHandler( context : ScriptExecutionContext ) : AISignalHandlerComponent
	{
		return GetPuppet( context ).GetSignalHandlerComponent();
	}

	public static function GetPuppet( context : ScriptExecutionContext ) : ScriptedPuppet
	{
		return ( ( ScriptedPuppet )( ScriptExecutionContext.GetOwner( context ) ) );
	}

	public function GetEditorSubCaption() : String
	{
		return ( "GateSignal(" + ( ( String )( m_signalName ) ) ) + ")";
	}

}

class AIGateSignalSender extends AIbehaviortaskStackScript
{
	mutable editable var tags : array< CName >;
	mutable editable var flags : array< EAIGateSignalFlags >;
	editable var priority : Float;

	public export override function GetInstanceTypeName() : CName
	{
		return 'script_ref:Uint32';
	}

	public export function OnActivate( context : ScriptExecutionContext, signalId : ref< Uint32 > )
	{
		var signal : AIGateSignal;
		var i : Int32;
		signal.priority = priority;
		signal.lifeTime = GetSignalLifeTime();
		for( i = 0; i < tags.Size(); i += 1 )
		{
			AIGateSignal.AddTag( signal, tags[ i ] );
		}
		for( i = 0; i < flags.Size(); i += 1 )
		{
			AIGateSignal.AddFlag( signal, flags[ i ] );
		}
		signalId = GetPuppet( context ).GetSignalHandlerComponent().AddSignal( signal );
	}

	public export function OnDeactivate( context : ScriptExecutionContext, signalId : ref< Uint32 > )
	{
		GetPuppet( context ).GetSignalHandlerComponent().RemoveSignal( signalId );
	}

	public virtual function GetSignalLifeTime() : Float
	{
		return 0.5;
	}

	public function GetEditorSubCaption() : String
	{
		var result : String;
		var i : Int32;
		for( i = 0; i < tags.Size(); i += 1 )
		{
			if( i > 0 )
			{
				result += ".";
			}
			result += ( ( String )( tags[ i ] ) );
		}
		result += ( ( " (" + priority ) + ")" );
		return result;
	}

}

