import {ChangeDetectorRef, Component} from "@angular/core";
import {
  Event,
  NavigationCancel,
  NavigationEnd,
  NavigationError,
  NavigationStart,
  Router
} from "@angular/router";
import {ProgressIndicationService} from "@webbpm/base-package";
import {AuditConstants, AuditService} from "../../../ervu/service/AuditService";

@Component({
  moduleId: module.id,
  selector: '[webbpm]',
  templateUrl: './../../../../../src/resources/template/webbpm/webbpm.html'
})
export class WebbpmComponent {
  public headerVisible: boolean = true;
  public footerVisible: boolean = true;
  public isLanding: boolean = false;

  constructor(private router: Router,
              private progressIndicationService: ProgressIndicationService,
              private cd: ChangeDetectorRef,
              private auditService: AuditService) {
    router.events.subscribe((event: Event) => {
      if (event instanceof NavigationStart) {
        progressIndicationService.showProgressBar();
        this.isLanding = event.url == '/home';
        this.cd.markForCheck();
      }
      else if (event instanceof NavigationEnd
               || event instanceof NavigationError
               || event instanceof NavigationCancel) {
        progressIndicationService.hideProgressBar();
        this.auditService.logActionAudit(AuditConstants.OPEN_PAGE_EVENT);
      }
    })
  }
}
