// Angular
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router';
// RxJS
import { Observable } from 'rxjs';
import { securityFunctions } from '../common/security';
@Injectable()
export class AuthGuard implements CanActivate {
    constructor(
        private router: Router) { }

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
        if (securityFunctions.getSessionStorage('LoginUserData')) {
            let userdata = securityFunctions.getSessionStorage('LoginUserData') ? JSON.parse(securityFunctions.getSessionStorage('LoginUserData')) : null
            if (userdata) {
                if (userdata.user_id) {
                    // let tpinApplicable = sessionStorage.getItem('TpinApplicable') == 'true' ? true : false;
                    // let tpinActivated = sessionStorage.getItem('T_Activated') == 'true' ? true : false;
                    // let tpinExpired = sessionStorage.getItem('T_Expired') == 'true' ? true : false;
                    // if (tpinApplicable) {
                    //     if (!tpinActivated || tpinExpired) {
                    //         let userId = sessionStorage.getItem('userId')
                    //         const now = new Date();
                    //         const formattedDate = now.toISOString().slice(0, 19).replace('T', ' ');
                    //         let keyVal = securityFunctions.EncryptReq(userId + '~' + formattedDate + '~' + (tpinExpired ? 'expired' : 'create')).trim()
                    //         this.router.navigate(['/Login/TPIN'], { queryParams: { key: keyVal } })
                    //         return false;
                    //     }
                    //     else {
                    //         return true
                    //     }
                    // }
                    return true
                }
                return false
            }
            return false;
        } else {
            window.location.href = '/Auth/Login';
            return false;
        }
    }
}
