import { Injectable } from '@angular/core';
import { Router, ActivatedRoute, RouterEvent, NavigationEnd } from '@angular/router';
import { TranslateService } from '@ngx-translate/core';

@Injectable()
export class RouterExtService {
  private lang: string = undefined;
  private previousUrl: string = undefined;
  private currentUrl: string = undefined;

  constructor(private router: Router,
              private route: ActivatedRoute,
              private translateService: TranslateService) {
    this.lang = this.translateService.currentLang || this.translateService.defaultLang;
    this.currentUrl = this.router.url;
    router.events.subscribe(event => {
      if (event instanceof NavigationEnd) {
        if (event.url !== this.currentUrl) {
          this.lang = this.translateService.currentLang || this.translateService.defaultLang;
          this.previousUrl = this.currentUrl;
          this.currentUrl = event.url;
        }
      }
    });
  }

  public getLang(): string {
    return this.lang;
  }

  public getRouter(): Router {
    return this.router;
  }

  public getActivatedRoute(): ActivatedRoute {
    return this.route;
  }

  public getPreviousUrl(): string {
    return this.previousUrl;
  }

  public goToHome(): void {
    this.router.navigate(['/', this.lang, 'discover']);
  }

  public goToPage(path: string[]): void {
    path = ['/', this.lang].concat(path);
    this.router.navigate(path);
  }
}
