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

@Injectable({
  providedIn: 'root'
})
export class LoginGuard implements CanActivate {
  constructor( private router:Router, private platform: Platform,) {

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

      // console.log(SessionStore.exist, SessionStore.user.Inactivity, SessionStore.user.LoginPreference)

      console.log(SessionStore.exist, SessionStore.user.Inactivity, SessionStore.user.LoginPreference)
      if(SessionStore.exist && SessionStore.user.Inactivity && SessionStore.user.LoginPreference != 'Pin' ) {
        this.router.navigate(['/home/events']);
        return false
      } else if(SessionStore.exist && !SessionStore.hasPin && !this.platform.is('desktop') && !this.platform.is('mobileweb') ) {
        this.router.navigate(['/pin']);
        return false
      } else if(SessionStore.exist && !SessionStore.user.Inactivity && SessionStore.user.LoginPreference == 'Pin' && !this.platform.is('desktop') && !this.platform.is('mobileweb')) {
        this.router.navigate(['/inactivity']);
        return false
      } else {
        return true
      }
  }

}
