import { Component, OnDestroy } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
import { Subscription } from 'rxjs/Subscription';

@Component({
  selector: 'of-pages-probe',
  templateUrl: './probe-page.component.html',
})
export class ProbePageComponent implements OnDestroy {

  private tabs = undefined;
  private routerSubscription$: Subscription;

  constructor(
    private router: Router
  ) {
    this.routerSubscription$ = this.router.events.subscribe((event) => {
      if (event instanceof NavigationEnd) {
        this.generateTabMenu(event);
      }
    });
  }

  ngOnDestroy() {
    this.routerSubscription$.unsubscribe();
  }

  generateTabMenu(event: NavigationEnd) {
    try {
      const parsedUrl = event.url.split('probe/')[1].split('/')[0];
      switch (parsedUrl) {
        case 'list':
        case 'noauth':
        case 'download':
          this.tabs = [
            { label: 'PROBES', routerLink: ['/probe/list'] },
            { label: 'UNAUTHORIZED', routerLink: ['/probe/noauth'] },
            { label: 'DOWNLOAD', routerLink: ['/probe/download'] },
          ];
          break;
        default:
          this.tabs = [
            { label: 'INFO', routerLink: ['/probe/', parsedUrl, 'info'] },
            { label: 'TARGETS', routerLink: ['/probe/', parsedUrl, 'targets'], disabled: true },
            { label: 'HISTORY', path: ['/probe/', parsedUrl, 'history'], disabled: true },
          ];
          break;
      }
    } catch {
      this.router.navigate(['error']);
    }
  }

}
