/* Common useful functions shared between objects */
import { ChatUtility } from "./chat-utility.js";
import { PasseArme } from "./combat/passe-arme.mjs";
import { Misc } from "./misc.js";
import { Grammar } from "./grammar.js";
import { TMRUtility } from "./tmr-utility.js";
import { DialogItemAchat } from "./achat-vente/dialog-item-achat.js";
import { ReglesOptionnelles } from "./settings/regles-optionnelles.js";
import { RdDDice } from "./rdd-dice.js";
import { RdDItem } from "./item.js";
import { RdDPossession } from "./combat/rdd-possession.js";
import { RdDNameGen } from "./rdd-namegen.js";
import { RdDConfirm } from "./rdd-confirm.js";
import { RdDResolutionTable } from "./rdd-resolution-table.js";
import { RdDTimestamp } from "./time/rdd-timestamp.js";
import { RdDRaretes } from "./item/raretes.js";
import { RdDEmpoignade } from "./combat/rdd-empoignade.js";
import { ExperienceLog } from "./actor/experience-log.js";
import { RdDCoeur } from "./coeur/rdd-coeur.js";
import { APP_ASTROLOGIE_REFRESH } from "./sommeil/app-astrologie.js";
import { ACTOR_TYPES, ITEM_TYPES, RDD_CONFIG, SYSTEM_RDD } from "./constants.js";
import { RdDBaseActor } from "./actor/base-actor.js";
import { RdDCarac } from "./rdd-carac.js";
import { RdDTextEditor } from "./apps/rdd-text-roll-editor.js";

import { RdDItemCompetence } from "./item-competence.js";
import { Monnaie } from "./item-monnaie.js";
import { ItemAction } from "./item/item-actions.js";
import { Targets } from "./combat/targets.js";
import { DialogSelect } from "./dialog-select.js";

/* -------------------------------------------- */
// This table starts at 0 -> niveau -10
const carac_array = ["taille", "apparence", "constitution", "force", "agilite", "dexterite", "vue", "ouie", "odoratgout", "volonte", "intellect", "empathie", "reve", "chance", "melee", "tir", "lancer", "derobee"];
const difficultesLibres = Misc.intArray(0, -11);
const ajustementsConditions = Misc.intArray(-10, 11);
const ajustementsEncaissement = Misc.intArray(-10, 26);

/* -------------------------------------------- */
function _buildAllSegmentsFatigue(max) {
  const cycle = [5, 2, 4, 1, 3, 0];
  const fatigue = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]];
  for (let i = 0; i <= max; i++) {
    const ligneFatigue = foundry.utils.deepClone(fatigue[i]);
    const caseIncrementee = cycle[i % 6];
    ligneFatigue[caseIncrementee]++;
    ligneFatigue[caseIncrementee + 6]++;
    ligneFatigue.fatigueMax = 2 * (i + 1);
    fatigue[i + 1] = ligneFatigue;
  }
  return fatigue;
}

/* -------------------------------------------- */
function _cumulSegmentsFatigue(matrix) {
  let cumulMatrix = [];
  for (let line of matrix) {
    let cumul = foundry.utils.deepClone(line);

    for (let i = 1; i < 12; i++) {
      cumul[i] += cumul[i - 1];
    }
    cumulMatrix.push(cumul);
  }
  return cumulMatrix;
}

/* -------------------------------------------- */
export const MAX_ENDURANCE_FATIGUE = 60;
const fatigueMatrix = _buildAllSegmentsFatigue(MAX_ENDURANCE_FATIGUE);
const cumulFatigueMatrix = _cumulSegmentsFatigue(fatigueMatrix);

const fatigueMalus = [0, 0, 0, -1, -1, -1, -2, -3, -4, -5, -6, -7]; // Provides the malus for each segment of fatigue
const fatigueLineSize = [3, 6, 7, 8, 9, 10, 11, 12];
const fatigueLineMalus = [0, -1, -2, -3, -4, -5, -6, -7];

/* -------------------------------------------- */
const nomEthylisme = ["Emeché", "Gris", "Pinté", "Pas frais", "Ivre", "Bu", "Complètement fait", "Ivre mort"];

const TABLE_ENCAISSEMENT_MORTEL = [
  { minimum: undefined, maximum: 0, endurance: "0", vie: "0", gravite: -1 },
  { minimum: 1, maximum: 10, endurance: "1d4", vie: "0", gravite: 0 },
  { minimum: 11, maximum: 15, endurance: "1d6", vie: "0", gravite: 2 },
  { minimum: 16, maximum: 19, endurance: "2d6", vie: "2", gravite: 4 },
  { minimum: 20, maximum: undefined, endurance: "100", vie: "4 + @over20", gravite: 6 },
]
const TABLE_ENCAISSEMENT_NONMORTEL = [
  { minimum: undefined, maximum: 0, endurance: "0", vie: "0", gravite: -1 },
  { minimum: 1, maximum: 10, endurance: "1d4", vie: "0", gravite: 0 },
  { minimum: 11, maximum: 15, endurance: "1d6", vie: "0", gravite: 0 },
  { minimum: 16, maximum: 19, endurance: "2d6", vie: "0", gravite: 2 },
  { minimum: 20, maximum: undefined, endurance: "100", vie: "0", gravite: 2 },
]
const TABLE_ENCAISSEMENT_MORTEL_ALTERNATIF = [
  { minimum: undefined, maximum: 0, endurance: "0", vie: "0", gravite: -1 },
  { minimum: 1, maximum: 4, endurance: "-1", vie: "0", gravite: 0 },
  { minimum: 5, maximum: 6, endurance: "-2", vie: "0", gravite: 0 },
  { minimum: 7, maximum: 8, endurance: "-3", vie: "0", gravite: 0 },
  { minimum: 9, maximum: 10, endurance: "-4", vie: "0", gravite: 0 },
  { minimum: 11, maximum: 12, endurance: "-4", vie: "0", gravite: 0 },
  { minimum: 13, maximum: 14, endurance: "-5", vie: "0", gravite: 0 },
  { minimum: 15, maximum: 15, endurance: "-6", vie: "0", gravite: 0 },
  { minimum: 16, maximum: 16, endurance: "-6", vie: "-2", gravite: 0 },
  { minimum: 17, maximum: 17, endurance: "-8", vie: "-2", gravite: 0 },
  { minimum: 18, maximum: 18, endurance: "-10", vie: "-2", gravite: 0 },
  { minimum: 19, maximum: 19, endurance: "-12", vie: "-2", gravite: 0 },
  { minimum: 20, maximum: undefined, endurance: "@end0", vie: "-4 + @over20", gravite: 0 },
]
const TABLE_ENCAISSEMENT_NONMORTEL_ALTERNATIF = [
  { minimum: undefined, maximum: 0, endurance: "0", vie: "0", gravite: -1 },
  { minimum: 1, maximum: 4, endurance: "-1", vie: "0", gravite: 0 },
  { minimum: 5, maximum: 6, endurance: "-2", vie: "0", gravite: 0 },
  { minimum: 7, maximum: 8, endurance: "-3", vie: "0", gravite: 0 },
  { minimum: 9, maximum: 12, endurance: "-4", vie: "0", gravite: 0 },
  { minimum: 13, maximum: 14, endurance: "-5", vie: "0", gravite: 2 },
  { minimum: 15, maximum: 15, endurance: "-6", vie: "0", gravite: 2 },
  { minimum: 16, maximum: 16, endurance: "-6", vie: "0", gravite: 2 },
  { minimum: 17, maximum: 17, endurance: "-8", vie: "0", gravite: 2 },
  { minimum: 18, maximum: 18, endurance: "-10", vie: "0", gravite: 2 },
  { minimum: 19, maximum: 19, endurance: "-12", vie: "0", gravite: 2 },
  { minimum: 20, maximum: undefined, endurance: "@end0", vie: "-1", gravite: 2 },
]
const TABLE_ENCAISSEMENT_ENTITE = [
  { minimum: undefined, maximum: 0, endurance: "0", vie: "0", gravite: -1 },
  { minimum: 1, maximum: 10, endurance: "1d4", vie: "0", gravite: 0 },
  { minimum: 11, maximum: 15, endurance: "1d6", vie: "0", gravite: 2 },
  { minimum: 16, maximum: 19, endurance: "2d6", vie: "0", gravite: 4 },
  { minimum: 20, maximum: undefined, endurance: "3d6 + @over20", vie: "0", gravite: 6 },
]

/* -------------------------------------------- */
export class RdDUtility {
  // persistent handling of conteneur show/hide
  static afficheContenu = {}
  /* -------------------------------------------- */
  static async initHooks() {
    Hooks.on('renderChatLog', (log, html, chatLog) => RdDUtility.chatListeners(html))
  }

  /* -------------------------------------------- */
  static preloadHandlebarsTemplates() {
    const templatePaths = [
      //Character Sheets
      'systems/foundryvtt-reve-de-dragon/templates/actor-sheet.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor-creature-sheet.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor-entite-sheet.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor-vehicule-sheet.hbs',
      // sous-parties de feuilles de personnages
      'systems/foundryvtt-reve-de-dragon/templates/actor/item-action-controls.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/header-buttons.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/header-etat.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/header-compteurs.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/header-compteurs-creature.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/header-compteurs-entitee.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/header-effects.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/header-hautreve.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/archetype.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/vue-detaillee.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/armures.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/carac-main.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/carac-derivee.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/carac-creature.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/carac-entitee.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/carac-total.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/comp-creature.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/comp-possession.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/competence.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/competence-categorie.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/xp-competences.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/combat.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/blessures.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/blessure.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/maladies-poisons.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/possessions.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/resonances.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/taches.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/livres.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/oeuvres.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/oeuvre.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/jeux.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/alchimie.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/astrologie.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/chirurgie.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/non-haut-revant.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/haut-revant.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/dragon-queues.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/dragon-queue.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/dragon-souffles.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/dragon-tetes.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/hr-signes-draconiques.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/hr-rencontres.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/hr-sorts.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/hr-sorts-reserve.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/hr-meditations.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/hr-casetmrs.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/xp-journal.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/editor-notes-mj.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/inventaire.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/inventaire-item.hbs',
      "systems/foundryvtt-reve-de-dragon/templates/actor/inventaire-monnaie.hbs",
      'systems/foundryvtt-reve-de-dragon/templates/actor/liens-animaux.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/liens-suivants.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/liens-vehicules.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/commerce-inventaire.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/commerce-inventaire-item.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/tooltip-bonmoments.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/dialog-gerer-herbes.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/dialog-gerer-ingredients.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/dialog-gerer-deniers.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/actor/dialog-gerer-potions.hbs',
      //Items
      'systems/foundryvtt-reve-de-dragon/templates/scripts/autocomplete-script.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/scripts/autocomplete.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/item/icon-arme-broken.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/item/temporel.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/item/dialog-verifier-ingredients.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/item/dialog-lancer-recette.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/item/enum-appreciation-bonmoment.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/item/enum-appreciation-carac.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/item/enum-appreciation-moral.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/item/tache-commune-flag.hbs',
      // partial enums
      'systems/foundryvtt-reve-de-dragon/templates/enum-aspect-tarot.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/enum-base-competence.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/enum-caracteristiques.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/enum-categories.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/enum-categorie-ingredient.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/enum-categorie-parade.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/item/enum-categorie-alchimie.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/item/enum-etat-alchimie.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/enum-categorie-queue.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/enum-categorie-vehicule.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/enum-competence.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/enum-draconic.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/enum-heures.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/enum-initpremierround.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/enum-mortalite.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/enum-niveau-ethylisme.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/enum-periode.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/enum-rarete.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/enum-tmr-effet.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/enum-tmr-type.hbs',
      // Partials
      'systems/foundryvtt-reve-de-dragon/templates/enchantement/partial-enchantement.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/coeur/chat-effet-tendre-moment.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/coeur/afficher-coeur.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/tirage/liste-resultats-recherche.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/time/horloge.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/voyage/fatigue-actor.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/voyage/option-vitesse-fatigue.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/common/timestamp.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/common/date-heure.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/common/periodicite.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/common/enum-duree.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/common/compendium-link.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/partial-description-overflow.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/partial-description-sort.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/partial-description.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/partial-roll-ajustements.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/partial-roll-astrologique.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/partial-roll-coeur.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/partial-roll-competences.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/partial-roll-diffLibre.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/partial-roll-diffFixe.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/partial-roll-diffCondition.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/partial-roll-enctotal.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/partial-roll-forcer.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/partial-roll-moral.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/partial-roll-surenc.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/partial-select-carac.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/roll/explain.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/resolution-table.hbs',
      // Dialogs
      'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-resolution.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-competence.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-carac.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-sort.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-encaisser.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-validation-encaissement.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-meditation.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-tmr.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-alchimie.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/sommeil/sommeil-actor-moral.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/sommeil/astrologie-gardien.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/sommeil/astrologie-joueur.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/sommeil/astrologie-theme.hbs',
      // HUD
      'systems/foundryvtt-reve-de-dragon/templates/hud-actor-init.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/hud-actor-attaque.hbs',
      // messages tchat
      'systems/foundryvtt-reve-de-dragon/templates/chat-infojet.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/chat-description.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/chat-info-appel-au-moral.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/chat-info-distance.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/chat-actor-turn-acteur.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/chat-actor-turn-sante.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/chat-actor-competence-xp.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/chat-actor-carac-xp.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/chat-fabriquer-potion-base.hbs',
      'systems/foundryvtt-reve-de-dragon/templates/chat-signe-draconique-actor.hbs'
    ];

    // foundry et options
    Handlebars.registerHelper('RDD_CONFIG', path => RDD_CONFIG[path])
    Handlebars.registerHelper('settings-get', (setting) => game.settings.get(SYSTEM_RDD, setting))
    Handlebars.registerHelper('linkCompendium', (pack, id, name) => RdDUtility.linkCompendium(pack, id, name));
    Handlebars.registerHelper('regle-optionnelle', (option) => ReglesOptionnelles.isUsing(option));

    Handlebars.registerHelper('plusMoins', diff => parseInt(diff) ? (diff > 0 ? '+' : '') + Math.round(diff) : diff)
    Handlebars.registerHelper('fractionOneN', n => new Handlebars.SafeString(Misc.getFractionOneN(n)))

    // Handle v12 removal of this helper
    Handlebars.registerHelper('select', function (selected, options) {
      const escapedValue = RegExp.escape(Handlebars.escapeExpression(selected));
      const rgx = new RegExp(' value=[\"\']' + escapedValue + '[\"\']');
      const html = options.fn(this);
      return html.replace(rgx, "$& selected");
    })

    // logic
    Handlebars.registerHelper('either', (a, b) => a ?? b);
    // string manipulation
    Handlebars.registerHelper('upperFirst', str => Misc.upperFirst(str ?? 'Null'));
    Handlebars.registerHelper('lowerFirst', str => Misc.lowerFirst(str ?? 'Null'));
    Handlebars.registerHelper('uppercase', str => str?.toUpperCase() ?? '');
    Handlebars.registerHelper('lowercase', str => str?.toLowerCase() ?? '');
    Handlebars.registerHelper('grammar-le', str => Grammar.articleDetermine(str));
    Handlebars.registerHelper('grammar-apostrophe', (article, str) => Grammar.apostrophe(article, str));
    Handlebars.registerHelper('grammar-un', str => Grammar.articleIndetermine(str));
    Handlebars.registerHelper('grammar-accord', (genre, ...args) => Grammar.accord(genre, args));
    Handlebars.registerHelper('json-stringify', object => JSON.stringify(object))
    Handlebars.registerHelper('escapeHtml', object => foundry.utils.escapeHTML(object))

    // math
    Handlebars.registerHelper('math-sum', (...values) => values.slice(0, -1).reduce(Misc.sum(), 0))
    Handlebars.registerHelper('math-abs', diff => Math.abs(parseInt(diff)))
    Handlebars.registerHelper('min', (...args) => Math.min(...args.slice(0, -1)));
    Handlebars.registerHelper('repeat', function (n, block) {
      let accum = '';
      for (let i = 0; i < n; ++i) {
        accum += block.fn(i)
      }
      return accum
    })

    // tableaux, listes
    Handlebars.registerHelper('array-includes', (array, value) => array.includes(value));
    Handlebars.registerHelper('isLastIndex', (index, list) => index + 1 >= list.length);
    Handlebars.registerHelper('trier', list => list.sort((a, b) => a.name.localeCompare(b.name)));

    // table de résolution
    Handlebars.registerHelper('computeResolutionScore', (row, col) => RdDResolutionTable.computePercentage(row, col));
    Handlebars.registerHelper('computeResolutionChances', (row, col) => RdDResolutionTable.computeChances(row, col));

    // gestion des dates et heures
    Handlebars.registerHelper('timestamp-imgSigneHeure', (heure) => { return new Handlebars.SafeString(RdDTimestamp.imgSigneHeure(heure)) });
    Handlebars.registerHelper('timestamp-imgSigne', (heure) => { return new Handlebars.SafeString(RdDTimestamp.imgSigne(heure)) });
    Handlebars.registerHelper('timestamp-label', (heure) => RdDTimestamp.definition(heure)?.label ?? 'inconnue')
    Handlebars.registerHelper('timestamp-definition', (heure) => RdDTimestamp.definition(heure))
    Handlebars.registerHelper('timestamp-extract', timestamp => new RdDTimestamp(timestamp).toCalendrier());
    Handlebars.registerHelper('timestamp-formulesDuree', () => RdDTimestamp.formulesDuree());
    Handlebars.registerHelper('timestamp-formulesPeriode', () => RdDTimestamp.formulesPeriode());

    // informations sur les acteurs
    Handlebars.registerHelper('actor-default', (actorType, ...path) => RdDBaseActor.getDefaultValue(actorType, path.slice(0, -1)));
    Handlebars.registerHelper('filtreTriCompetences', competences => RdDItemCompetence.triVisible(competences));
    Handlebars.registerHelper('experienceLog-topic', topic => ExperienceLog.labelTopic(topic));

    Handlebars.registerHelper('carac-label', (code) => RdDCarac.label(code))

    // inventaire et marchands
    Handlebars.registerHelper('buildLigneInventaire', (item, options) => { return new Handlebars.SafeString(RdDUtility.buildLigneInventaire(item, options)); });
    Handlebars.registerHelper('buildInventaireConteneur', (actorId, itemId, options) => { return new Handlebars.SafeString(RdDUtility.buildInventaireConteneur(actorId, itemId, options)); });
    Handlebars.registerHelper('buildContenuConteneur', (item, options) => { return new Handlebars.SafeString(RdDUtility.buildContenuConteneur(item, options)); });
    Handlebars.registerHelper('calculerPrixCommercant', item => item.calculerPrixCommercant());
    Handlebars.registerHelper('uniteQuantite', (itemId, actorId) => RdDUtility.getItem(itemId, actorId)?.getUniteQuantite());
    Handlebars.registerHelper('isFieldInventaireModifiable', (type, field) => RdDItem.isFieldInventaireModifiable(type, field));
    // Items
    Handlebars.registerHelper('rarete-getChamp', (rarete, field) => RdDRaretes.getChamp(rarete, field));
    Handlebars.registerHelper('item-action-applies', (action, item, options) => ItemAction.applies(action, item, options))
    Handlebars.registerHelper('item-action-label', (action, item) => new Handlebars.SafeString(ItemAction.label(action, item)))
    Handlebars.registerHelper('item-action-img', (action, item) => new Handlebars.SafeString(ItemAction.img(action, item)))
    Handlebars.registerHelper('item-name', (item) => item.nameDisplay)

    // TMRs
    Handlebars.registerHelper('caseTmr-label', coord => TMRUtility.getTMRLabel(coord));
    Handlebars.registerHelper('caseTmr-type', coord => TMRUtility.getTMRType(coord));
    Handlebars.registerHelper('typeTmr-name', type => TMRUtility.typeTmrName(type));
    Handlebars.registerHelper('effetRencontre-name', coord => TMRUtility.typeTmrName(coord));

    foundry.applications.handlebars.loadTemplates(templatePaths);
  }

  static getItem(itemId, actorId = undefined) {
    return actorId ? game.actors.get(actorId)?.getItem(itemId) : game.items.get(itemId);
  }

  static linkCompendium(pack, id, name) {
    return `@Compendium[${pack}.${id}]{${name}}`;
  }

  /* -------------------------------------------- */
  static buildListOptions(min, max) {
    let options = ""
    for (let i = min; i <= max; i++) {
      options += `<option value="${i}">${i}</option>`
    }
    return options;
  }

  /* -------------------------------------------- */
  static arrayOrEmpty(items) {
    if (items?.length) {
      return items;
    }
    return [];
  }

  /* -------------------------------------------- */
  static getNomEthylisme(niveauEthylisme) { return niveauEthylisme > 0 ? 'Aucun' : nomEthylisme[-niveauEthylisme] }

  /* -------------------------------------------- */
  static toggleAfficheContenu(conteneurId) {
    RdDUtility.afficheContenu[conteneurId] = !RdDUtility.afficheContenu[conteneurId];
  }
  /* -------------------------------------------- */
  static getAfficheContenu(conteneurId) {
    if (conteneurId)
      return RdDUtility.afficheContenu[conteneurId];
    return undefined;
  }

  /* -------------------------------------------- */
  static buildArbreDeConteneurs(conteneurs, inventaires) {
    let objetVersConteneur = {};
    // Attribution des objets aux conteneurs
    for (let conteneur of conteneurs) {
      if (conteneur.isConteneur()) {
        conteneur.subItems = [];
        for (let id of conteneur.system.contenu ?? []) {
          let objet = inventaires.find(objet => (id == objet.id));
          if (objet) {
            objet.estContenu = true;
            objet.parentConteneurName = conteneur.name;
            objetVersConteneur[id] = conteneur.id;
            conteneur.subItems.push(objet);
          }
        }
      }
    }
    for (let conteneur of conteneurs) {
      conteneur.system.encTotal = RdDUtility.calculEncContenu(conteneur, inventaires);
    }
    return objetVersConteneur;
  }

  /* -------------------------------------------- */
  static calculEncContenu(conteneur, inventaires) {
    const contenus = (conteneur.system.contenu ?? []).filter(id => id != undefined)
      .map(id => inventaires.find(it => (id == it.id)))
      .filter(it => it);
    let enc = Number(conteneur.system.encombrement ?? 0) * Number(conteneur.system.quantite ?? 1);
    for (let contenu of contenus) {
      if (contenu.type == 'conteneur') {
        enc += RdDUtility.calculEncContenu(contenu, inventaires);
      }
      else {
        enc += Number(contenu.system.encombrement ?? 0) * Number(contenu.system.quantite ?? 1)
      }
    }
    return enc
  }

  /* -------------------------------------------- */
  // Construit la liste des conteneurs de niveau 1 (c'est à dire non contenu eux-même dans un conteneur)
  static conteneursRacine(conteneurs) {
    return conteneurs.filter((conteneur, index, arr) => !conteneur.estContenu);
  }

  static prepareOptionsArbreInventaire(item, optionsArbre) {
    if (!optionsArbre.profondeur) {
      optionsArbre.profondeur = 1
    };
    if (!optionsArbre.templateItem) {
      optionsArbre.templateItem = item.parent?.type == 'commerce'
        ? "systems/foundryvtt-reve-de-dragon/templates/actor/commerce-inventaire-item.hbs"
        : "systems/foundryvtt-reve-de-dragon/templates/actor/inventaire-item.hbs";
    }
    item.niveau = optionsArbre.profondeur;
  }

  /* -------------------------------------------- */

  /**
   * Construit la structure récursive des conteneurs, avec imbrication potentielle
   */
  static buildLigneInventaire(item, options = {}, optionsArbre = { ouvert: false, profondeur: 1 }) {
    RdDUtility.prepareOptionsArbreInventaire(item, optionsArbre);

    const isConteneur = item.type == 'conteneur';
    const inventaire = {
      item: item,
      vide: isConteneur && item.system.contenu.length == 0,
      ouvert: isConteneur && RdDUtility.getAfficheContenu(item.id),
      sature: isConteneur && item.system.encTotal > item.system.capacite,
      fillPercent: isConteneur ? Math.min(100, (item.system.encTotal / (item.system.capacite || 1)) * 100) : 0,
      options: options
    };
    optionsArbre.ouvert = inventaire.ouvert
    const ligneObjet = Handlebars.partials[optionsArbre.templateItem](inventaire);
    if (isConteneur) {
      return ligneObjet + RdDUtility.buildContenuConteneur(item, options, optionsArbre);
    }
    return ligneObjet;
  }

  static filterItemsPerTypeForSheet(formData, itemTypes, triEquipement = 'name') {
    Object.values(ITEM_TYPES).forEach(t => {
      formData[RdDItem.itemTypePluriel(t)] = Misc.arrayOrEmpty(itemTypes[t])
      itemTypes[t].forEach(item => item.actions = item.itemActions())
    })

    formData.maladiesPoisons = formData.maladies.concat(formData.poisons)
    formData.competences = formData.competences.concat(formData.competencecreatures)
    formData.monnaies = formData.monnaies.sort(Monnaie.triValeurEntiere())

    formData.inventaires = RdDUtility.prepareInventaire(itemTypes, triEquipement)
  }

  static buildInventaireConteneur(actorId, itemId, options) {
    const actor = game.actors.get(actorId)
    const item = actor?.items.get(itemId)
    if (item?.type == ITEM_TYPES.conteneur) {
      const formData = {}
      RdDUtility.filterItemsPerTypeForSheet(formData, actor.itemTypes, options?.triEquipement);
      RdDUtility.buildArbreDeConteneurs(formData.conteneurs, formData.inventaires);
      item.subItems = formData.conteneurs.find(it => it.id == itemId)?.subItems;

      return RdDUtility.buildContenuConteneur(item, options, { ouvert: true, profondeur: 1 })
    }
    return ''
  }

  static prepareInventaire(itemTypes, triEquipement = 'name') {
    let items = RdDItem.getItemTypesInventaire('all')
      .map(t => Misc.arrayOrEmpty(itemTypes[t]))
      .reduce((a, b) => a.concat(b), []);
    items = items.filter(it => !(it.type === ITEM_TYPES.livre && it.system.estCopieLue));
    switch (triEquipement) {
      case 'type':
        items.sort(Misc.ascending(it => Misc.typeName('Item', it.type)));
        break;
      case 'enc':
        items.sort(Misc.ascending(it => it.system.encombrement ?? 0));
        break;
      case 'quantite':
        items.sort(Misc.descending(it => it.system.quantite ?? 0));
        break;
      default:
        items.sort(Misc.ascending(it => it.name));
    }
    return items;
  }

  /* -------------------------------------------- */
  static buildContenuConteneur(conteneur, options = {}, optionsArbre = {}) {
    RdDUtility.prepareOptionsArbreInventaire(conteneur, optionsArbre);
    const display = optionsArbre.ouvert ? 'item-display-show' : 'item-display-hide';
    const profondeur = optionsArbre.profondeur;

    optionsArbre.profondeur++;
    const sortBy = options.triEquipement || 'name';
    let sorted = [...conteneur.subItems];
    sorted = sorted.filter(it => !(it.type === ITEM_TYPES.livre && it.system.estCopieLue));
    switch (sortBy) {
      case 'type':
        sorted.sort(Misc.ascending(it => Misc.typeName('Item', it.type)));
        break;
      case 'enc':
        sorted.sort(Misc.ascending(it => it.system.encombrement ?? 0));
        break;
      case 'quantite':
        sorted.sort(Misc.descending(it => it.system.quantite ?? 0));
        break;
      default:
        sorted.sort(Misc.ascending(it => it.name));
    }
    const lignesContenu = sorted
      .map(contenu => this.buildLigneInventaire(contenu, options, optionsArbre));

    return `<ul class='item-list alterne-list ${display} list-item-margin${Math.min(profondeur, 6)}'>`
      + lignesContenu.reduce(Misc.joining(''), '')
      + "</ul>";

  }

  /* -------------------------------------------- */
  static getCaracArray() {
    return carac_array;
  }
  static getDifficultesLibres() {
    return difficultesLibres;
  }
  static getAjustementsConditions() {
    return ajustementsConditions;
  }
  static getAjustementsEncaissement() {
    return ajustementsEncaissement;
  }

  /* -------------------------------------------- */
  static getSegmentsFatigue(maxEndurance) {
    return fatigueMatrix[Math.min(Math.max(maxEndurance, 1), fatigueMatrix.length)];
  }

  /* -------------------------------------------- */
  static calculMalusFatigue(fatigue, endurance) {
    endurance = Math.min(Math.max(endurance, 1), cumulFatigueMatrix.length);
    let segments = cumulFatigueMatrix[endurance];
    for (let i = 0; i < segments.length; i++) {
      if (fatigue <= segments[i]) {
        return fatigueMalus[i]
      }
    }
    return -7;
  }

  /* -------------------------------------------- */
  static calculFatigueHtml(fatigue, endurance) {
    return ReglesOptionnelles.isUsing("appliquer-fatigue") ? {
      malus: RdDUtility.calculMalusFatigue(fatigue, endurance),
      html: "<table class='table-fatigue'>" + RdDUtility.makeHTMLfatigueMatrix(fatigue, endurance).html() + "</table>"
    } : { malus: 0, html: '' };
  }

  /* -------------------------------------------- */
  // Build the nice (?) html table used to manage fatigue.
  // max should be the endurance max value
  static makeHTMLfatigueMatrix(fatigue, maxEndurance) {
    const segments = this.getSegmentsFatigue(maxEndurance);
    return this.makeHTMLfatigueMatrixForSegment(fatigue, segments);
  }

  /* -------------------------------------------- */
  static makeHTMLfatigueMatrixForSegment(fatigue, segments) {
    fatigue = Math.max(fatigue, 0);
    fatigue = Math.min(fatigue, segments.fatigueMax);

    let table = $("<table/>").addClass('table-fatigue');
    let segmentIdx = 0;
    let fatigueCount = 0;
    for (var line = 0; line < fatigueLineSize.length; line++) {
      let row = $("<tr/>");
      let segmentsPerLine = fatigueLineSize[line];
      row.append("<td class='fatigue-malus'>" + fatigueLineMalus[line] + "</td>");
      while (segmentIdx < segmentsPerLine) {
        let freeSize = segments[segmentIdx];
        for (let col = 0; col < 5; col++) {
          if (col < freeSize) {
            if (fatigueCount < fatigue)
              row.append("<td class='fatigue-used'>X</td>");


            else
              row.append("<td class='fatigue-free'/>");
            fatigueCount++;
          } else {
            row.append("<td class='fatigue-none'/>");
          }
        }
        row.append("<td class='fatigue-separator'/>");
        segmentIdx = segmentIdx + 1;
      }
      table.append(row);
    }
    return table;
  }

  /* -------------------------------------------- */
  static async getLocalisation(type = 'personnage') {
    const loc = { result: await RdDDice.rollTotal("1d20") };
    if (type == 'personnage') {
      if (loc.result <= 3) loc.txt = "Jambe, genou, pied, jarret";
      else if (loc.result <= 7) loc.txt = "Hanche, cuisse, fesse";
      else if (loc.result <= 9) loc.txt = "Ventre, reins";
      else if (loc.result <= 12) loc.txt = "Poitrine, dos";
      else if (loc.result <= 14) loc.txt = "Avant-bras, main, coude";
      else if (loc.result <= 18) loc.txt = "Epaule, bras, omoplate";
      else if (loc.result == 19) loc.txt = "Tête";
      else if (loc.result == 20) loc.txt = "Tête (visage)";
    } else {
      if (loc.result <= 7) loc.txt = "Jambes/Pattes";
      else if (loc.result <= 18) loc.txt = "Corps";
      else if (loc.result <= 20) loc.txt = "Tête";
    }
    return loc
  }

  /* -------------------------------------------- */
  static async jetEncaissement(actor, dmg, armure, options = { showDice: HIDE_DICE }) {
    const diff = Math.abs(dmg.diff)
    const formula = RdDUtility.formuleEncaissement(diff, options)
    const roll = await RdDDice.roll(formula, options);

    RdDUtility.remplaceDeMinParDifficulte(roll, diff, options);

    return await RdDUtility.prepareEncaissement(actor, dmg, roll, armure);
  }

  static remplaceDeMinParDifficulte(roll, diff, options) {
    if (!ReglesOptionnelles.isUsing('degat-minimum-malus-libre-simple')) {
      return
    }
    // 1 dé fait au minmum la difficulté libre
    const total = options.forceDiceResult?.total;
    if (total) {
      const reste = Math.max(total - diff, 1)
      roll.terms[0].number = reste + diff
    }
    else {
      if (roll.terms[0].results[0].result < diff) {
        roll.terms[0].results[0].result = diff;
      } else if (roll.terms[0].results[1].result < diff) {
        roll.terms[0].results[1].result = diff;
      }
      roll._total = roll.terms[0].results[0].result + roll.terms[0].results[1].result;
    }
  }

  static formuleEncaissement(diff) {
    // Chaque dé fait au minimum la difficulté libre
    if (ReglesOptionnelles.isUsing('degat-minimum-malus-libre')) {
      return `2d10min${diff}`
    }
    return '2d10'
  }

  /* -------------------------------------------- */
  static async prepareEncaissement(targetActor, dmg, roll, armure) {
    const jetTotal = roll.total + dmg.total - armure
    const encaissement = RdDUtility.$selectEncaissement(targetActor, jetTotal, dmg.mortalite)
    const over20 = Math.max(jetTotal - 20, 0)
    encaissement.dmg = dmg
    if (ReglesOptionnelles.isUsing('localisation-aleatoire')) {
      encaissement.dmg.loc = dmg.loc ?? await RdDUtility.getLocalisation(targetActor.type)
      encaissement.dmg.loc.label = encaissement.dmg.loc.label ?? 'Corps'
    }
    else {
      encaissement.dmg.loc = { label: '' }
    }
    encaissement.roll = roll
    encaissement.armure = armure
    encaissement.penetration = dmg.penetration
    encaissement.total = jetTotal
    encaissement.vie = await RdDUtility._evaluatePerte(encaissement.vie, over20)
    if (encaissement.endurance === "@end0") {
      encaissement.endurance = targetActor.system.sante.endurance.value
      encaissement._reduireEndAZero = true
    } else {
      encaissement.endurance = await RdDUtility._evaluatePerte(encaissement.endurance, over20)
    }
    return encaissement
  }

  /* -------------------------------------------- */
  static $selectEncaissement(targetActor, degats, mortalite) {
    const table = RdDUtility.$getTableEncaissement(targetActor, mortalite)
    const encaissement = table.find(it => ((it.minimum ?? degats) <= degats && degats <= (it.maximum ?? degats)))
    return foundry.utils.deepClone(encaissement ?? table[0])
  }

  static $getTableEncaissement(targetActor, mortalite) {
    if (targetActor.isEntite()) {
      return TABLE_ENCAISSEMENT_ENTITE
    }
    const reviser = ReglesOptionnelles.isUsing('tables-encaissement-revisees')
    switch (mortalite) {
      case RDD_CONFIG.encaissement.nonmortel:
        return reviser ? TABLE_ENCAISSEMENT_NONMORTEL_ALTERNATIF : TABLE_ENCAISSEMENT_NONMORTEL
      case RDD_CONFIG.encaissement.entiteincarnee:
        return TABLE_ENCAISSEMENT_ENTITE
    }
    return reviser ? TABLE_ENCAISSEMENT_MORTEL_ALTERNATIF : TABLE_ENCAISSEMENT_MORTEL
  }

  /* -------------------------------------------- */
  static async _evaluatePerte(formula, over20) {
    let perte = new Roll(formula, { over20: over20 });
    await perte.evaluate();
    return perte.total;
  }

  /* -------------------------------------------- */
  static onSocketMessage(sockmsg) {
    switch (sockmsg.msg) {
      case "msg_app_astrologie_refresh":
        return Hooks.callAll(APP_ASTROLOGIE_REFRESH)
      case "msg_request_nombre_astral":
        return game.system.rdd.calendrier.requestNombreAstral(sockmsg.data)
      case "msg_tmr_move":
        let actor = game.actors.get(sockmsg.data.actorId);
        if (actor.isOwner || game.user.isGM) {
          actor.refreshTMRView()
        }
        break
    }
  }

  /* -------------------------------------------- */
  static async chatListeners(html) {
    PasseArme.registerChatCallbacks(html)
    RdDEmpoignade.registerChatCallbacks(html)
    RdDCoeur.registerChatCallbacks(html)
    RdDTextEditor.registerChatCallbacks(html)

    // Gestion spécifique message passeurs
    $(html).on("click", '.tmr-passeur-coord a', event => {
      let coord = event.currentTarget.attributes['data-tmr-coord'].value;
      let actorId = event.currentTarget.attributes['data-actor-id'].value;
      let actor = game.actors.get(actorId);
      actor.tmrApp.positionnerDemiReve(coord);
    });
    // Gestion spécifique des sorts en réserve multiples (ie têtes)
    $(html).on("click", '.declencher-sort-reserve', async event => {
      let coord = event.currentTarget.attributes['data-tmr-coord'].value;
      let sortId = event.currentTarget.attributes['data-sort-id'].value;
      let actorId = event.currentTarget.attributes['data-actor-id'].value;
      let actor = game.actors.get(actorId);
      await actor.tmrApp.lancerSortEnReserve(coord, sortId);
      // TODO: supprimer le message?
    });

    // gestion bouton tchat Possession
    $(html).on("click", '.defense-possession', event => {
      let attackerId = event.currentTarget.attributes['data-attackerId'].value
      let defenderId = event.currentTarget.attributes['data-defenderId'].value
      let possessionId = event.currentTarget.attributes['data-possessionId'].value
      RdDPossession.onDefensePossession(attackerId, defenderId, possessionId)
    });

    // gestion bouton tchat Acheter
    $(html).on("click", '.button-acheter', event => {
      const venteData = DialogItemAchat.preparerAchat(event.currentTarget);
      if (venteData) {
        DialogItemAchat.onAcheter(venteData);
      }
    });
    $(html).on("click", '.button-creer-acteur', event => RdDNameGen.onCreerActeur(event));

    // Gestion du bouton payer
    $(html).on("click", '.payer-button', event => {
      let sommeAPayer = Number(event.currentTarget.attributes['data-somme-a-payer']?.value ?? 0);
      let actor = RdDUtility.getSelectedActor("Pour effectuer le paiement:");
      if (actor) {
        actor.payerSols(sommeAPayer)
        const chatMessage = RdDUtility.findChatMessageId(event.currentTarget)
        ChatUtility.remover(chatMessage)()
      }
    });
    $(html).on("click", '.creer-resultat-chat', async event => {
      const btn = event.currentTarget
      const actor = game.actors.get(btn.dataset.actorId)
      if (!actor) return ui.notifications.warn('Acteur introuvable.')
      const resultat = JSON.parse(btn.dataset.resultat)
      const recipeName = btn.dataset.recipeName
      await actor.createEmbeddedDocuments('Item', [{
        name: resultat.name || recipeName,
        type: resultat.type,
        img: btn.dataset.recipeImg || '',
        system: { quantite: resultat.quantite || 1 },
      }])
      const messageEl = $(btn).closest('.chat-message')
      messageEl.find('.creer-resultat-chat').replaceWith('<span class="resultat-created">✓ Résultat créé</span>')
      ui.notifications.info(`${resultat.name || recipeName} a été créé avec succès !`)
    });
    $(html).on("click", '.rdd-world-content-link', async event => {
      const htmlElement = $(html).find(event.currentTarget);
      const id = htmlElement?.data("id");
      const doctype = htmlElement?.data("doctype");
      switch (doctype ?? 'Item') {
        case 'Actor':
          return game.actors.get(id)?.sheet.render(true);
        case 'Item':
        default:
          return game.items.get(id)?.sheet.render(true);
      }
    });
  }

  static findChatMessageId(current) {
    return RdDUtility.getChatMessageId(RdDUtility.findChatMessage(current));
  }

  static getChatMessageId(node) {
    return node?.attributes.getNamedItem('data-document-id')?.value
      ?? node?.attributes.getNamedItem('data-message-id')?.value;
  }

  static findChatMessage(current) {
    return RdDUtility.findNodeMatching(current, it => it.classList.contains('chat-message')
      && (it.attributes.getNamedItem('data-document-id') || it.attributes.getNamedItem('data-message-id')));
  }

  static findNodeMatching(current, predicate) {
    if (current) {
      if (predicate(current)) {
        return current;
      }
      return RdDUtility.findNodeMatching(current.parentElement, predicate);
    }
    return undefined;
  }

  static getSelectedToken(actor) {
    const controlled = canvas?.tokens?.controlled
    if (controlled?.length > 0) {
      return controlled
        .find(it => it.actor.id == actor.id)
    }
    return undefined
  }

  static getSelectedActor(msgPlayer = undefined) {
    const controlled = canvas?.tokens?.controlled
    if (controlled?.length == 1) {
      let token = controlled[0];
      if (token.actor) {
        return token.actor;
      }
      if (msgPlayer != undefined) {
        msgPlayer += "<br>le token sélectionné doit être lié à un personnage";
      }
    }
    if (game.user.character) {
      return game.user.character;
    }
    if (msgPlayer != undefined) {
      msgPlayer += "<br>vous pouvez sélectionner un seul token lié à un personnage"
      msgPlayer += "<br>vous devez être connecté comme joueur avec un personnage sélectionné";
      ui.notifications.warn(msgPlayer);
      ChatMessage.create({ content: msgPlayer, whisper: [game.user] })
    }
    return undefined
  }

  static doWithSelectedActor(onSelected = () => { }, filter = actor => true) {
    const selected = RdDUtility.getSelectedActor()
    if (selected) {
      onSelected(selected)
    }
    else {
      const actors = game.actors
        .filter(it => it.getUserLevel(game.user) == CONST.DOCUMENT_OWNERSHIP_LEVELS.OWNER)
        .filter(it => ![ACTOR_TYPES.commerce, ACTOR_TYPES.vehicule].includes(it.type))
        .filter(filter)
      const selectData = {
        title: "Choisir un acteur",
        label: "Choisir un acteur",
        list: actors.map(it => Targets.extractActorData(it))
      };
      DialogSelect.select(selectData, it => onSelected(game.actors.get(it.id)))
    }

  }

  /* -------------------------------------------- */
  static createMonnaie(name, cout, img = "", enc = 0.01) {
    let piece = {
      name: name, type: 'monnaie', img: img, _id: foundry.utils.randomID(16),
      dasystemta: {
        quantite: 0,
        cout: cout,
        encombrement: enc,
        description: ""
      }
    }
    return piece;
  }

  /* -------------------------------------------- */
  static afficherDemandePayer(som1, som2) {
    som1 = (som1) ? som1.toLowerCase() : "0d";
    som2 = (som2) ? som2.toLowerCase() : "0d";
    let regExp1 = /(\d+)(\w+)/g;
    let p1 = regExp1.exec(som1);
    let regExp2 = /(\d+)(\w+)/g;
    let p2 = regExp2.exec(som2);
    let deniers = 0;
    let sols = 0;
    if (p1[2] == 'd') deniers += Number(p1[1]);
    if (p1[2] == 's') sols += Number(p1[1]);
    if (p2[2] == 'd') deniers += Number(p2[1]);
    if (p2[2] == 's') sols += Number(p2[1]);

    let sommeAPayer = sols + deniers / 100;
    let msgPayer = `La somme de ${sols} Sols et ${deniers} Deniers est à payer<br>
      <a class='payer-button chat-card-button' data-somme-a-payer='${sommeAPayer}'>Payer</a>`
    ChatMessage.create({ content: msgPayer });
  }

  /* -------------------------------------------- */
  static chatDataSetup(content, modeOverride, isRoll = false, forceWhisper) {
    let chatData = {
      user: game.user.id,
      rollMode: modeOverride || game.settings.get("core", "messageMode"),
      content: content
    }
    ChatUtility.applyRollMode(chatData)

    if (forceWhisper) { // Final force !
      chatData.speaker = ChatMessage.getSpeaker();
      chatData.whisper = ChatMessage.getWhisperRecipients(forceWhisper);
    }
    return chatData;
  }

  /* -------------------------------------------- */
  static confirmSubActeurDelete(sheet, subActor, htmlToDelete, onSuppression = () => { }) {
    RdDConfirm.confirmer({
      settingConfirmer: "confirmation-supprimer-lien-acteur",
      content: `<p>Etes vous certain de vouloir supprimer le lien vers ${subActor.name} ?</p>`,
      title: 'Confirmer la suppression',
      buttonLabel: 'Supprimer le lien',
      onAction: onSuppression
    })
  }

  /* -------------------------------------------- */
  static async confirmActorItemDelete(item, actor) {
    const itemId = item.id;
    const confirmationSuppression = {
      settingConfirmer: "confirmation-supprimer-" + item.getItemGroup(),
      content: `<p>Etes vous certain de vouloir supprimer: ${item.name}?</p>`,
      title: `Supprimer ${item.name}`,
      buttonLabel: "Supprimer",
      onAction: async () => {
        console.log('Delete : ', itemId);
        await actor.deleteEmbeddedDocuments('Item', [itemId], { renderSheet: false });
      }
    };
    if (item.isConteneurNonVide()) {
      confirmationSuppression.content += `<p>Ce conteneur n'est pas vide. Que voulez vous supprimer?</p>`;
      confirmationSuppression.settingConfirmer = undefined;
      RdDConfirm.confirmer(confirmationSuppression,
        {
          'deleteall': {
            icon: '<i class="fas fa-check"></i>',
            label: "Supprimer conteneur et contenu",
            callback: async () => {
              console.log("Delete : ", itemId);
              await actor.deleteAllConteneur(itemId, { renderSheet: false });
            }
          }
        });
    }
    else {
      RdDConfirm.confirmer(confirmationSuppression)
    }
  }

  static slideOnDelete(sheet, htmlToDelete) {
    return htmlToDelete?.slideUp(200, () => sheet.render(false));
  }

  /* -------------------------------------------- */
  static afficherHeuresChanceMalchance(heureNaissance) {
    if (game.user.isGM) {
      const heure = RdDTimestamp.findHeure(heureNaissance)
      if (heureNaissance && heure) {
        const ajustement = game.system.rdd.calendrier.getAjustementAstrologique(heureNaissance)
        const current = game.system.rdd.calendrier.heureCourante()
        ChatMessage.create({
          content: `A l'heure de <strong>${current.label}</strong>, le modificateur de Chance/Malchance est de <strong>${Misc.toSignedString(ajustement)}</strong> pour l'heure de naissance <strong>${heure.label}</strong>.`,
          whisper: ChatUtility.getGMs()
        });
      }
      else if (heureNaissance) {
        ui.notifications.warn(heureNaissance + " ne correspond pas à une heure de naissance");
      }
      else {
        ui.notifications.warn("Pas d'heure de naissance selectionnée");
      }
    } else {
      ui.notifications.warn("Vous n'avez pas accès à cette commande");
    }
  }

  /*-------------------------------------------- */
  static async checkThanatosXP(item) {
    if (item.isCompetencePersonnage() && item.name.includes('Thanatos')) {
      let message = "Vous avez mis des points d'Expérience en Thanatos !<br>Vous devez réduire manuellement d'un même montant d'XP une autre compétence Draconique.";
      await ChatMessage.create({
        whisper: ChatUtility.getUserAndGMs(),
        content: message
      });
    }
  }

}
