import { Injectable } from "@angular/core";
import {
  CanActivate,
  Router,
  ActivatedRouteSnapshot,
  RouterStateSnapshot
} from "@angular/router";
import { AuthService } from "../services/auth2.service";

@Injectable({
  providedIn: "root"
})
export class AuthGuard implements CanActivate {
  constructor(private authService: AuthService, private router: Router) {}

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    if (this.authService.isLoggedIn()) {
      //const userLevel = +this.authService.getUserLevel();
      /*if (route.data.roles && route.data.roles.indexOf(userLevel) === -1) {
        // role not authorised so redirect to home page
        this.router.navigate(['/']);
        return false;
      }*/
      return true;
    } else {
      this.router.navigate(["login"], {
        queryParams: { returnUrl: state.url }
      });
      return !this.authService.isLoggedIn();
    }
  }
}
