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

export class User {
  id:number;
  profileName:String;
  userName:String;
  summary:String;

  constructor(id:number, profileName:String, userName:String, summary:String){
    this.id = id;
    this.profileName = profileName;
    this.userName = userName;
    this.summary = summary;
  }
}

@Injectable()
export class UsersService{
  public API = "https://tabletoptracker.herokuapp.com";
  public USERS_API = this.API + "/users";
  public GAMES_API = this.API + "/games";
  public user: User;
  public userId: any;

  constructor(public http: HttpClient){
  }

  addUser(account): Observable<any> {
    console.log(account);
    return this.http.post(this.USERS_API,account);
  }

  setUser(account){
    this.userId = account.id;
    this.user = account;
    console.log(this.userId);
  }

  getUser() {
    return this.user;
  }

  updateProfile(profile): Observable<any>{
    return this.http.patch(this.USERS_API + "/" + this.userId, profile);
  }

  getUserProfile(): Observable<any>{
    return this.http.get(this.USERS_API+ "/id/" + this.userId);
  }

  getUserIdByUserName(userName:String){
    return this.http.get(this.USERS_API + "/username/" + userName);
  }

  addGameToUserLibrary(gameId){
    console.log(gameId);
    console.log(this.USERS_API + "/" + this.userId + "/library");
    return this.http.put(this.USERS_API + "/" + this.userId + "/library", gameId);
  }

  getUsersGames(userId){
    return this.http.get(this.GAMES_API+"/library/"+userId);
  }
}