'use strict';
import { shuffleHeroes } from '../heroesHelper';
import stateMixins from './stateMixins';
export default function selectTournamentState(duelController) {
  stateMixins.call(this);
  let duelCtrl = duelController;
  this.start = () => {
    /** @type {import('type/game').TH_GameDataStore} */
    let gameData = duelCtrl.getGameData();
    gameData.game['waitingFor/blue'] = true;
    gameData.game['waitingFor/red'] = true;
    gameData.game.currentPlayer = 'both';
    duelCtrl.storeData(gameData);
  };

  this.update = (
    /** @type {import('type/comm').TH_MessageTournamentDeckStep} */ payload = null
  ) => {
    /** @type {import('type/game').TH_GameDataStore} */
    let gameData = duelCtrl.getGameData();
    if (payload) {
      let player = payload.color + 'player';
      /** @type {import('type/game').TH_HeroInGame[]} */
      let twelveHeroes = [];
      payload.twelveHeroesIds.forEach(id => {
        twelveHeroes.push({ id, position: 'pile', possibleActions: [] });
      });
      let randomHeroes = shuffleHeroes(twelveHeroes);
      // Init player twelve heroes
      gameData[player].twelveHeroes = randomHeroes;
      gameData.game['waitingFor/' + payload.color] = false;
      duelCtrl.storeData(gameData);
    }

    if (
      gameData.game['waitingFor/blue'] === false &&
      gameData.game['waitingFor/red'] === false
    ) {
      duelCtrl.endCurrentState();
    }
  };

  this.end = () => {
    console.log('Go to next game state : 2_CHANGE_UP_TO_3_CARDS');

    duelCtrl.setCurrentState('2_CHANGE_UP_TO_3_CARDS');
    duelCtrl.startCurrentState();
  };
}
