class TaskManager {
  tasks;
  hiddenTasks;
  displayedTasks;

  constructor(tasks) {
    this.tasks = tasks.map(
      (task) =>
        new Task(task.title, task.timestamp, task.data.type, task.data.content)
    );
    this.hiddenTasks = this.tasks;
    this.displayedTasks = [];
  }

  clockTickObservable;
  newTimeObservable;

  loadTask(title) {
    const task = this.tasks.find((task) => task.title === title).getContent();
  }

  subscribe(clockTickObservable, newTimeObservable) {
    const { throttleTime } = rxjs;

    this.clockTickObservable = clockTickObservable
      .pipe(throttleTime(1000))
      .subscribe((currentTime) => {
        this.onTick(currentTime);
      });

    this.newTimeObservable = newTimeObservable.subscribe((currentTime) => {
      this.populateTaskQueue(currentTime);
    });
  }

  unsubscribe() {
    this.clockTickObservable.unsubscribe();
    this.newTimeObservable.unsubscribe();
  }

  reset() {
    this.tasks.forEach((task) => (task.seen = false));
    $("#tasksList").html({});
  }

  populateTaskQueue(currentTime) {
    this.hiddenTasks = this.tasks;
    this.displayedTasks = [];
  }

  onTick(currentTime) {
    for (const task of this.hiddenTasks) {
      if (task.timestamp < currentTime) {
        this.displayedTasks.push(task);
        this.hiddenTasks = this.hiddenTasks.filter((t) => t !== task);
        let tasksList = parseTaskList(this.displayedTasks);
        $("#tasksListContent").html(tasksList);
      }
    }

    const notSeenTasksCount = this.displayedTasks.filter(task => task.seen === false).length 

    if(notSeenTasksCount > 0){
      $("#newTasksCount").html(notSeenTasksCount);
      $("#newTasksCount").attr("style", "display: inline-block;");
    }        
    else {
      $("#newTasksCount").attr("style", "display: none;");
    }
  }
}
