import { Component, OnInit } from "@angular/core";
import { Router } from "@angular/router";

import { User } from "../models/user";

import { AuthenticationService } from "../services/authentication.service";
import { ServiceComm } from "../services/service-comm.service";
import { ConfigurationHelper } from "../services/configuration-helper";

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.css"]
})
export class AppComponent implements OnInit {
  public title = "app";
  public showMenu = false;
  public currentUser: User;
  public version: string;

  constructor(private router: Router,
              private authenticationService: AuthenticationService,
              private serviceComm: ServiceComm) {
    this.authenticationService.currentUser.subscribe(user => { this.currentUser = user; });

    ConfigurationHelper.settings.subscribe(settings => 
      {
          if (settings != null) {
            this.version = settings.version;
          }
      });
  }

  ngOnInit() {
    this.serviceComm.componentTitle.subscribe(title => (this.title = title));
  }

  show() {
    return this.authenticationService.currentUserValue != undefined;
  }

  logout() {
    this.authenticationService.logout();
    this.showMenu = !this.showMenu;
    this.router.navigate(["/login"], { skipLocationChange: true });
  }
}
