class SequenceController {
  Event[] eventQueue;
  
  SequenceController() {
    eventQueue = new Event[0];
  }
  
  void addLinear(String functionName) { addLinear(functionName, -1); }
  void addLinear(String functionName, float delay) {
    Event newEvent = new Event(functionName, delay);
    eventQueue = (Event[]) append(eventQueue, newEvent);
  }
  void addLinear(String functionName, String alertName) {
    Event newEvent = new Event(functionName, alertName);
    eventQueue = (Event[]) append(eventQueue, newEvent);
  }
  
  void start() {
    // Just execute the first event.
    if (eventQueue.length <= 0) return;
    eventQueue[0].execute(null);
  }
  
  
  void executeNextEvent(String howLastEventEnded) {
    // Remove the current one from the list
    eventQueue = (Event[]) subset(eventQueue, 1);
    // Is there another one on queue?
    if (eventQueue.length > 0) {
      Event nextEvent = eventQueue[0];
      nextEvent.execute(howLastEventEnded);
    }
    
    // Hey! Do another check to see if there's no delay nor alert between this and the next event.
    if (eventQueue.length>0) {
      Event nextEvent = eventQueue[0];
      if (nextEvent.alertName == null) { // If the next event has nothing to wait for...
        executeNextEvent(null); // ...go ahead and execute it right away!
      }
    }
  }
  
  
  void update() {
    // If there are events in the queue...
    if (eventQueue.length > 0) {
      // Update the event in the front of our queue.
      Event currentEvent = eventQueue[0];
      currentEvent.update();
    }
  }
  
  void alert(String whatHappened) {
    alert(whatHappened, null);
  }
  void alert(String whatHappened, String howItHappened) {
    // If there are events in the queue...
    if (eventQueue.length > 0) {
      // Notify the event in the front of our queue.
      Event currentEvent = eventQueue[0];
      currentEvent.alert(whatHappened, howItHappened);
    }
  }
  
}



