import { MatDialog } from "@angular/material";
import { Router } from "@angular/router";
import { NgxSpinnerService } from "ngx-spinner";
import { ToastrService } from "ngx-toastr";
import { OnlineusersService } from "./onlineusers.service";

import { Component, OnInit, TemplateRef,HostListener } from "@angular/core";
import { FormBuilder, FormGroup, Validators } from "@angular/forms";

import { DialogService } from "ng2-bootstrap-modal";
import { BsModalRef, BsModalService, ModalOptions } from "ngx-bootstrap";
import { interval } from "rxjs";
import { mergeMap } from "rxjs/operators";

@Component({
  selector: "app-onlineusers",
  templateUrl: "./onlineusers.component.html",
  styleUrls: ["./onlineusers.component.css"],
})
export class OnlineusersComponent implements OnInit {
  onlineuserslst: any = [];
  intervalId;
  p = 1;
  public searchText: string;
  operation: any;
  itemsPerPage = 10;
  userid: string = localStorage.getItem("userid");
  key = "errortype";
  reverse = false;
  actionRights: any;
  offline: boolean = false;
  mode: string;
  constructor(
    private router: Router,
    private dialog: MatDialog,
    private onlineusersservice: OnlineusersService,
    private formBuilder: FormBuilder,
    private SpinnerService: NgxSpinnerService,
    private toastr: ToastrService
  ) {
    if (this.router.getCurrentNavigation().extras.state !== undefined) {
      localStorage.setItem(
        "OnlineUsers_menuid",
        this.router.getCurrentNavigation().extras.state.menuid
      );
      localStorage.setItem(
        "OnlineUsers_userRights",
        this.router.getCurrentNavigation().extras.state.action
      );
      this.actionRights = [];
      this.actionRights =
        this.router.getCurrentNavigation().extras.state.action;
    } else {
      this.actionRights = [];
      this.actionRights = localStorage.getItem("OnlineUsers_userRights");
    }
  }

  ngOnDestroy(): void {
    clearInterval(this.intervalId);
  }

  async ngOnInit() {
    this.mode = "login";
    await this.onload();
    this.auto_call();
  }

  onload() {
    this.onlineusersservice.Getloginoutuser(this.mode).subscribe(
      (res) => {
        //debugger;
        if (
          res.body[0] != null &&
          res.body[0] != undefined &&
          res.body[0] != ""
        ) {
          this.onlineuserslst = JSON.parse(res.body[0]);
          console.log(this.onlineuserslst);
          // console.log('onload');
        } else {
          this.onlineuserslst = [];
        }
      },
      (err) => {
        this.toastr.error("Getting while fetching the Online Users data.");
      }
    );
  }

  auto_call() {
    this.intervalId = setInterval(() => {
      this.onload();
      console.log("auto call");
    }, 0.2 * 60 * 1000);
    // interval(1 * 60 * 1000)
    // .pipe(
    //     mergeMap(() => this.onlineusersservice.Getloginoutuser(this.mode))
    // )
    // .subscribe(res => {
    //   if(res.body[0] != null && res.body[0] != undefined && res.body[0] != ''){
    //     this.onlineuserslst = JSON.parse(res.body[0]);
    //     console.log(this.onlineuserslst);
    //     console.log('Auto call');
    //     // console.log(JSON.parse(res.body[0]));

    //   }else{
    //     this.onlineuserslst = [];
    //   }
    // } );
  }

  sort(key) {
    this.key = key;
    this.reverse = !this.reverse;
  }
  checkbox(evt) {
    if (evt.target.checked == true) {
      this.mode = "logout";
    } else {
      this.mode = "login";
    }
    this.onload();
  }

  @HostListener('window:focus', ['$event'])
  onFocus(event: FocusEvent): void {  
    this.ngOnInit();
  }

  @HostListener('window:blur', ['$event'])
  onBlur(event: FocusEvent): void {  
    clearInterval(this.intervalId);
  }
}
