import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { LocalStorageService } from './services/localstorage.service';
import { LoginModalService } from './services/login-modal.service';

@Injectable({
  providedIn: 'root',
})
export class AuthGuard implements CanActivate {
  constructor(
    private localStorageService: LocalStorageService,
    private router: Router,
    private loginModalService: LoginModalService
  ) {}

  canActivate(): boolean {
    const token = this.localStorageService.getToken();
    if (token) {
      return true;
    } else {
      this.router.navigate(['/']).then(() => {
        this.loginModalService.openModal();
      });
      return false;
    }
  }
}
