import { Viewer } from "../Viewer";

const DELAY_TIME_MULTIPLEXER = 2.0;
const START_UPDATE_TIME = 1000;

export enum UpdateType {
  kDelay,
  kNormal,
  kForce,
}

export class UpdaterController {
  private lastUpdate: number;
  private viewer: Viewer;
  public delayUpdateTime: number;

  constructor() {
    this.lastUpdate = 0;
    this.delayUpdateTime = START_UPDATE_TIME;
  }

  initialize(viewer: Viewer) {
    this.viewer = viewer;
    this.lastUpdate = performance.now();
  }

  update(type: UpdateType) {
    const isNeedUpdate = type !== UpdateType.kDelay || performance.now() - this.lastUpdate >= this.delayUpdateTime;
    const isForce = type === UpdateType.kForce;

    if (isNeedUpdate) {
      this.viewer.update(isForce);
      this.lastUpdate = performance.now();
      this.delayUpdateTime *= DELAY_TIME_MULTIPLEXER;
    }
  }
}
