import {Behavior, Visible} from "@webbpm/base-package";
import {HttpClient} from "@angular/common/http";
import {AuthenticationService} from "../modules/security/authentication.service";

export class FileUploadChecker extends Behavior {
  private httpClient: HttpClient;
  private authService: AuthenticationService;
  private allowed: boolean = false;

  initialize() {
    this.httpClient = this.injector.get(HttpClient);
    this.authService = this.injector.get(AuthenticationService);
  }

  postStart() {
    super.postStart();
    if (this.authService.isAuthenticated()){
      this.checkUploadPermission();
    }
  }

  @Visible()
  public fileUploadAllowed(): boolean {
    return this.allowed;
  }

  private checkUploadPermission(): void {
    this.httpClient.get<boolean>('upload/access')
        .toPromise()
        .then(response => {
          this.allowed = response;
        });
  }
}