import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { filter, startWith, switchMap, tap } from 'rxjs/operators';
import { OidcAuthService } from 'src/app/core/service/oidc/oidc.service';
import { AppTokenService } from 'src/app/core/service/security/app-token.service';

@Component({
  selector: 'app-toolbar',
  templateUrl: './toolbar.component.html',
  styleUrls: ['./toolbar.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ToolbarComponent implements OnInit {
  user;
  name;
  navigator$ = new Observable()
  constructor(
    private oidc: OidcAuthService,
    private appTokenSV: AppTokenService,
    private router: Router,
    private activeRoute: ActivatedRoute,
  ) { }

  ngOnInit(): void {
    this.user = this.appTokenSV.user
    this.navigator$ = this.router.events.pipe(
      filter(r => r instanceof NavigationEnd),
      startWith(this.router),
      switchMap(r => this.activeRoute.firstChild.data),
    )
  }

  logout(){
    this.oidc.logout()
  }

  changePassword(){
    this.router.navigate(['change-password'])
  }

}
