import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanActivateChild } from '@angular/router';
import { Observable ,  of } from 'rxjs';
// import { CognitoService } from '../AwsService/cognito.service';
@Injectable()
export class AuthGuard implements CanActivate, CanActivateChild {

    constructor(private router: Router) { }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {

        const token = localStorage.getItem('user_token'); 
       
        if(token != '' && token != undefined && token != null){
            return true
        }else{
           // alert("else");
           // this.router.navigate(['auth/login']);
            this.router.navigate(['/auth/login'])
            return false;
        }

        // if (this.shared.getToken()) {
        //     alert("if");
        //     // this.router.navigate(['']);
        //     return true
        // } else {
        //     alert("else");
        //     this.router.navigate(['/login']);
        //     return false;
        // }
    }

    canActivateChild(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {

        const token = localStorage.getItem('user_token'); 
        console.log(token)
        if(token != '' && token != undefined && token != null){
            return true
        }else{
         //   setTimeout(() => {
                this.router.navigate(['/auth/login']);
                return false;
              //  },1000);
        }
       
        // if (this.shared.getCurrentUser() != null && this.shared.getToken()) {
        //     // alert("if");
            
        //         return true;
        // } else {
        //     // alert("else");
        //     setTimeout(() => {
        //     this.router.navigate(['authentication/login']);
        //     return false;
        //     },1000);
            
        // }
    }

    // public getToken(): string {
    //     return localStorage.getItem('token');
    // }

    // public logOut() {
    //     localStorage.clear();
    //     if(localStorage.getItem('token') === null){
    //         this.router.navigate(['/login']);
    //     }
    // }
}