'use strict';
import { shuffleHeroes, getDraftSets } from '../heroesHelper.js';
import stateMixins from './stateMixins.js';
export default function selectDraftState(duelController) {
  stateMixins.call(this);
  let duelCtrl = duelController;
  let draftSets = [];

  this.start = () => {
    /** @type {import('type/game').TH_GameDataStore} */
    let gameData = duelCtrl.getGameData();
    draftSets = getDraftSets(
      gameData.game.allHeroes,
      gameData.game.advRules.includes('popularity')
    );

    gameData.bluePlayer.draftHeroesIds = draftSets[0][0];
    gameData.redPlayer.draftHeroesIds = draftSets[0][1];
    let instructions =
      'Select 2 heroes, the 4 others will be given to other player';
    gameData.bluePlayer.instructions = instructions;
    gameData.redPlayer.instructions = instructions;
    gameData.game['waitingFor/blue'] = true;
    gameData.game['waitingFor/red'] = true;
    gameData.game.currentPlayer = 'both';
    //Save draftSets in case game is stopped and resumed  in the middle of draft phase
    gameData.game.draftSets = draftSets;
    gameData.game.updatePlayerState = 'both';
    duelCtrl.storeData(gameData);
  };

  this.update = (
    /** @type {import('type/comm').TH_MessageDraftStep} */ payload = null
  ) => {
    /** @type {import('type/game').TH_GameDataStore} */
    let gameData = duelCtrl.getGameData();
    if (payload) {
      let player = payload.color + 'Player';
      /** @type {number[]} */
      let heroesInPile = gameData[player].heroesInPile;
      /**@type {import('type/game').TH_HeroCard[]} */
      let twelveHeroes = gameData[player].twelveHeroes;
      /** @type {number[]} */
      let draftHeroesIds = gameData[player].draftHeroesIds;
      payload.chosenIds.forEach(id => {
        heroesInPile.push(id);
        twelveHeroes.push(gameData.game.allHeroes.find(hero => hero.id === id));
        draftHeroesIds = draftHeroesIds.filter(item => item !== id);
      });

      gameData[player].heroesInPile = heroesInPile;
      gameData[player].draftHeroesIds = draftHeroesIds;
      gameData.game['waitingFor/' + payload.color] = false;
    }

    // We finished waiting for both players to select 2 cards
    if (
      gameData.game['waitingFor/blue'] === false &&
      gameData.game['waitingFor/red'] === false
    ) {
      gameData.game.updatePlayerState = 'both';
      // Check if players completed their deck
      if (
        gameData.bluePlayer.heroesInPile.length == 12 &&
        gameData.redPlayer.heroesInPile.length == 12
      ) {
        //Shuffle the pile (not the tweleve heroes)
        shuffleHeroes(gameData.bluePlayer.heroesInPile);
        shuffleHeroes(gameData.redPlayer.heroesInPile);

        // No need to keep all heroes now for rest of the game
        gameData.game.allHeroes = [];
        // Clean Json file
        gameData.game.allHeroesJson = '';
        duelCtrl.storeData(gameData);

        // End of state, decks are complete
        duelCtrl.endCurrentState();
      } else {
        // Check if set of 6 cards is done
        if (gameData.redPlayer.draftHeroesIds.length === 0) {
          // Deal next 6 cards
          gameData.bluePlayer.draftHeroesIds = gameData.game.draftSets[1][0];
          gameData.redPlayer.draftHeroesIds = gameData.game.draftSets[1][1];
          let instructions =
            'Another time with those 6 cards, until you both have 12 heroes';
          gameData.bluePlayer.instructions = instructions;
          gameData.redPlayer.instructions = instructions;
        } else {
          // swap cards
          let temp = gameData.bluePlayer.draftHeroesIds;
          gameData.bluePlayer.draftHeroesIds =
            gameData.redPlayer.draftHeroesIds;
          gameData.redPlayer.draftHeroesIds = temp;
          let instructions = 'Keep going, select 2 more cards';
          gameData.bluePlayer.instructions = instructions;
          gameData.redPlayer.instructions = instructions;
        }
        // Wait again for players to chose 2 more cards
        gameData.game['waitingFor/blue'] = true;
        gameData.game['waitingFor/red'] = true;
        duelCtrl.storeData(gameData);
      }
    } else {
      // Still waiting for one player
      gameData.game.updatePlayerState = payload.color;
      duelCtrl.storeData(gameData);
    }
  };

  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();
  };
}
