import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { SessionStore } from '../store/session.service';

@Injectable({
  providedIn: 'root'
})
export class InativityService {

  constructor(
    private router: Router,
  ) { 

    var t;
    window.onload = resetTimer;
    window.onmousemove = resetTimer;
    window.onmousedown = resetTimer;  // catches touchscreen presses as well      
    window.ontouchstart = resetTimer; // catches touchscreen swipes as well 
    window.onclick = resetTimer;      // catches touchpad clicks as well
    window.onkeydown = resetTimer;   
    window.addEventListener('scroll', resetTimer, true); // improved; see comments

    function userIsNotActive() {
      // your function for too long inactivity goes here
      SessionStore.setInativity(false)
      // alert('go out')
      window['inactivity/function']()
    }

    function resetTimer() {
      clearTimeout(t);
      t = setTimeout(userIsNotActive, 60000 * 15);  // time is in milliseconds
    }
  }
}
