import { Component, Renderer2 } from '@angular/core';
import * as moment from 'moment';
import { Subscription, interval } from 'rxjs';
import { CommonFunctionService } from './core/common/common-function.service';
import { API } from './core/services/api.service';
import { RouteChangeService } from './core/common/RouteChangeService.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
})
export class AppComponent {
  // displayPosition = false;
  // position: string;
  title = "AngularStructure";
  constructor(private renderer: Renderer2, public commonService: CommonFunctionService, private ApiSer: API, private routeChangeService: RouteChangeService) {
    // Check if userId exists in sessionStorage
    if (sessionStorage.getItem('LoginUserData')) {
      // Set up an interval that runs every second
      this.commonService.StartTimer();
    } else {
      // If userId doesn't exist in sessionStorage
      // Set displayPosition to false, remove 'excd' from sessionStorage, and unsubscribe from the subscription
      this.commonService.displayPosition = false;
      sessionStorage.removeItem('excd');
      this.commonService.subscription?.unsubscribe();
    }

  }
  stopTimer() {
    if (this.commonService.subscription) {
      this.commonService.subscription.unsubscribe();
      this.commonService.subscription = undefined;
    }
  };

  ngOnInit(): void {
    //sessionStorage.getItem("userId")
    //  ? ""
    //  : (window.location.href = "/Login/Login");
    // const links = this.renderer.createElement("link");
    // links.setAttribute("rel", "stylesheet");
    // links.setAttribute("type", "text/css");
    // links.setAttribute(
    //   "href",
    //   isTheme ? "../assets/css/empire/e-default.css" : "../assets/css/retina/r-default.css"
    // );
    // this.renderer.appendChild(document.head, links);
    // const link = this.renderer.createElement("link");
    // link.setAttribute("rel", "stylesheet");
    // link.setAttribute("type", "text/css");
    // link.setAttribute(
    //   "href",
    //   isTheme ? "../assets/css/empire.css" : "../assets/css/retina.css"
    // );
    // this.renderer.appendChild(document.head, link);
    // this.commonService.GetOrgDetails();
    // this.route.queryParams.subscribe(obj => {
    //   console.log(obj)
    //   // if(obj.key) CommonFunctions.DecryptReq("UfCP%2FE7upxltDZyVLfTj%2F8fMMbudOAFfwLCR1RVqPXNvD%203jblAZ3pQ0i9LtrXpE0DFYbSnyHNj353T2bJx9Zg%3D%3D");

    // })
    // sessionStorage.getItem('userId') ? '' : window.location.href = '/Login/Login'
  }

  showPositionDialog(position: string) {
    this.commonService.position = position;
    this.commonService.displayPosition = true;
  };

  ngOnDestroy() {
    // Unsubscribe from the observable to prevent memory leaks
    this.commonService.subscription.unsubscribe();
  }
  extendJWTTime() {
    this.stopTimer();
    let postdata = {
      "JwtToken": sessionStorage.getItem('JwtToken'),
      "RefreshToken": sessionStorage.getItem('RefreshToken')
    };
    this.ApiSer.AdminPost('/JWT/RefreshToken', postdata).subscribe(res => {
      if (!res.Status) {
        this.commonService.loginOut();
      } else {
        sessionStorage.setItem('JwtToken', res.JwtToken);
        sessionStorage.setItem('RefreshToken', res.RefreshToken);
        sessionStorage.setItem('JwtTokenExpiry', res.JwtTokenExpiry);
        this.Close(true)
        this.commonService.StartTimer();
      }
    })
  };
  // remainingTime: number;
  exit() {
    this.commonService.displayPosition = false;
    sessionStorage.removeItem('excd');
    this.commonService.loginOut();
  };
  Close(check) {
    this.commonService.displayPosition = false;
    sessionStorage.setItem('excd', 'OK')
    if (check) {
      setTimeout(function () {
        sessionStorage.removeItem('excd');
      }, 10000); // 10000 milliseconds = 10 second
    }
  }
}
