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

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

  tabs;
  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('sensor/')[1].split('/')[0];
      this.tabs = parsedUrl === 'list' || 'setting' ? null : [
        { label: 'INFO', routerLink: ['/sensor/', parsedUrl, 'info'] },
        { label: 'HISTORY', path: ['/sensor/', parsedUrl, 'history'], disabled: true },
      ];
    } catch {
      this.router.navigate(['error']);
    }
  }

}
