class SimManager {
  model;
  sims;

  constructor(model) {
    this.model = model;
    this.sims = [];

    for (const data_type of this.model.data_types) {
      for (const product of data_type.products) {
        if (product.source === "local") {
          if (product.data.type === "prediction") {
            this.sims.push(new LocalPredictionSim(product, this.model.time.revealDates));
          }
          // type === observation
          else {
            product.data.validity_time === true
              ? this.sims.push(new LocalSimValidityTime(product, this.model.time.revealDates))
              : this.sims.push(new LocalSimMaxFrames(product, this.model.time.revealDates));
          }
        } 
        // source === WMS        
        else {
          this.sims.push(new WMSSim(product, this.model.time.revealDates));
        }
      }
    }
  }

  loadSim(title) {
    this.sims
      .filter((sim) => sim.product.name !== title)
      .forEach((sim) => sim.close());

    var sim = this.sims.find((sim) => sim.product.name === title);
    if (!sim.isOpen) {
      sim.open();
    }
  }

  subscribe(clockTickObservable, newTimeObservable) {
    this.sims.forEach((sim) => {
      sim.subscribe(clockTickObservable, newTimeObservable);
    });
  }

  unsubscribe() {
    this.sims.forEach((sim) => {
      sim.unsubscribe();
    });
  }
}
