import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { LocalStorageService } from './services/local-storage/local-storage.service';
import { ModalService } from './services/modal/modal.service';

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

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