'use strict';
import HeroFactory from '../game-model/heroes/hero-factory';
import { PhaserScene } from '../../common/utils/const/phaser-scene-enum';
import GameWorldScene from '../views/game-world-scene';
import { GameDeckMode } from '../../common/utils/const/game-deck-mode-enum';
import FactionDeckBuildingScene from '../views/faction-deck-building-scene';
import Player from '../game-model/player';

export default class GameControllerPnp {
  constructor(phaserEngine, gameParams) {
    this.phaserEngine = phaserEngine;
    console.log("In constructor" + this.phaserEngine);
    this.gameParams = gameParams;

    this.playerBlue = new Player(gameParams.playerBlueName, "Blue");
    this.playerRed = new Player(gameParams.playerResName, "Red");

    console.log();
    switch (this.gameParams.gameDeckMode) {
      case GameDeckMode.FACTIONS:
        this.deckBuildingScene = new FactionDeckBuildingScene(this.gameEventListener());
        break;

      default:
        // ONLY Factions mode
        this.deckBuildingScene = new FactionDeckBuildingScene(this.gameEventListener());
        break;
    }

    // Instantiates Heroes
    this.heroFactory = new HeroFactory();
  }

  displayDeckBuilding() {
    console.log("In displayDeckBuilding " + this.phaserEngine);
    this.phaserEngine.scene.add(PhaserScene.DECK_BUILDING, this.deckBuildingScene);
    this.phaserEngine.scene.start(PhaserScene.DECK_BUILDING);
    this.phaserEngine.scene.remove(PhaserScene.MAIN_MENU);
  }
  displayGameWorld() {
    this.phaserEngine.scene.add(PhaserScene.GAME_WORLD, this.gameWorldScene);
    this.phaserEngine.scene.start (PhaserScene.GAME_WORLD);
    this.phaserEngine.scene.remove(PhaserScene.DECK_BUILDING);
  }

  gameEventListener() {
    return {
      onFactionsSelected: this.onFactionsSelected.bind(this)
    };
  }

  onFactionsSelected(playerBlueFaction, playerRedFaction ) {
    this.playerBlue.setFaction(playerBlueFaction);
    this.playerRed.setFaction(playerRedFaction);

    this.playerBlue.setHeroesDeck(this.heroFactory.getFactionRandom(this.playerBlue.faction));
    this.playerRed.setHeroesDeck(this.heroFactory.getFactionRandom(this.playerRed.faction));

    console.log('player Blue deck : ', this.playerBlue.getHeroesDeck());
    console.log('player Red deck : ', this.playerRed.getHeroesDeck());
    this.gameWorldScene = new GameWorldScene(this.playerBlue, this.playerRed);
    this.displayGameWorld();
  }

}
