package game.ui;

import base.GameSprite;

class Speech extends GameSprite {
  public var current: SpeechSentence = null;

  var queue: Array<SpeechSentence> = [];

  public function new() {
    super();
  }

  function next() {
    if(current != null) {
      removeChild(current);
      current = null;
    }

    if(queue.length > 0) {
      current = queue.shift();
      current.y = Main.SCREEN_HEIGHT - current.height - 3;
      addChild(current);
    } 
  }

  public function pushSentance(text: String, time: Null<Float> = null, callback: Void -> Void = null) {
    queue.push(new SpeechSentence(text, time, next, callback));
  }

  public function now(text: String, time: Null<Float> = null, callback: Void -> Void = null) {
    queue.unshift(new SpeechSentence(text, time, next, callback));
    next();
  }

  public function stopAll() {
    queue = [];
    next();
  }

  override public function update(delta: Float) {
    super.update(delta);

    if((queue.length > 0) && (current == null)) {
      next();
    }
  }
}
