import { Component } from '@angular/core';
import {IonicPage, NavController, NavParams, ToastController} from 'ionic-angular';
import { BggGameSearch } from '../../providers/bggGames-service';
import { UsersService } from '../../providers/users-service';
import { GamesService } from '../../providers/games-service';
import { SettingsPageModule } from '../settings/settings.module';
 
@IonicPage()
@Component({
  selector: 'page-game-search',
  templateUrl: 'game-search.html',
})
export class GameSearchPage {
    game = {
      bggGameId:'',
      name:'',
      thumbnail: '',
      // mechanics:'',
    };
    searchResult: any;
    searchTerm: '';

  constructor(public navCtrl: NavController, public navParams: NavParams, public toastCtrl: ToastController, public bggGames: BggGameSearch, public usersService: UsersService, public gamesService: GamesService) {
  }
  
  ionViewDidLoad() {
  }

  searchGame(){
    console.log("search method called");
    this.bggGames.searchGame(this.searchTerm).subscribe(games=>{
      this.searchResult = games;
      this.searchResult = Array.of(this.searchResult);
    })
    ;
  }

  async doAddToOwnedList(bggGameId, name, thumbnail, mechanics) {
    await this.setGame(bggGameId, name, thumbnail, mechanics);
    this.gamesService.addGame(this.game).subscribe(response =>{
      console.log(response)
      this.usersService.addGameToUserLibrary(this.gamesService.setGameId(response));
    });
    let toast = this.toastCtrl.create({
      message: 'Game Added To MyGames',
      duration: 2000,
      position: 'bottom'
    });
    toast.present();
  }

  setGame(bggGameId, name, thumbnail, mechanics) {
    this.game.name = name;
    this.game.thumbnail = thumbnail;
    //this.game.mechanics = mechanics;
    this.game.bggGameId = bggGameId;
  }

  async doAddToPlayedList(bggGameId, name, thumbnail, mechanics) {
      await this.setGame(bggGameId, name, thumbnail, mechanics);
      let toast = this.toastCtrl.create({
        message: 'Game Added To PlayedGames',
        duration: 2000,
        position: 'bottom'
      });
      toast.present();

    }

    doAddToOwnedAndPlayedList() {
      let toast = this.toastCtrl.create({
        message: 'Game Added To MyPlayedGames',
        duration: 2000,
        position: 'bottom'
      });
      toast.present();

    }
}
