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

@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate {

  constructor(
    private router: Router,
    private authService: AuthService
  ) {}

  canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): boolean {

      if (this.authService.isReported) {
        this.router.navigate(['/landing']);
        return false;
      }

      if (this.authService.isLoggedIn) {
        return true;
      }

      this.router.navigate(['/landing']);
      return false;
  }
}
