import {ChangeDetectorRef, Component, OnInit} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {CookieService} from "ngx-cookie";

@Component({
  moduleId: module.id,
  selector: "[log-out]",
  templateUrl: "../../../../../src/resources/template/app/component/log_out.html"
})
export class LogOutComponent implements OnInit{

  private userFullname: string;

  constructor(private httpClient: HttpClient,
                    private cookieService: CookieService, private cd: ChangeDetectorRef) {
  }

  ngOnInit(): void {
    let isAuth = this.getIsAuth();
    if (isAuth) {
      Promise.all([
                    this.httpClient.get<string>("esia/userfullname").toPromise(),
                  ]).then(([userFullname]) => {
        this.userFullname = userFullname;
        this.cd.markForCheck();
      });
    }
  }

  public logout(): void {
    this.httpClient.get<string>("esia/logout").toPromise().then(url => {
      window.open(url, "_self");
    })
  }

  public getUserFullname(): string {
    return this.userFullname;
  }

  public getIsAuth(): boolean {
    return this.cookieService.get("webbpm.ervu-lkrp-fl") != null;
  }
}