import {Component, OnDestroy, ViewEncapsulation} from "@angular/core";
import {
  Event,
  NavigationCancel,
  NavigationEnd,
  NavigationError,
  NavigationStart,
  Router
} from "@angular/router";
import {ProgressIndicationService} from "@webbpm/base-package";
import {Subscription} from "rxjs";


@Component({
             moduleId: module.id,
             encapsulation: ViewEncapsulation.ShadowDom,
             selector: 'mfe-webbpm',
             templateUrl: './../../../../../src/resources/template/webbpm/mfe-webbpm.html',
             styleUrls: ['./../../../../../src/resources/css/style.css'],
           })
export class MfeWebbpmComponent implements OnDestroy {
  public headerVisible: boolean = true;
  public footerVisible: boolean = true;
  private subscription: Subscription;

  constructor(private router: Router,
              private progressIndicationService: ProgressIndicationService) {
   this.subscription = router.events.subscribe((event: Event) => {
     this.handleRouterEvent(event);
    })
  }

  private handleRouterEvent(event: Event): void {
    if (event instanceof NavigationStart) {
      this.progressIndicationService.showProgressBar();
    }
    else if (
        event instanceof NavigationEnd
        || event instanceof NavigationError
        || event instanceof NavigationCancel
    ) {
      this.progressIndicationService.hideProgressBar();
    }
  }

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