
using Godot;


namespace Rokojori
{  
  [Tool][GlobalClass, Icon("res://addons/rokojori_action_library/Icons/OnEvent.svg") ]
  public partial class OnSensor: Node
  {
      [Export]
    public Sensor sensor;

    [Export]
    public Action onStart;    

    [Export]
    public Action onActive;    

    [Export]
    public Action onEnd;    

    [Export]
    public bool onlyWhenNotConsumed = true;

    [Export]
    public bool consumeEvent = true;

    
    [ExportGroup("Condition")]    
    [Export]
    public Condition condition;

    [Export]
    public SceneCondition sceneCondition;

    public override void _Process( double delta )
    {     
      if ( sensor == null )
      {
        return;
      }

      if ( onlyWhenNotConsumed && sensor.consumed )
      {
        return;
      }

      if ( condition != null && ! condition.Evaluate() )
      {
        return;
      }

      if ( sceneCondition != null && ! sceneCondition.Evaluate() )
      {
        return;
      }


      if ( sensor.isDown )
      { 
        Action.Trigger( onStart );

        if ( consumeEvent && onStart != null )
        {
          sensor.Consume();
          return;
        }
      }

      if ( sensor.isHold )  
      {
        Action.Trigger( onActive );

        if ( consumeEvent && onActive != null )
        {
          sensor.Consume();
          return;
        }
      }

      if ( sensor.isUp )  
      {
        Action.Trigger( onEnd );

        if ( consumeEvent && onEnd != null )
        {
          sensor.Consume();
          return;
        }
      }

    }
  }
}