
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System;
using Godot;


namespace Rokojori
{  
  public partial class TimeLineManager:NetworkNode
  {        
    public static void RemoveEvent( TimeLine timeline, int id )
    {
      RJLog.Log( "Removing tick:", timeline, id );

      timeline = TimeLineManager.Ensure( timeline );
      var runner = timeline.runner;

      runner.RemoveEvent( id );

    }

    public static TimeLineCallback ScheduleCallback( TimeLine timeline, System.Action<TimeLineCallback> callback )
    {
      timeline = TimeLineManager.Ensure( timeline );
      var runner = timeline.runner;
      var eventID = TimeLineManager.CreateID();

      return runner._ScheduleCallback( callback, eventID );

    }

    public static TimeLineEvent ScheduleEvent( TimeLine timeline, float position, Action<TimeLineEvent> callback )
    {
      timeline = TimeLineManager.Ensure( timeline );
      var runner = timeline.runner;
      var eventID = TimeLineManager.CreateID();

      return runner._ScheduleEvent( position, eventID, false, callback );
    }

    public static TimeLineEvent ScheduleEventIn( TimeLine timeline, float offset, Action<TimeLineEvent> callback )
    {
      timeline = TimeLineManager.Ensure( timeline );
      var runner = timeline.runner;
      var position = timeline.position + offset;
      var eventID = TimeLineManager.CreateID();

      return runner._ScheduleEvent( position, eventID, false, callback );
    }

    public static TimeLineEvent ScheduleEventWith( Duration duration, Action<TimeLineEvent> callback )
    {
      return ScheduleEventIn( duration.timeLine, duration.GetDurationInSeconds(), callback );
    }

    public static TimeLineEvent ScheduleLoopEvent( TimeLine timeline, float duration, float offset, Action<TimeLineEvent> callback )
    {
      timeline = TimeLineManager.Ensure( timeline );
      var runner = timeline.runner;
      var eventID = TimeLineManager.CreateID();

      return runner._ScheduleLoopEvent( duration, offset, eventID, false, callback );
    }


    public static TimeLineSpan ScheduleSpan( TimeLine timeline, float start, float end, Action<TimeLineSpan,TimeLineSpanUpdateType> callback )
    {
      timeline = TimeLineManager.Ensure( timeline );
      var runner = timeline.runner;
      var spanID = TimeLineManager.CreateID();
      var duration = end - start;

      return runner._ScheduleSpan( start, end, spanID, false, callback );
    }

    public static TimeLineSpan ScheduleSpanWith( Duration duration, Action<TimeLineSpan,TimeLineSpanUpdateType> callback )
    {
      return ScheduleSpanIn( duration.timeLine, 0, duration.GetDurationInSeconds(), callback );
    }

    public static TimeLineSpan ScheduleSpanIn( TimeLine timeline, float delay, float duration, Action<TimeLineSpan,TimeLineSpanUpdateType> callback )
    {
      timeline = TimeLineManager.Ensure( timeline );
      var runner = timeline.runner;

      if ( runner == null )
      {
        return null;
      }

      var start = timeline.position + delay;
      var end = start + duration;

      var spanID = TimeLineManager.CreateID();

      return runner._ScheduleSpan( start, end, spanID, false, callback );
    }

  
  }
}