import { Component } from '@angular/core';
import { NavController, ToastController } from 'ionic-angular';
import { GameService } from '../../providers/game-service';

@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  private games: Array<any>;

  constructor(public navCtrl: NavController,
              public gameService: GameService,
              public toastCtrl: ToastController) {

  }


  ionViewDidEnter() {
    // setInterval(() => {
    // this.getAllGames();
    // }, 5000);
    console.log("MILK");
    this.getAllGames();
  }

  ionViewWillLeave() {
    console.log("Looks like I'm about to leave :(");
  }

  getAllGames(){
    this.gameService.getGames().subscribe(games => {
      console.log("chocolate")
      this.games= games;
    })
  }

  openSummary(gameId){
    console.log("openSummary works!");
  }

  doAddGame(gameId){
    
    this.gameService.addGameToUser(gameId.id).subscribe( response => {
      console.log(response);
      let toast = this.toastCtrl.create({
        message: 'Game Added to Library',
        duration: 3000,
        position: 'top'
       });
      toast.present();
      console.log("post toast");
  });
  }

}
