'use strict';
function shuffle(a) {
  for (let i = a.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [a[i], a[j]] = [a[j], a[i]];
  }
  return a;
}

export const initHeroesFromJson = function(allHeroesJson) {
  let abilitiesMap = new Map();
  allHeroesJson.abilities.forEach(ability => {
    abilitiesMap.set(ability.abilityName, {
      name: ability.abilityName,
      hook: ability.abilityHook,
      isOptionnal: ability.optionnal,
      desc: ability['abilityDesc-FR']
    });
  });
  let heroes = [];
  let heroUniqueId = 0;
  allHeroesJson.heroes.forEach(hero => {
    let i = 0;
    while (i < hero.nbInDeck) {
      /** @type {import('type/game').TH_HeroCard} */
      let heroCard = {
        id: heroUniqueId,
        name: hero.name,
        cost: hero.cost,
        power: hero.power,
        faction: hero.faction,
        ability: abilitiesMap.get(hero.ability),
        isDraftable: hero.draftMode,
        popularity: hero.popularity,
        possibleActions: []
      };
      heroes.push(heroCard);
      heroUniqueId++;
      i++;
    }
  });
  return heroes;
};

/**
 * Get 12 heroes id by faction.
 *
 * @param {import("type/game").TH_HeroCard[]} allHeroes - Array of all heroes cards
 * @param {import("type/game").TH_Faction} faction - Faction to filter on
 * @param {boolean} popularityRule - Are we playing with popularity rule
 * @returns {import("type/game").TH_HeroCard[]} - Array of heroes
 */
export const getHeroesByFaction = function(allHeroes, faction, popularityRule) {
  /** @type {import("type/game").TH_Popularity} */
  let popularity = 'without';
  if (popularityRule === true) {
    popularity = 'with';
  }
  let heroes = allHeroes.filter(hero => {
    return (
      hero.faction === faction &&
      (hero.popularity === popularity || hero.popularity === 'any')
    );
  });
  return heroes;
};

/**
 * Get draft sets to play draft mode
 *
 * @param {import("type/game").TH_HeroCard[]} allHeroes - Array of all heroes cards
 * @param {import("type/game").TH_Popularity} popularityRule - Are we playing with popularity rule
 * @returns {Array<Array<number>>} Ids of heroes (4 arrays of 6 cards)
 */
export const getDraftSets = function(allHeroes, popularityRule) {
  let popularity = 'without';
  if (popularityRule === true) {
    popularity = 'with';
  }
  let heroesDraftable = allHeroes
    .filter(hero => {
      return (
        hero.isDraftable === true &&
        (hero.popularity === popularity || hero.popularity === 'any')
      );
    })
    .map(hero => hero.id);

  let shuffledHeroes = shuffle(heroesDraftable);

  // Return 4 sets of 6 heroes for draft mode
  let draftSets = [
    [shuffledHeroes.slice(0, 6), shuffledHeroes.slice(6, 12)],
    [shuffledHeroes.slice(12, 18), shuffledHeroes.slice(18, 24)]
  ];
  return draftSets;
};
/**
 * Shuffle Heroes
 *
 * @param {import("type/game").HeroCard[]|Array} heroes - Array of heroes
 * @returns {Array} Same Array in random order
 */
export const shuffleHeroes = function(heroes) {
  return shuffle(heroes);
};
