import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import {Observable} from "rxjs/Observable";
import "rxjs/add/operator/catch";

export class Game{
  id: number;
  title: string;
  studio: string;
  platform: string;
  
  constructor(id:number, title: string, studio: string, platform: string){
    this.id = id;
    this.title = title;
    this.studio = studio;
    this.platform = platform;
  }
}
export class User{
  id: number;
  name: string;
  email: string;
  gameLibrary: Game[];


  constructor(id:number,name: string, email: string, gameLibrary: Game[]){
    this.id = id;
    this.name = name;
    this.email = email;
    this.gameLibrary = gameLibrary;
  }
}

@Injectable()
export class AuthServiceProvider {

  public currentUser: User;
  public currentUserId: number;

  public LOGIN_URL = 'https://backlogg.herokuapp.com/users';
  public REGISTER_URL = 'https://backlogg.herokuapp.com/users/register';

  // public LOGIN_URL = 'http://localhost:8080/users';
  // public REGISTER_URL = 'http://localhost:8080/users/register';

  constructor(public http: HttpClient) {}

  public register(credentials): Observable<any> {
      // At this point store the credentials to your backend!
      //return this.setUser(this.http.post(this.REGISTER_URL, credentials));
      return this.http.post(this.REGISTER_URL, credentials);
  }
  
  login(email): Observable<any>{
    console.log("BEFORE REQUEST: " + email);
    return this.http.get(this.LOGIN_URL + "/email/" + email);
  }
  getUsers(): Observable<any> {
    return this.http.get(this.LOGIN_URL);
  }

  getThisUser(): Observable<any> {
    return this.http.get(this.LOGIN_URL + "/" + this.currentUserId);
  }

}
