package org.hirw.game.util;

public final class Time {
  private static long lastTime = NanoTimer.system().nanoTime();
  private static long currentTime = NanoTimer.system().nanoTime();
  private static NanoTimer nanoTimer = NanoTimer.system();

  public interface NanoTimer {
    long nanoTime();

    static NanoTimer system() {
      return System::nanoTime;
    }
  }

  static void setNanoTimer(NanoTimer timer) {
    nanoTimer = timer;
    lastTime = nanoTimer.nanoTime();
    currentTime = nanoTimer.nanoTime();
  }

  public static void update() {
    lastTime = currentTime;
    currentTime = nanoTimer.nanoTime();
  }

  public static float deltaTime() {
    final float ONE_SECOND = 1_000_000_000f;
    return (currentTime - lastTime) / ONE_SECOND;
  }
}
