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

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

  constructor(private elementRef: ElementRef, private router: Router,
              private progressIndicationService: ProgressIndicationService) {
    router.events.subscribe((event: Event) => {
      if (event instanceof NavigationStart) {
        progressIndicationService.showProgressBar();
      }
      else if (event instanceof NavigationEnd
          || event instanceof NavigationError
          || event instanceof NavigationCancel) {
        progressIndicationService.hideProgressBar();
      }

      if (event instanceof NavigationEnd) {
        let navigationEnd = (<NavigationEnd>event);
        $(elementRef.nativeElement).removeClass('citizen');
        $(elementRef.nativeElement).removeClass('citizencard');

        if (navigationEnd.urlAfterRedirects.startsWith('/citizencard/')) {
          $(elementRef.nativeElement).addClass('citizencard');
        }
        else if (navigationEnd.urlAfterRedirects.startsWith('/citizen') ||
                 navigationEnd.urlAfterRedirects.startsWith('/citizen_all_filters')) {
          $(elementRef.nativeElement).addClass('citizen');
        }
      }
    })
  }
}
