import { HttpClient, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import * as JWT from 'jwt-decode';
import { tokenNotExpired, JwtHelper } from 'angular2-jwt';
import { Observable, Subject } from 'rxjs';
import { ActivatedRoute } from '@angular/router';
import { JwtHelperService } from '@auth0/angular-jwt';
import { Location } from '@angular/common';




@Injectable({
  providedIn: 'root'
})

export class AuthServiceService {

  constructor(private http:  HttpClient, private location: Location, private route: ActivatedRoute) { }
  
  jwtHelper: JwtHelper = new JwtHelper();

  //user permision name
  addButton: string = 'add_user';
  editButton: string = 'edit_user';
  deleteButton: string = 'delete_uder';
  showAddBtn: boolean = false;
  showDeleteBtn:boolean = false;
  showEditBtn: boolean = false;
  showReportBtn: boolean = false;
  showPrintBtn: boolean = false;
  showPermissionsBtn: boolean = false;
  showHeaderLogin: boolean = false;
  showHeaderDashBoard: boolean = false;
  showMoreLinks:boolean = false;
  showDashboardHeader: boolean = false;
  internalHeader: boolean = false;

  arabicTemplate: boolean = true;
  arabicLanguage: boolean = true;
  englishLanguage:boolean = false;
  currentDate: number = Date.now();

  pathApi: string = 'http://hospital.rabbittec.com/back_end/api';
  pathImg: string = "http://hospital.rabbittec.com/back_end/public/";
  myInnerHeight = window.innerHeight;
  imgSrc: string =  '../../assets/image/avatar.png'; //page profile default
  iconImg: string = '../../assets/image/Group 92.png';

  showSearchHeader: boolean = true;


  cachedRequests: Array<HttpRequest<any>> = [];
  notificationLogin: boolean = true;
  
  dataLoginUser = {};
  //login function
  login(dataForm) {
    console.log(dataForm);
    return this.http.post(this.pathApi + '/login', dataForm);
  } 

  //signup Function
  signUp(dataSignUp){
    console.log(dataSignUp);
    return this.http.post(this.pathApi + '/register', dataSignUp);
  }

  //return data user function 
  setDataUser(user) {
    this.dataLoginUser = user.user;
    console.log('data loooogin' , this.dataLoginUser);
  }

  
  //Authentication 
  public getToken(): string {
    return localStorage.getItem('token');
  }

  public isAuthenticated(): boolean {
    // get the token
    const token = this.getToken();
    // console.log('toooooken',this.jwtHelper.isTokenExpired(token));
    // return a boolean reflecting 
    // whether or not the token is expired
    return this.jwtHelper.isTokenExpired(token);
  }

 
  public collectFailedRequest(request): void {
    this.cachedRequests.push(request);
  }
  

  //previous back locarion
  perviousLocation() {
    this.location.back();
  }


  //language content 
  onLanguageContent(typeLang:string) {
    if(typeLang == 'ar') {
      this.arabicTemplate = true;
      this.arabicLanguage = true;
      this.englishLanguage = false;
    } else if(typeLang == 'en'){
      this.arabicTemplate = false;
      this.arabicLanguage = false;
      this.englishLanguage = true;
    }
  }

   writeHijri(date, lang) {
    var date = date || new Date();
    lang = lang || 'en';
    var options = {
      year: 'numeric', month: 'long', day: 'numeric'
    };
    return date.toLocaleString(lang + '-u-ca-islamic', options);
  }


}
