import { Grammar } from "../grammar.js";
import { RdDItem } from "../item.js";
import { SystemCompendiums } from "../settings/system-compendiums.js";
import { ACTION_ITEM_ENCHANTER } from "../enchantement/dialog-enchanter.js";

// --- Actions sur les "potions"
const _CONSOMMER_POTION = {
  code: 'item-potion-consommer', label: 'Consommer', icon: it => 'fa-solid fa-vial',
  optionsFilter: options => options.editable,
  action: (item, actor) => actor.consommerPotion(item)
}

/**
 * Item pour gérer les potions
 */
export class RdDItemPotion extends RdDItem {

  static async herbesSoins() {
    return await RdDItemPotion.$listHerbes(it => Grammar.equalsInsensitive(it.system.categorie, 'Soin') && it.system.niveau > 0)
  }
  static async herbesRepos() {
    return await RdDItemPotion.$listHerbes(it => Grammar.equalsInsensitive(it.system.categorie, 'Repos') && it.system.niveau > 0)
  }

  static async $listHerbes(filter) {
    const herbes = await SystemCompendiums.getWorldOrCompendiumItems('herbe', 'faune-flore-mineraux');
    return herbes.filter(filter)
  }

  static get defaultIcon() {
    return "systems/foundryvtt-reve-de-dragon/icons/objets/liqueur_de_bagdol.webp"
  }

  get isEnchantementPossible() {
    return this.system.etat == 'Liquide'
  }

  itemSpecificActions() {
    return [_CONSOMMER_POTION, ACTION_ITEM_ENCHANTER]
  }

  prepareDerivedData() {
    super.prepareDerivedData()
    this.system.puissance = this.system.magique ? this.calculPuissance() : 0
  }

  getUtilisation() {
    switch (this.system.categorie) {
      case 'Alchimie': case 'Autre': case 'AlchimieAutre':
        // TODO: distinguer les remèdes alchimiques enchantables/non
        return 'alchimie'
      case 'Cuisine':
        return 'cuisine'
      case 'Remede': case 'Repos': case 'Soin':
        return 'soins'
    }
    return ''
  }

  getProprietes() {
    const proprietes = [
      `<b>Rareté</b>: ${this.system.rarete} `,
      `<b>Catégorie</b>: ${this.system.categorie}`,
      `<b>Etat</b>: ${this.system.etat}`
    ]
    const proprietesMagiques = this.system.magique ? [
      `<b>Enchantée</b> <i class="fa-solid fa-sparkles"></i> ${this.system.purifie ? ', purifiée' : ''} ${this.system.prpermanent ? 'permanente' : ''} `,
      `<b>Points de rêve</b>: ${this.system.pr}`,
    ] : []
    return proprietes
      .concat(proprietesMagiques)
      .concat(this._inventaireTemplateChatData())
      .filter(it => it != undefined)
  }

  perteReveChateauDormant() {
    if (this.system.magique && !this.system.prpermanent && this.system.pr > 0) {
      const nouveaupr = Math.max(this.system.pr - 1, 0)
      return {
        alias: this.parent.getAlias(),
        item: this,
        nouveaupr: nouveaupr,
        update: {
          _id: this.id,
          'system.pr': nouveaupr,
          'system.magique': nouveaupr > 0
        }
      }
    }
    return undefined
  }

  calculPuissance() { return this.system.herbebonus * this.system.pr }

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

}