import { Injectable } from "@angular/core";
import {
  Router,
  CanActivate,
  ActivatedRouteSnapshot,
  RouterStateSnapshot
} from "@angular/router";
import { LoginService } from "./service/login.service";
import { HttpService } from "app/service/http.service";

@Injectable()
export class AuthGuard implements CanActivate {
  constructor(private httpService: HttpService, private router: Router) {}

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
    if (!this.httpService._token) {
      this.router.navigate(["/login"]);
      return false;
    }
    if (this.httpService._permissions.indexOf(state.url) === -1) {
      this.router.navigate(["/login"]);
      return false;
    }

    return true;
  }
}
