'use strict';

import MainMenuScene from '../views/main-menu-scene';
import { PhaserScene } from '../../common/utils/const/phaser-scene-enum';
import GameParams from '../../common/game-params';
import GameControllerPnp from '../../game/control/game-controller-pnp';
import SocketService from '../../common/socket-service';

export default class MenuController {

  constructor(phaserEngine) {
    this.phaserEngine = phaserEngine;

    this.socketService = new SocketService();

    this.mainMenuScene = new MainMenuScene(this.menuEventListener());
  }

  displayMainMenu() {

    this.phaserEngine.scene.add(PhaserScene.MAIN_MENU,this.mainMenuScene);
    this.phaserEngine.scene.start(PhaserScene.MAIN_MENU);
  }


  tryConnect(username) {
    return this.socketService.connect(username);
  }

  onRequestDisconnection() {
    this.socketService.disconnect();
  }

  menuEventListener() {
    return {
      onNewPnpGame: this.onNewPnpGame.bind(this),
      onOnlineGamesListRequested: this.onOnlineGamesListRequested.bind(this),
      onTryConnect: this.tryConnect.bind(this),
      onRequestDisconnection: this.onRequestDisconnection.bind(this),
      onGameCreationRequest: this.onGameCreationRequest.bind(this)
    };
  }

  onNewPnpGame(pBlueName, pRedName, gameMode, advancedRules) {
    console.log("onNewPnpGame this  : " + this);
      let gameParams = new GameParams(pBlueName,pRedName,gameMode,advancedRules);
      let gameControllerPnp = new GameControllerPnp(this.phaserEngine, gameParams);
      gameControllerPnp.displayDeckBuilding();
  }

  onOnlineGamesListRequested(playername) {
    return this.socketService.getGamesList(playername);
  }

  onGameCreationRequest(playername, deckModeSelected, advancedOptions) {
    let game = {
      player1: playername,
      player2: "",
      deck: deckModeSelected,
      advRules: advancedOptions,
      status: 'CREATED',
      data: "",
    };
    return this.socketService.createGame(game);
  }
}
