import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ModalController } from 'ionic-angular';
import {GameService} from "../../providers/game-service";
import { AuthServiceProvider } from '../../providers/auth-service';
import { GameModalPage } from './game-modal';

/**
 * Generated class for the GamesPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-games',
  templateUrl: 'games.html',
})
export class GamesPage {
  private games: Array<any>;
  private userGames: Array<any>
  private singleGame: any ={};
  gameDetails: Array<any>;

  constructor(public navCtrl: NavController, public navParams: NavParams,
              public gameService: GameService, public modalCtrl: ModalController,
              public authService: AuthServiceProvider) {
  }

  ionViewDidEnter() {
    // setInterval(() => {
    this.getGamesByUserId();
    // }, 5000);
  }

  ionViewDidLoad(){
    this.getGamesByUserId();
  }
  

  getImageURL(){
    this.gameService.getImageURL(this.gameService.currentGame.title).subscribe(games => {
      this.gameDetails = games;
      console.log(games.summary);
    })
  }

  sortGamesByName(){
    console.log("WHY");
    this.userGames.sort();
  }

  getGamesByUserId(){
    this.gameService.getGameByUserId(this.authService.currentUserId).subscribe(games => {
      this.userGames = games;
    })
  }

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

  openModal(){
    console.log("openModal works!");
    let modal = this.modalCtrl.create(GameModalPage);
    modal.present();

    modal.onDidDismiss(() => this.ionViewDidLoad())
  }

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

  doUpdateGame(){
    console.log("updateGame works!");
  }

  // visible = false;
  doCompleteGame(){
  console.log("completeGame works!");
  //  this.visible = !this.visible;
  //  this.ionViewDidEnter();
  }


}
