class LocalPredictionSim extends LocalSim {
  constructor(product, showDate) {
    super(product, showDate);

    product.data.images.forEach((img) => {
      img = img.replace(/[.](?=.*[.])/g, ":"); // replace all . with : except last (file extension)
      var imgObj = new HanisImage(
        img.split(".")[0],
        `${product.data.path}/${img}`
      );
      this.allImages.push(imgObj);
    });
    this.spreadOverlays(product);

    this.allImages.sort((a, b) => a.startTime - b.startTime);
    this.availableImages = this.allImages;

    this.htmlContent = parseHanis(
      this.product.name,
      this.availableImages.map((displayedImg) => displayedImg.fullPath),
      this.availableImages.map((displayedImg) => displayedImg.startTime),
      this.product.data.overlays?.map((overlay) => ({
        overlay_name: overlay.overlay_name,
        overlay_images: overlay.overlay_images.map(
          (img) => `${overlay.overlay_path}/${img}`
        ),
      })),
      this.showDate,
      true
    );
  }

  spreadOverlays(product) {
    if (!product.data.overlays) {
      return;
    }

    const imagesMapped = product.data.images.map((i) =>
      luxon.DateTime.fromISO(i.replace(/[.](?=.*[.])/g, ":").split(".")[0])
    );

    product.data.overlays.forEach((o) => {
      // static, one-to-one overlays don't need to be spread
      if (
        o.overlay_images.length === 1 ||
        o.overlay_images.length === product.data.images.length
      ) {
        return;
      }

      let spreadOverlay = [];

      const overlaysMapped = o.overlay_images.map((oi) =>
        luxon.DateTime.fromISO(
          oi.replace(/[.](?=.*[.])/g, ":").split(".")[0]
        )
      );

      let i = 0;
      let j = 0;

      while (i < product.data.images.length) {
        if (imagesMapped[i++] <= overlaysMapped[j]) {
          spreadOverlay.push(o.overlay_images[j]);
          continue;
        }
        if (
          j + 1 >= o.overlay_images.length ||
          imagesMapped[i] < overlaysMapped[j + 1]
        ) {
          spreadOverlay.push(o.overlay_images[j]);
        } else {
          spreadOverlay.push(o.overlay_images[j++]);
        }
      }

      o.overlay_images = spreadOverlay;
    });
  }

  onTick(currentTime) {
    if (!this.isOpen) return;
  }

  populateImageQueue(currentTime) {
    // override and dont do anything
  }
}
