class LocalSimValidityTime extends LocalSim {
  displayedImages;

  constructor(product, showDate) {
    super(product, showDate);
    this.displayedImages = [];

    product.data.images.forEach((img) => {
      img = img.replace(/[.](?=.*[.])/g, ":"); // replace all . with : except last (file extension)
      var imgTimes = img.split("_");

      var imgObj = new HanisImageValidityTime(
        imgTimes[0],
        imgTimes[1].substr(0, imgTimes[1].lastIndexOf(".")),
        `${product.data.path}/${img}`
      );
      this.allImages.push(imgObj);
    });

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

    this.availableImages = this.allImages;
    this.init();
  }

  populateImageQueue(currentTime) {
    this.displayedImages = [];
    this.availableImages = this.allImages.filter(
      (imgObj) => imgObj.endTime < currentTime
    );
  }

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

    for (const img of this.displayedImages) {
      if (currentTime > img.endTime || currentTime < img.startTime) {
        this.displayedImages = this.displayedImages.filter(
          (displayedImage) => img !== displayedImage
        );
        this.htmlContent = parseHanis(
          this.product.name,
          this.displayedImages.map((displayedImg) => displayedImg.fullPath),
          this.displayedImages.map((displayedImg) => displayedImg.startTime),
          this.product.data.overlays?.map((overlay) => ({
            overlay_name: overlay.overlay_name,
            overlay_images: overlay.overlay_images.map(
              (img) => `${overlay.overlay_label}/${img}`
            ),
          })),
          this.showDate,
          true
        );

        $("#main").html(this.htmlContent);
      }
    }

    for (const img of this.availableImages) {
      if (img.startTime < currentTime) {
        // console.log("adding img: ", img);
        this.displayedImages.push(img);
        this.availableImages = this.availableImages.filter(
          (availableImg) => img !== availableImg
        );

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

        $("#main").html(this.htmlContent);
      }
    }
  }
}
