'use strict';
import allHeroesJson from "../../../assets/all-heroes.json";
import Hero from './hero';
import HeroAbility from './hero-ability';

export default class HeroFactory {
  constructor() {
    let abilitiesMap = new Map();
    allHeroesJson.abilities.forEach(ability => {
      let abilityObj = new HeroAbility(ability.abilityName,
        ability.abilityHook,
        ability.optionnal,
        ability['abilityDesc-FR']);
      abilitiesMap.set(ability.abilityName, abilityObj);
    });

    this.heroesSet = new Set();
    allHeroesJson.heroes.forEach(hero => {
      let i = 0;
      while (i < hero.nbInDeck) {
        let heroObj = new Hero(
          hero.name,
          hero.cost,
          hero.power,
          hero.faction,
          abilitiesMap.get(hero.ability),
          hero.draftMode
        );
        this.heroesSet.add(heroObj);
        i++;
      }
    });
  }

  getAllHeroesSet() {
    return this.heroesSet;
  }

  getFactionRandom(faction) {
    let factionHeroes = shuffleArray(Array.from(this.heroesSet).filter(hero => hero.faction === faction));
    return factionHeroes;
  }

}

function shuffleArray(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;
}
