import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router, CanActivateChild } from '@angular/router';
import { Observable } from 'rxjs';
import { routePath } from 'src/app/_providers/_resources/route-path.properties';

/*
    Created By  : Arun Joy
    Created On  : 04-01-2020
    Created For : For handling the route path of the application.
                  For provding security i.e it blocks unautherized acess of rote paths.
*/

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

  constructor(
    private router: Router
  ) { }

  // canActivate(
  //   next: ActivatedRouteSnapshot,
  //   state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {

  //     const currentUser = localStorage.getItem("authtoken");
  //     // console.log("===currentUser====",currentUser);
  //     if (!currentUser) {
  //         return true;
  //     }
  //     this.router.navigate([routePath.dashboard]);
  //     return false;

  // }

  async canActivate(
    next: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Promise<boolean> | Promise<boolean> {

    const currentUser = localStorage.getItem("authtoken");
    const seculevel = localStorage.getItem("seculevel");
    if (!currentUser) {
      //   await this.lan.init("en","ar");
      return true;
    }
    if (seculevel == 'Analyst')
      this.router.navigate([routePath.analyst]);
    else if (seculevel == 'Admin')
      this.router.navigate([routePath.admin]);
    else if (seculevel == 'SuperAdmin')
      this.router.navigate([routePath.superAdmin]);
    else if (seculevel == 'User')
      this.router.navigate([routePath.user])
    return false;

  }
}
