import { Injectable } from "@angular/core";
import { CanActivate, CanActivateChild, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from "@angular/router";
import { Observable } from "rxjs";
import { OidcAuthService } from "../service/oidc/oidc.service";
@Injectable()
export class AuthGuard implements CanActivate,CanActivateChild {

  constructor(
    private odicSV: OidcAuthService
  ){

  }
  canActivateChild(childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean | UrlTree | Observable<boolean | UrlTree> | Promise<boolean | UrlTree> {
    return this.canActivate(childRoute,state)
  }

  canActivate(
      next: ActivatedRouteSnapshot,
      state: RouterStateSnapshot
    ): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
    if (this.odicSV.isLoggedIn()) {
        return true
    }

      this.odicSV.startAuthentication()
      return false;
  }

}
