import { RdDUtility } from "./rdd-utility.js";
import { RdDCalendrier } from "./rdd-calendrier.js";

/* -------------------------------------------- */
export class RdDHerbes extends Item {

  /* -------------------------------------------- */
  static isHerbeSoin( botaniqueItem ) {
    return botaniqueItem.categorie == 'Soin';
  }
  /* -------------------------------------------- */
  static isHerbeRepos( botaniqueItem ) {
    return botaniqueItem.categorie == 'Repos';
  }

  /* -------------------------------------------- */
  static async initializeHerbes( ) {  
    this.herbesSoins = await RdDUtility.loadCompendium('foundryvtt-reve-de-dragon.botanique', item => this.isHerbeSoin(item));
    this.herbesRepos = await RdDUtility.loadCompendium('foundryvtt-reve-de-dragon.botanique', item => this.isHerbeRepos(item));
  }

  /* -------------------------------------------- */
  static buildHerbesList(listeHerbes, max) {
    let list = {}
    for ( let herbe of listeHerbes) {
      let brins = max - herbe.system.niveau;
      list[herbe.system.name] = `${herbe.system.name} (Bonus: ${herbe.system.niveau}, Brins: ${brins})`;
    }
    list['Autre'] = 'Autre (Bonus: variable, Brins: variable)'
    return list;
  }

  /* -------------------------------------------- */
  static updatePotionData( formData ) {
    formData.herbesSoins = this.buildHerbesList(this.herbesSoins, 12);
    formData.herbesRepos = this.buildHerbesList(this.herbesRepos, 7);
    formData.jourMoisOptions = RdDCalendrier.buildJoursMois();
    formData.dateActuelle = game.system.rdd.calendrier.getDateFromIndex();
    formData.splitDate = game.system.rdd.calendrier.getNumericDateFromIndex(formData.system.prdate);

    if (formData.system.categorie.includes('Soin') ) {
      formData.isHerbe = true;
      this.computeHerbeBonus(formData, this.herbesSoins, 12);
    } else if (formData.system.categorie.includes('Repos')) {
      formData.isRepos = true;
      this.computeHerbeBonus(formData, this.herbesRepos, 7);
    }
  }

  /* -------------------------------------------- */
  static calculePuissancePotion( potion ) {
    return potion.system.herbebonus * potion.system.pr;
  }

  /* -------------------------------------------- */
  static calculePointsRepos( potion ) {
    return potion.system.herbebonus * potion.system.pr;
  }

  /* -------------------------------------------- */
  static calculePointsGuerison( potion ){
    return potion.system.herbebonus * potion.system.pr;
  }

  /* -------------------------------------------- */
  static computeHerbeBonus( formData, herbesList, max) {
    if ( Number(formData.system.herbebrins) ) {
    let herbe = herbesList.find(item => item.name.toLowerCase() == formData.system.herbe.toLowerCase() );
      if( herbe ) {
        let brinsBase = max - herbe.system.niveau;
        formData.system.herbebonus = Math.max(herbe.system.niveau - Math.max(brinsBase - formData.system.herbebrins, 0), 0);
      }
    }
  }  

}  