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

import { BreadcrumbService } from './breadcrumb.service';
import { MenuItemComponent } from './components/menu/menuitem.component';
import { AuthenticationService } from '../providers/user/authentication.service';

@Component({
  standalone: true,
  selector: 'app-breadcrumb',
  templateUrl: './app.breadcrumb.component.html',
  imports: [RouterLink]
})
export class AppBreadcrumbComponent implements OnDestroy
{
  private readonly router = inject(Router);
  public readonly bcService = inject(BreadcrumbService);
  private readonly authService = inject(AuthenticationService);

  public items: MenuItemComponent[];
  public subscription: Subscription;

  constructor()
  {
    this.subscription = this.bcService.itemsHandler.subscribe(response =>
    {
      this.items = response;
    });
  }

  public gotoHome(): void
  {
    if (this.authService?.currentUserValue)
    {
      if (this.authService.currentUserValue.dbOnStartup)
        this.router.navigate(['/dashboard']);
      else
        this.router.navigate(['/home']);
    }
  }

  ngOnDestroy()
  {
    if (this.subscription)
    {
      this.subscription.unsubscribe();
    }
  }
}
