import { Injectable } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';

@Injectable({
  providedIn: 'root'
})
export class UrlService {

  private previousUrl: string | undefined = undefined;
  private currentUrl: string | undefined = undefined;
  private checkpointUrl: string | undefined = undefined;

  constructor(private router : Router) {
    this.currentUrl = undefined;
    router.events.subscribe(event => {
      if (event instanceof NavigationEnd) {       
        this.previousUrl = this.currentUrl;
        this.currentUrl = event.url;
      };
    });
  }

  public getCurrentUrl() {
    return this.currentUrl;
  }

  public getPreviousUrl() {
    return this.previousUrl;
  }   
  
  public getCheckpointUrl() {
    return this.checkpointUrl;
  }

  public saveCheckpoint(possibleUrls: string[]) {
    possibleUrls.forEach(url => {
      if(this.previousUrl == '/' + url) {
        this.checkpointUrl = this.previousUrl;
      }
    });
  }
}
