import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree } from '@angular/router';
import { Observable } from 'rxjs';
import { SessionStore } from '../store/session.service';
import { AlertController, Platform } from '@ionic/angular';

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

  constructor(
    private router:Router,
    private platform: Platform,
  ){}

  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {

      if (this.platform.is('desktop') || this.platform.is('mobileweb')) {
        this.router.navigate(['/home/events']);
        return false
      } else if(SessionStore.exist && SessionStore.user.Inactivity && !SessionStore.hasPin ) {
        return true
      } else if(SessionStore.exist && !SessionStore.user.Inactivity) {
        return true
      } else {
        this.router.navigate(['/home/events']);
        return false
      }
      
  }
  
}
