
using Godot;


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

    bool _active = false;

    [Export]
    public bool active
    {
      get => _active;
      set 
      {
        SetActive( value );
      } 
    } 

    [Export]
    public float duration;
    
    [Export]
    public float offset;

    [Export]
    public TimeLine timeLine;

    int _eventID = -1;

    void SetActive( bool active )
    {
      if ( active == _active || Engine.IsEditorHint() )
      {
        return;
      }

      _active = active;

      if ( _active )
      { 
        if ( _eventID != -1 )
        {
          TimeLineManager.RemoveEvent( timeLine, _eventID );
          return;
        }

        var tle = TimeLineManager.ScheduleLoopEvent( timeLine, duration, offset, tle => Action.Trigger( action ) ); 

        _eventID = tle.id;
      }
      else
      {
        if ( _eventID == -1 )
        {
          return;
        }

        TimeLineManager.RemoveEvent( timeLine, _eventID );
        _eventID = -1;
      }

       

    }
  }
}