import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
import { tokenNotExpired } from 'angular2-jwt';

import { environment } from '../../environments/environment';

const apiUrl = environment.apiUrl;

@Injectable()
export class AuthService {
  authToken: any;
  user: any;

  constructor(private http: Http) { }

  registerUser (user) {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    return this.http.post(apiUrl + 'users/register', user, {headers: headers})
      .map(res => res.json());
  }

  authenticateUser (user) {
    let headers = new Headers();
    headers.append('Content-Type', 'application/json');
    return this.http.post(apiUrl + 'users/authenticate', user, {headers: headers})
      .map(res => res.json());
  }

  getProfile () {
    let headers = new Headers();
    this.loadToken();
    headers.append('Authorization',this.authToken);
    headers.append('Content-Type', 'application/json');
    return this.http.get(apiUrl + 'users/profile', {headers: headers})
      .map(res => res.json());
  }

  storeUserData (token, user) {
    localStorage.setItem('id_token', token);
    localStorage.setItem('user', JSON.stringify(user));
    this.authToken = token;
    this.user = user;
  }

  loadToken () {
    const token = localStorage.getItem('id_token');
    this.authToken = token;
  }

  loggedIn () {
    // console.log(tokenNotExpired());
    // return true;
    return tokenNotExpired('id_token');
  }
    this.user = null;
    localStorage.clear();
  }
}

  logout () {
    this.authToken = null;