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

export class Game{
  id: number;
  title: string;
  studio: string;
  platform: string;
  imageURL: string;
  
  constructor(id:number, title: string, studio: string, platform: string, imageURL: string){
    this.id = id;
    this.title = title;
    this.studio = studio;
    this.platform = platform;
    this.imageURL = imageURL;
  }
}

@Injectable()
export class GameService{
  public API = "https://backlogg.herokuapp.com/";
  // public API = "http://localhost:8080";
  public GAME_API = this.API + "/games";
  public GAME_USER_API = this.API + "users/";
  public currentGame: Game;
  public currentGameId: number;
  public currentUserId: number;
  public 

  constructor(public http: HttpClient){
  }

  getGames(): Observable<any> {
    return this.http.get(this.GAME_API);
  }

  getGame(id: number): Observable<any> {
    return this.http.get(this.GAME_API + "/id/" + id);
  }

  getGameByUserId(id: number): Observable<any> {
    return this.http.get(this.GAME_USER_API + id + "/library");
  }

  addGame(credentials): Observable<any>{
      // At this point store the credentials to your backend!
      return this.http.post(this.GAME_API, credentials);
  }

  addGameToUser(credentials): Observable<any>{
    return this.http.put(this.GAME_USER_API + this.currentUserId + "/library",credentials);
  }

  getGameByName(credentials): Observable<any>{
    return this.http.get(this.GAME_API + "/name/" + credentials);
  }

  getImageURL(gameTitle: string): Observable<any>{
    var url = "https://api-endpoint.igdb.com/games/?search=" + gameTitle + "&fields=name,cover,summary";
    let header = new HttpHeaders();
    header = header.set("user-key","4aafe5d7db305154aa7b60f13bad89bd");
    header = header.set('content-type','application/json');
      
    return this.http.get(url,{headers:header});
  }

  

}
