import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { EncryptDecryptService } from './EncryptDecryptService';

@Injectable()
export class AuthService {
  public LoggedIn = 'LoggedIn';
  constructor(private router: Router, private encryptDecrypt: EncryptDecryptService) { }
  sessionSetItem(key, value) {
    localStorage.setItem(this.encryptDecrypt.encrypt(key), this.encryptDecrypt.encrypt(value));
  }
  sessionGetItem(key) {

    let decryptedValue = localStorage.getItem(this.encryptDecrypt.encrypt(key));
    return decryptedValue != null ? this.encryptDecrypt.decrypt(decryptedValue) : null;

  }

  sessionRemoveItem(key) {
    localStorage.removeItem(this.encryptDecrypt.encrypt(key));
  }
  // Set User Secure Token
  setSecureToken(secure_token: string) {

    localStorage.setItem('LoggedIn', secure_token);
  }

  // Set User Secure Token
  getSecureToken() {
    return localStorage.getItem('LoggedIn');
  }

  // Check User is LoggedIn or not!
  isLoggednIn() {
    return this.getSecureToken() !== null;
  }

  // Logout method
  logout() {
    localStorage.clear();
    localStorage.removeItem('LoggedIn');
    this.router.navigate(['login']);
  }
}
