import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';
import { UserPages } from '../models/users/userPages';
import { SessionStorageService } from './services/sessionStorage.service';

@Injectable()
export class AuthGuard implements CanActivate {

    public userPagePermission: UserPages[] = new Array<UserPages>();

    constructor(
        private sessionStorageService: SessionStorageService,
        private myRoute: Router
    ) {
    }
    canActivate(
        next: ActivatedRouteSnapshot,
        state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
        if (sessionStorage.getItem("PagePermission") && JSON.parse(sessionStorage.getItem("PagePermission") ?? 'null'!).length > 0)
            this.userPagePermission = JSON.parse(sessionStorage.getItem("PagePermission") ?? 'null'!) as UserPages[];
        let credential = JSON.parse(sessionStorage.getItem("Credential") ?? 'null'!) as any;
        if (credential && credential.userId) {

            let url = state.url.toString().split('/')[2];
            if (credential.userRole.roleId != 1) {
                let ind = this.userPagePermission.findIndex(c => c.name.toLowerCase() == url.toLowerCase())
                if (ind >= 0)
                    return true;
                else {
                    this.myRoute.navigate(["admin/permission-denied"]);
                    return false;
                }
            }
            else {
                return true;
            }
        } else {
            this.myRoute.navigate(["admin/login"]);
            return false;
        }
    }
}