'use strict';
import { shuffleHeroes, getDraftSets } from '../heroesHelper';
export default function selectDraftState(duelController) {
  let duelCtrl = duelController;
  let draftSets = [];
  // For draft there are two turns, we are at 0
  let draftTurn = 0;
  //Blue will get first 6 cards, and red following 6
  let blueDraftSetnb = 0;
  let redDraftSetnb = 1;

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

    gameData.bluePlayer.draftHeroesIds =
      draftSets[draftTurn - 1][blueDraftSetnb];
    gameData.redPlayer.draftHeroesIds = draftSets[draftTurn - 1][redDraftSetnb];
    gameData.game['waitingFor/blue'] = true;
    gameData.game['waitingFor/red'] = true;
    gameData.game.currentPlayer = 'both';
    duelCtrl.storeData(gameData);
  };

  let 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';
      gameData[player].twelveHeroes = payload.twelveHeroes;
      gameData[player].draftHeroesIds = payload.draftHeroesIds;
      gameData.game['waitingFor/' + payload.color] = false;
      duelCtrl.storeData(gameData);
    }

    // We finished waiting for both players to select 2 cards
    if (
      gameData.game['waitingFor/blue'] === false &&
      gameData.game['waitingFor/red'] === false
    ) {
      // Check if players completed their deck
      if (
        gameData.bluePlayer.twelveHeroes.length == 12 &&
        gameData.redPlayer.twelveHeroes.length == 12
      ) {
        shuffleHeroes(gameData.bluePlayer.twelveHeroes);
        shuffleHeroes(gameData.redPlayer.twelveHeroes);
        // 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
          draftTurn++;
          gameData.bluePlayer.draftHeroesIds =
            draftSets[draftTurn - 1][blueDraftSetnb];
          gameData.redPlayer.draftHeroesIds =
            draftSets[draftTurn - 1][redDraftSetnb];
        } else {
          // swap cards
          let temp = gameData.bluePlayer.draftHeroesIds;
          gameData.bluePlayer.draftHeroesIds =
            gameData.redPlayer.draftHeroesIds;
          gameData.redPlayer.draftHeroesIds = temp;
        }
        // Wait again for players to chose 2 more cards
        gameData.game['waitingFor/blue'] = true;
        gameData.game['waitingFor/red'] = true;
        duelCtrl.storeData(gameData);
      }
    }
  };

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

    duelCtrl.setCurrentState('2_CHANGE_UP_TO_3_CARDS');
    duelCtrl.startCurrentState();
  };
  return {
    start,
    update,
    end
  };
}