/* Common useful functions shared between objects */

import { RdDRollTables } from "./rdd-rolltables.js";
import { ChatUtility } from "./chat-utility.js";
import { RdDCombat } from "./rdd-combat.js";
import { RdDRollResolutionTable } from "./rdd-roll-resolution-table.js";
import { RdDItemCompetenceCreature } from "./item-competencecreature.js";
import { RdDItemArme } from "./item-arme.js";
import { RdDItemCompetence } from "./item-competence.js";

/* -------------------------------------------- */
const categorieCompetences = {
  "generale": { level: "-4", label: "Générales" },
  "particuliere": { level: "-8", label: "Particulières" },
  "specialisee": { level: "-11", label: "Spécialisées" },
  "connaissance": { level: "-11", label: "Connaissances" },
  "draconic": { level: "-11", label: "Draconics" },
  "melee": { level: "-6", label: "Mêlée" },
  "tir": { level: "-8", label: "Tir" },
  "lancer": { level: "-8", label: "Lancer" }
}

/* -------------------------------------------- */
// 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 = [0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10];
const ajustementsConditions = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, +1, +2, +3, +4, +5, +6, +7, +8, +9, +10];
const ajustementsEncaissement = [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, +1, +2, +3, +4, +5, +6, +7, +8, +9, +10, +11, +12, +13, +14, +15, +16, +17, +18, +19, +20, +21, +22, +23, +24, +25];

const tableCaracDerivee = {
  // xp: coût pour passer du niveau inférieur à ce niveau
  1: { xp: 3, poids: "moins de 1kg", plusdom:-5, sconst: 0.5, sust: 0.1 }, 
  2: { xp: 3, poids: "1-5", plusdom:-4, sconst: 0.5, sust: 0.3 }, 
  3: { xp: 4, poids: "6-10", plusdom:-3, sconst: 1, sust: 0.5 , beaute:'hideux'}, 
  4: { xp: 4, poids: "11-20", plusdom:-3, sconst: 1, sust: 1 , beaute:'repoussant'}, 
  5: { xp: 5, poids: "21-30", plusdom:-2, sconst: 1, sust: 1  , beaute:'franchement très laid'}, 
  6: { xp: 5, poids: "31-40", plusdom:-1, sconst: 2, sust: 2 , beaute:'laid'}, 
  7: { xp: 6, poids: "41-50", plusdom:-1, sconst: 2, sust: 2 , beaute:'très désavantagé'}, 
  8: { xp: 6, poids: "51-60", plusdom:0, sconst: 2, sust: 2 , beaute:'désavantagé'}, 
  9: { xp: 7, poids: "61-65", plusdom:0, sconst: 3, sust: 2 , beaute:'pas terrible'}, 
  10: { xp: 7, poids: "66-70", plusdom:0, sconst: 3, sust: 3 , beaute:'commun'}, 
  11: { xp: 8, poids: "71-75", plusdom:0, sconst: 3, sust: 3 , beaute:'pas mal'}, 
  12: { xp: 8, poids: "76-80", plusdom:+1, sconst: 4, sust: 3 , beaute:'avantagé'}, 
  13: { xp: 9, poids: "81-90", plusdom:+1, sconst: 4, sust: 3 , beaute:'mignon'}, 
  14: { xp: 9, poids: "91-100", plusdom:+2, sconst: 4, sust: 4 , beaute:'beau'}, 
  15: { xp: 10, poids: "101-110", plusdom:+2, sconst: 5, sust: 4 , beaute:'très beau'}, 
  16: { xp: 20, poids: "111-120", plusdom:+3, sconst: 5, sust: 4 , beaute:'éblouissant'}, 
  17: { xp: 30, poids: "121-131", plusdom:+3, sconst: 5, sust: 5 }, 
  18: { xp: 40, poids: "131-141", plusdom:+4, sconst: 6, sust: 5 }, 
  19: { xp: 50, poids: "141-150", plusdom:+4, sconst: 6, sust: 5 }, 
  20: { xp: 60, poids: "151-160", plusdom:+4, sconst: 6, sust: 6 }, 
  21: { xp: 70, poids: "161-180", plusdom:+5, sconst: 7, sust: 6 }, 
  22: { xp: 80, poids: "181-200", plusdom:+5, sconst: 7, sust: 7 }, 
  23: { xp: 90, poids: "201-300", plusdom:+6, sconst: 7, sust: 8 }, 
  24: { xp: 100, poids: "301-400", plusdom:+6, sconst: 8, sust: 9 }, 
  25: { xp: 110, poids: "401-500", plusdom:+7, sconst: 8, sust: 10 }, 
  26: { xp: 120, poids: "501-600", plusdom:+7, sconst: 8, sust: 11 }, 
  27: { xp: 130, poids: "601-700", plusdom:+8, sconst: 9, sust: 12 }, 
  28: { xp: 140, poids: "701-800", plusdom:+8, sconst: 9, sust: 13 }, 
  29: { xp: 150, poids: "801-900", plusdom:+9, sconst: 9, sust: 14 }, 
  30: { xp: 160, poids: "901-1000", plusdom:+9, sconst: 10, sust: 15 }, 
  31: { xp: 170, poids: "1001-1500", plusdom:+10, sconst: 10, sust: 16 }, 
  32: { xp: 180, poids: "1501-2000", plusdom:+11, sconst: 10, sust: 17 }
}

/* -------------------------------------------- */
function _buildAllSegmentsFatigue(max) {
  const cycle = [5, 2, 4, 1, 3, 0];
  let fatigue = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]];
  for (let i = 0; i <= max; i++) {
    const ligneFatigue = duplicate(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 = duplicate(line);

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

/* -------------------------------------------- */
const fatigueMatrix = _buildAllSegmentsFatigue(60);
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 fatigueMarche = {
  "aise": { "4": 1, "6": 2, "8": 3, "10": 4, "12": 6 },
  "malaise": { "4": 2, "6": 3, "8": 4, "10": 6 },
  "difficile": { "4": 3, "6": 4, "8": 6 },
  "tresdifficile": { "4": 4, "6": 6 }
}

/* -------------------------------------------- */
/* Static tables for commands /table */
const table2func = {
  "rdd": { descr: "rdd: Ouvre la table de résolution", func: RdDRollResolutionTable.open },
  "queues": { descr: "queues: Tire une queue de Dragon", func: RdDRollTables.getQueue },
  "ombre": { descr: "ombre: Tire une Ombre de Dragon", func: RdDRollTables.getOmbre },
  "tetehr": { descr: "tetehr: Tire une Tête de Dragon pour Hauts Revants", fund: RdDRollTables.getTeteHR },
  "tete": { descr: "tete: Tire une Tête de Dragon", func: RdDRollTables.getTete },
  "souffle": { descr: "souffle: Tire un Souffle de Dragon", func: RdDRollTables.getSouffle },
  "tarot": { descr: "tarot: Tire une carte de Tarot Dracnique", func: RdDRollTables.getTarot }
};

/* -------------------------------------------- */
const definitionsBlessures = [
  { type: "legere", facteur: 2 },
  { type: "grave", facteur: 4 },
  { type: "critique", facteur: 6 }
]

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

/* -------------------------------------------- */
const definitionsEncaissement = {
  "mortel": [
    { minimum: undefined, maximum: 0, endurance: "0", vie: "0", eraflures: 0, legeres: 0, graves: 0, critiques: 0 },
    { minimum: 1, maximum: 10, endurance: "1d4", vie: "0", eraflures: 1, legeres: 0, graves: 0, critiques: 0 },
    { minimum: 11, maximum: 15, endurance: "1d6", vie: "0", eraflures: 0, legeres: 1, graves: 0, critiques: 0 },
    { minimum: 16, maximum: 19, endurance: "2d6", vie: "2", eraflures: 0, legeres: 0, graves: 1, critiques: 0 },
    { minimum: 20, maximum: undefined, endurance: "100", vie: "4 + @over20", eraflures: 0, legeres: 0, graves: 0, critiques: 1 },
  ],
  "non-mortel": [
    { minimum: undefined, maximum: 0, endurance: "0", vie: "0", eraflures: 0, legeres: 0, graves: 0, critiques: 0 },
    { minimum: 1, maximum: 10, endurance: "1d4", vie: "0", eraflures: 1, legeres: 0, graves: 0, critiques: 0 },
    { minimum: 11, maximum: 15, endurance: "1d6", vie: "0", eraflures: 1, legeres: 0, graves: 0, critiques: 0 },
    { minimum: 16, maximum: 19, endurance: "2d6", vie: "0", eraflures: 0, legeres: 1, graves: 0, critiques: 0 },
    { minimum: 20, maximum: undefined, endurance: "100", vie: "0", eraflures: 0, legeres: 1, graves: 0, critiques: 0 },
  ],
  "cauchemar": [
    { minimum: undefined, maximum: 0, endurance: "0", vie: "0", eraflures: 0, legeres: 0, graves: 0, critiques: 0 },
    { minimum: 1, maximum: 10, endurance: "1d4", vie: "0", eraflures: 1, legeres: 0, graves: 0, critiques: 0 },
    { minimum: 11, maximum: 15, endurance: "1d6", vie: "0", eraflures: 1, legeres: 0, graves: 0, critiques: 0 },
    { minimum: 16, maximum: 19, endurance: "2d6", vie: "0", eraflures: 1, legeres: 0, graves: 0, critiques: 0 },
    { minimum: 20, maximum: undefined, endurance: "3d6 + @over20", vie: "0", eraflures: 1, legeres: 0, graves: 0, critiques: 0 },
  ]
};

/* -------------------------------------------- */
export class RdDUtility {
  /* -------------------------------------------- */
  static async preloadHandlebarsTemplates() {
    const templatePaths = [
      //Character Sheets
      'systems/foundryvtt-reve-de-dragon/templates/actor-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/actor-creature-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/actor-entite-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/actor-vehicule-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/actor-sheet-competence-partial.html',
      //Items
      'systems/foundryvtt-reve-de-dragon/templates/item-competence-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-competencecreature-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-arme-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-armure-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-objet-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-conteneur-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-sort-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-herbe-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-ingredient-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-livre-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-tache-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-potion-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-rencontresTMR-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-queue-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-souffle-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-tarot-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-tete-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-ombre-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-monnaie-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/item-meditation-sheet.html',
      'systems/foundryvtt-reve-de-dragon/templates/competence-carac-defaut.html',
      'systems/foundryvtt-reve-de-dragon/templates/competence-base.html',
      'systems/foundryvtt-reve-de-dragon/templates/enum-aspect-tarot.html',
      'systems/foundryvtt-reve-de-dragon/templates/enum-categorie-competence.html',
      'systems/foundryvtt-reve-de-dragon/templates/enum-categorie-ingredient.html',
      'systems/foundryvtt-reve-de-dragon/templates/enum-categorie-parade.html',
      'systems/foundryvtt-reve-de-dragon/templates/enum-categorie-vehicule.html',
      'systems/foundryvtt-reve-de-dragon/templates/enum-competence.html',
      'systems/foundryvtt-reve-de-dragon/templates/enum-rarete.html',
      'systems/foundryvtt-reve-de-dragon/templates/sort-draconic.html',
      'systems/foundryvtt-reve-de-dragon/templates/sort-tmr.html',
      'systems/foundryvtt-reve-de-dragon/templates/niveau-ethylisme.html',
      'systems/foundryvtt-reve-de-dragon/templates/casetmr-specific-list.html',
      // Dialogs
      'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-ajustements.html',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-resolution.html',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-competence.html',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-carac.html',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-sort.html',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-encaisser.html',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-meditation.html',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-tmr.html',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-surenc.html',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-enctotal.html',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-alchimie.html',
      'systems/foundryvtt-reve-de-dragon/templates/dialog-astrologie-joueur.html',
      // Calendrier
      'systems/foundryvtt-reve-de-dragon/templates/calendar-template.html',
      'systems/foundryvtt-reve-de-dragon/templates/calendar-editor-template.html',
      'systems/foundryvtt-reve-de-dragon/templates/heures-select-option.html',
      // Conteneur/item in Actor sheet
      'systems/foundryvtt-reve-de-dragon/templates/actor-inventaire-conteneur.html',
      'systems/foundryvtt-reve-de-dragon/templates/editor-notes-mj.html',
      // HUD
      'systems/foundryvtt-reve-de-dragon/templates/hud-actor-init.html',
      'systems/foundryvtt-reve-de-dragon/templates/hud-actor-attaque.html',
      // messages tchat
      'systems/foundryvtt-reve-de-dragon/templates/chat-infojet.html',
      'systems/foundryvtt-reve-de-dragon/templates/chat-demande-defense.html',
      'systems/foundryvtt-reve-de-dragon/templates/chat-demande-attaque-particuliere.html',
      'systems/foundryvtt-reve-de-dragon/templates/chat-demande-attaque-etotal.html',
      'systems/foundryvtt-reve-de-dragon/templates/chat-resultat-appelchance.html',
      'systems/foundryvtt-reve-de-dragon/templates/chat-resultat-attaque.html',
      'systems/foundryvtt-reve-de-dragon/templates/chat-resultat-encaissement.html',
      'systems/foundryvtt-reve-de-dragon/templates/chat-resultat-parade.html',
      'systems/foundryvtt-reve-de-dragon/templates/chat-resultat-esquive.html',
      'systems/foundryvtt-reve-de-dragon/templates/chat-resultat-competence.html',
      'systems/foundryvtt-reve-de-dragon/templates/chat-resultat-general.html',
      'systems/foundryvtt-reve-de-dragon/templates/chat-resultat-tache.html',
      'systems/foundryvtt-reve-de-dragon/templates/chat-resultat-sort.html',
      'systems/foundryvtt-reve-de-dragon/templates/chat-resultat-alchimie.html',
      'systems/foundryvtt-reve-de-dragon/templates/chat-actor-turn-summary.html',
      'systems/foundryvtt-reve-de-dragon/templates/chat-actor-competence-xp.html',
      'systems/foundryvtt-reve-de-dragon/templates/chat-actor-carac-xp.html'
    ];

    return loadTemplates(templatePaths);
  }

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

  /* -------------------------------------------- */
  static getNomEthylisme(niveauEthylisme) {
    let index = -niveauEthylisme;
    return index < 0 ? 'Aucun' : nomEthylisme[index];
  }

  /* -------------------------------------------- */
  static initAfficheContenu(actorId) { // persistent handling of conteneur show/hide
    if (!this.afficheContenu)
      this.afficheContenu = {};
  }
  /* -------------------------------------------- */
  static toggleAfficheContenu(conteneurId) {
    this.afficheContenu[conteneurId] = !this.afficheContenu[conteneurId];
  }
  /* -------------------------------------------- */
  static getAfficheContenu(conteneurId) {
    if ( conteneurId )
      return this.afficheContenu[conteneurId];
    return undefined;
  }

  /* -------------------------------------------- */
  static filterItemsPerTypeForSheet(data) {
    data.data.materiel = this.checkNull(data.itemsByType['objet']);
    data.data.conteneurs = this.checkNull(data.itemsByType['conteneur']);
    data.data.armes = this.checkNull(data.itemsByType['arme']);
    data.data.armures = this.checkNull(data.itemsByType['armure']);
    data.data.livres = this.checkNull(data.itemsByType['livre']);
    data.data.potions = this.checkNull(data.itemsByType['potion']);
    data.data.ingredients = this.checkNull(data.itemsByType['ingredient']);
    data.data.munitions = this.checkNull(data.itemsByType['munition']);
    data.data.herbes = this.checkNull(data.itemsByType['herbe']);
    data.data.sorts = this.checkNull(data.itemsByType['sort']);
    data.data.queues = this.checkNull(data.itemsByType['queue']);
    data.data.souffles = this.checkNull(data.itemsByType['souffle']);
    data.data.ombres = this.checkNull(data.itemsByType['ombre']);
    data.data.tetes = this.checkNull(data.itemsByType['tete']);
    data.data.taches = this.checkNull(data.itemsByType['tache']);
    data.data.monnaie = this.checkNull(data.itemsByType['monnaie']);
    data.data.meditations = this.checkNull(data.itemsByType['meditation']);
    data.data.recettesAlchimiques = this.checkNull(data.itemsByType['recettealchimique']);
    data.data.objets = data.data.conteneurs.concat(data.data.materiel).concat(data.data.armes).concat(data.data.armures).concat(data.data.munitions).concat(data.data.livres).concat(data.data.potions).concat(data.data.herbes).concat(data.data.ingredients);
  }

  /* -------------------------------------------- */
  static async processItemDropEvent(actorSheet, event) {
    let dragData = JSON.parse(event.dataTransfer.getData("text/plain"));
    console.log(dragData, actorSheet.actor._id);
    let dropID = $(event.target).parents(".item").attr("data-item-id"); // Only relevant if container drop
    let objetId = dragData.id || dragData.data._id;
    if ( dragData.type == 'Item') {
      if ( dropID ) { // Dropped over an item !!!
        if (actorSheet.objetVersConteneur[objetId] != dropID && objetId != dropID) {
          if (actorSheet.actor.validateConteneur(objetId, dropID) && actorSheet.actor.testConteneurCapacite(objetId, dropID)) {
            await actorSheet.actor.enleverDeConteneur(objetId, actorSheet.objetVersConteneur[objetId]);
            await actorSheet.actor.ajouterAConteneur(objetId, dropID);
          }
        }
      }
      if (dragData.actorId && dragData.actorId != actorSheet.actor._id ) { // Un acteur est à l'origine de l'item -> deplacement
        console.log("Moving objects");
        actorSheet.actor.moveItemsBetweenActors( objetId, dragData.actorId);
        return false;
      }
      actorSheet.actor.computeEncombrementTotalEtMalusArmure();
    } else if ( dragData.type == "Actor" ) { 
      actorSheet.actor.addSubacteur( objetId );
    }
    return true;
  }

  /* -------------------------------------------- */
  static buildArbreDeConteneur(actorSheet, data) {
    actorSheet.objetVersConteneur = {}; // Table de hash locale pour recupération rapide du conteneur parent (si existant)    
    // Attribution des objets aux conteneurs
    for (let conteneur of data.data.conteneurs) {
      conteneur.subItems = [];
      if (!conteneur.data.encTotal) conteneur.data.encTotal = 0;
      //conteneur.data.encTotal = ; Deja calculé
      if (conteneur.data.contenu) {
        for (let id of conteneur.data.contenu) {
          let objet = data.data.objets.find(objet => (id == objet._id));
          if (objet) {
            if (!objet.data.encombrement) objet.data.encombrement = 0; // Auto-fix
            objet.estContenu = true; // Permet de filtrer ce qifui est porté dans le template
            actorSheet.objetVersConteneur[id] = conteneur._id;
            conteneur.data.encTotal += Number(objet.data.encombrement) * Number(((objet.data.quantite) ? objet.data.quantite : 1));
            conteneur.subItems.push(objet);
          }
        }
      }
    }
    // Construit la liste des conteneurs de niveau 1 (c'est à dire non contenu eux-même dans un conteneur)
    let newConteneurs = data.data.conteneurs.filter(function (conteneur, index, arr) { return !conteneur.estContenu });
    data.data.conteneurs = newConteneurs;
    //console.log(newConteneurs);
  }

  /* -------------------------------------------- */
  /** Construit la structure récursive des conteneurs, avec imbrication potentielle
   * 
   */
  static buildConteneur(objet, niveau) {
    if (!niveau) niveau = 1;
    objet.niveau = niveau;
    //console.log("OBJ:", objet);
    let str = Handlebars.partials['systems/foundryvtt-reve-de-dragon/templates/actor-inventaire-conteneur.html']({ item: objet });
    if (objet.type == 'conteneur') {
      //console.log("ITEM DISPLAYED", objet );
      if (this.getAfficheContenu(objet._id)) {
        str = str + "<ul class='item-list alterne-list item-display-show list-item-margin" + niveau + "'>";
      } else {
        str = str + "<ul class='item-list alterne-list item-display-hide list-item-margin" + niveau + "'>";
      }
      for (let subItem of objet.subItems) {
        str = str + this.buildConteneur(subItem, niveau + 1);
      }
      str = str + "</ul>";
    }
    return new Handlebars.SafeString(str);
  }

  /* -------------------------------------------- */
  static getCategorieCompetences() {
    return categorieCompetences;
  }
  static getLevelCategory(category) {
    return categorieCompetences[category].level;
  }
  static getLabelCategory(category) {
    return categorieCompetences[category].label;
  }
  static getCaracArray() {
    return carac_array;
  }
  static getDifficultesLibres() {
    return difficultesLibres;
  }
  static getAjustementsConditions() {
    return ajustementsConditions;
  }
  static getAjustementsEncaissement() {
    return ajustementsEncaissement;
  }

  static getDefinitionsBlessures() {
    return definitionsBlessures;
  }

  /* -------------------------------------------- */
  static getCaracNextXp(value) {
    // xp est le coût pour atteindre cette valeur, on regarde donc le coût de la valeur+1
    return tableCaracDerivee[Number(value)+1].xp;
  }

  /* -------------------------------------------- */
  /** Retourne une liste triée d'armes avec le split arme1 main / arme 2 main */
  static _finalizeArmeList(armes, competences, carac) {
    // Gestion des armes 1/2 mains
    let armesEquipe = [];
    for (const arme of armes) {
      if (arme.data.equipe) {
        armesEquipe.push(arme);
        let comp = competences.find(c => c.name == arme.data.competence);
        arme.data.initiative = RdDUtility.calculInitiative(arme.data.niveau, carac[comp.data.defaut_carac].value);
        // Dupliquer les armes pouvant être à 1 main et 2 mains en patchant la compétence
        if (arme.data.unemain && !arme.data.deuxmains) {
          arme.data.mainInfo = "(1m)";
        } else if (!arme.data.unemain && arme.data.deuxmains) {
          arme.data.mainInfo = "(2m)";
        } else if (arme.data.unemain && arme.data.deuxmains) {
          arme.data.mainInfo = "(1m)";
          let arme2main = duplicate(arme);
          arme2main.data.mainInfo = "(2m)";
          arme2main.data.dommages = arme2main.data.dommages.split("/")[1]; // Existence temporaire uniquement dans la liste des armes, donc OK
          arme2main.data.competence = arme2main.data.competence.replace(" 1 main", " 2 mains"); // Replace !
          let comp = competences.find(c => c.name == arme2main.data.competence);
          arme2main.data.niveau = comp.data.niveau;
          arme2main.data.initiative = RdDUtility.calculInitiative(arme2main.data.niveau, carac[comp.data.defaut_carac].value);
          armesEquipe.push(arme2main);
        }
      }
    }
    return armesEquipe.sort((a, b) => {
      const nameA = a.name + (a.data.mainInfo ?? '');
      const nameB = b.name + (b.data.mainInfo ?? '');
      if (nameA > nameB) return 1;
      if (nameA < nameB) return -1;
      return 0;
    });
  }

  /* -------------------------------------------- */
  static calculInitiative(niveau, caracValue) {
    let base = niveau + Math.floor(caracValue / 2);
    return "1d6" + (base >= 0 ? "+" : "") + base;
  }

  /* -------------------------------------------- */
  static computeCarac(data) {
    data.carac.force.value = Math.min(data.carac.force.value, parseInt(data.carac.taille.value) + 4);

    data.carac.derobee.value = Math.floor(parseInt(((21 - data.carac.taille.value)) + parseInt(data.carac.agilite.value)) / 2);
    let bonusDomKey = Math.floor((parseInt(data.carac.force.value) + parseInt(data.carac.taille.value)) / 2);
    bonusDomKey = Math.min( Math.max(bonusDomKey, 0), 32); // Clamp de securite

    let tailleData = tableCaracDerivee[bonusDomKey];
    data.attributs.plusdom.value = tailleData.plusdom;

    tailleData = tableCaracDerivee[Number(data.carac.taille.value)];
    data.attributs.sconst.value = tailleData.sconst; 
    data.attributs.sust.value   = tailleData.sust; 

    data.attributs.encombrement.value = (parseInt(data.carac.force.value) + parseInt(data.carac.taille.value)) / 2;
    data.carac.melee.value = Math.floor((parseInt(data.carac.force.value) + parseInt(data.carac.agilite.value)) / 2);
    data.carac.tir.value = Math.floor((parseInt(data.carac.vue.value) + parseInt(data.carac.dexterite.value)) / 2);
    data.carac.lancer.value = Math.floor((parseInt(data.carac.tir.value) + parseInt(data.carac.force.value)) / 2);

    data.sante.vie.max = Math.ceil((parseInt(data.carac.taille.value) + parseInt(data.carac.constitution.value)) / 2);

    data.sante.vie.value = Math.min(data.sante.vie.value, data.sante.vie.max)
    data.sante.endurance.max = Math.max(parseInt(data.carac.taille.value) + parseInt(data.carac.constitution.value), parseInt(data.sante.vie.max) + parseInt(data.carac.volonte.value));
    data.sante.endurance.value = Math.min(data.sante.endurance.value, data.sante.endurance.max);
    data.sante.fatigue.max = data.sante.endurance.max * 2;
    data.sante.fatigue.value = Math.min(data.sante.fatigue.value, data.sante.fatigue.max);

    //Compteurs
    data.reve.reve.max = data.carac.reve.value;
    data.compteurs.chance.max = data.carac.chance.value;
  }

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

  /* -------------------------------------------- */
  static calculMalusFatigue(fatigue, maxEnd) {
    maxEnd = Math.max(maxEnd, 1);
    maxEnd = Math.min(maxEnd, cumulFatigueMatrix.length);
    let segments = cumulFatigueMatrix[maxEnd];
    for (let i = 0; i < 12; i++) {
      if (fatigue <= segments[i]) {
        return fatigueMalus[i]
      }
    }
    return -7;
  }
  /* -------------------------------------------- */
  // Build the nice (?) html table used to manage fatigue.
  // max should be the endurance max value
  static makeHTMLfatigueMatrix(fatigue, maxEndurance) {
    let 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 getLocalisation() {
    // TODO: bouger dans une RollTable du compendium et chercher dans les RoolTable puis compendium pour permettre le changement?
    let result = new Roll("1d20").roll().total;
    let txt = ""
    if (result <= 3) txt = "Jambe, genou, pied, jarret";
    else if (result <= 7) txt = "Hanche, cuisse, fesse";
    else if (result <= 9) txt = "Ventre, reins";
    else if (result <= 12) txt = "Poitrine, dos";
    else if (result <= 14) txt = "Avant-bras, main, coude";
    else if (result <= 18) txt = "Epaule, bras, omoplate";
    else if (result == 19) txt = "Tête";
    else if (result == 20) txt = "Tête (visage)";

    return { result: result, label: txt };
  }

  /* -------------------------------------------- */
  static selectEncaissement(degats, mortalite) {
    const table = definitionsEncaissement[mortalite] === undefined ? definitionsEncaissement["mortel"] : definitionsEncaissement[mortalite];
    for (let encaissement of table) {
      if ((encaissement.minimum === undefined || encaissement.minimum <= degats)
        && (encaissement.maximum === undefined || degats <= encaissement.maximum)) {
        return duplicate(encaissement);
      }
    }
    return duplicate(table[0]);
  }


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

  /* -------------------------------------------- */
  static currentFatigueMalus(value, max) {
    max = Math.max(1, Math.min(max, 60));
    value = Math.min(max * 2, Math.max(0, value));

    let fatigueTab = fatigueMatrix[max];
    let fatigueRem = value;
    for (let idx = 0; idx < fatigueTab.length; idx++) {
      fatigueRem -= fatigueTab[idx];
      if (fatigueRem <= 0) {
        return fatigueMalus[idx];
      }
    }
    return -7; // This is the max !
  }

  /* -------------------------------------------- */
  static async loadCompendiumNames(compendium) {
    const pack = game.packs.get(compendium);
    let competences;
    await pack.getIndex().then(index => competences = index);
    return competences;
  }

  /* -------------------------------------------- */
  static async loadCompendium(compendium, filter = item => true) {
    let compendiumItems = await RdDUtility.loadCompendiumNames(compendium);

    const pack = game.packs.get(compendium);
    let list = [];
    for (let compendiumItem of compendiumItems) {
      await pack.getEntity(compendiumItem._id).then(it => {
        const item = it.data;
        if (filter(item)) {
          list.push(item);
        }
      });
    };
    return list;
  }

  /* -------------------------------------------- */
  static async responseNombreAstral(data) {
    let actor = game.actors.get(data.id);
    actor.ajouteNombreAstral(data);
  }

  /* -------------------------------------------- */
  static onSocketMesssage(sockmsg) {
    console.log(">>>>> MSG RECV", sockmsg);
    switch (sockmsg.msg) {
      case "msg_gm_chat_message":
        return ChatUtility.handleGMChatMessage(sockmsg.data);
      case "msg_sync_time":
        return game.system.rdd.calendrier.syncPlayerTime(sockmsg.data);
      case "msg_request_nombre_astral":
        return game.system.rdd.calendrier.requestNombreAstral(sockmsg.data);
      case "msg_response_nombre_astral":
        return RdDUtility.responseNombreAstral(sockmsg.data);
    }
  }

  /* -------------------------------------------- */
  static rollInitiativeCompetence(combatantId, arme) {
    const combatant = game.combat.getCombatant(combatantId);
    const actor = combatant.actor;
    
    let initOffset   = 0;
    let caracForInit = 0;
    let compNiveau   = 0;
    if ( actor.getSurprise() == "totale") {
      initOffset = -1; // To force 0
    } else if ( actor.getSurprise() == "demi") {
      initOffset = 0;
    } else if (arme.name == "Autre action") {
      initOffset = 2;
    } else if (arme.name == "Draconic") {
      initOffset = 7;
    } else {
      initOffset = 3; // Melée = 3.XX
      let competence = RdDItemCompetence.findCompetence(combatant.actor.data.items, arme.data.competence);
      compNiveau = competence.data.niveau;
      
      if (actor.data.type == 'creature' || actor.data.type == 'entite') {
        caracForInit = competence.data.carac_value;
        if ( competence.data.categorie == "lancer") { 
          initOffset = 5;
        }
      } else {
        caracForInit = actor.data.data.carac[competence.data.defaut_carac].value;
        if (competence.data.categorie == "lancer") { // Offset de principe pour les armes de jet
          initOffset = 4;
        }
        if (competence.data.categorie == "tir") { // Offset de principe pour les armes de jet
          initOffset = 5;
        }
        if (competence.data.categorie == "melee") { // Offset de principe pour les armes de jet
          initOffset = 3;
        }
      }
    }
    let malus = actor.getEtatGeneral(); // Prise en compte état général 
    // Cas des créatures et entités vs personnages
    let rollFormula = initOffset + "+ ( (" + RdDUtility.calculInitiative(compNiveau, caracForInit) + " + " + malus + ") /100)";
    game.combat.rollInitiative(combatantId, rollFormula);
  }

  /* -------------------------------------------- */
  static buildListeActionsCombat(combatant) {
    const actor = combatant.actor;  // Easy access
    let items = actor.data.items;
    let actions = []
    if (actor.isCreature()) {
      actions = actions.concat(items.filter(it => it.type == 'competencecreature' && it.data.iscombat)
        .map(competence => RdDItemCompetenceCreature.toArme(competence)));
    } else {
      // Recupération des items 'arme'
      let armes = items.filter(it => it.type == 'arme')
        .map(arme => duplicate(arme)) /* pas de changements aux armes d'origine */
        .concat(RdDItemArme.mainsNues());

      let competences = items.filter(it => it.type == 'competence');
      actions = actions.concat(this._finalizeArmeList(armes, competences, actor.data.data.carac));

      actions.push({ name: "Draconic", data: { initOnly: true, competence: "Draconic" } });
    }

    actions.push({ name: "Autre action", data: { initOnly: true, competence: "Autre action" } });
    for (let index = 0; index < actions.length; index++) {
      actions[index].index = index;
    }
    return actions;
  }

  /* -------------------------------------------- */
  static displayInitiativeMenu(html, combatantId) {
    const combatant = game.combat.getCombatant(combatantId);
    let armesList = this.buildListeActionsCombat(combatant);

    // Build the relevant submenu
    if (armesList) {
      let menuItems = [];
      for (let arme of armesList) {
        menuItems.push({
          name: arme.data.competence,
          icon: "<i class='fas fa-dice-d6'></i>",
          callback: target => { RdDUtility.rollInitiativeCompetence(combatantId, arme) }
        });
      }
      new ContextMenu(html, ".directory-list", menuItems).render();
    }
  }

  /* -------------------------------------------- */
  static pushInitiativeOptions(html, options) {
    options.push(
      {
        name: "Sélectionner l'initiative...",
        condition: true,
        icon: '<i class="far fa-question-circle"></i>',
        callback: target => {
          RdDUtility.displayInitiativeMenu(html, target.data('combatant-id'));
        }
      });
  }

  /* -------------------------------------------- */
  static async chatListeners(html) {
    RdDCombat.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.forceDemiRevePosition(coord);
    });
    // Gestion spécifique des sorts en réserve multiples (ie têtes)
    html.on("click", '#sort-reserve', 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);
      actor.tmrApp.lancerSortEnReserve(coord, sortId);
    });
    // Gestion du bouton payer
    html.on("click", '#payer-button', event => {
      let sumdenier = event.currentTarget.attributes['data-somme-denier'].value;
      let jsondata = event.currentTarget.attributes['data-jsondata']
      let objData
      if (jsondata) {
        objData = JSON.parse(jsondata.value)
      }
      if (game.user.character) {
        game.user.character.payerDenier(sumdenier, objData);
      } else {
        let msgPayer = "Vous devez avoir un acteur relié pour effectuer le paiement";
        ChatMessage.create({ content: msgPayer, whisper: [game.user] });
      }
    });
  }

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

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

    let sumtotald = sumd + (sums * 100);
    let msgPayer = "La somme de " + sums + " Sols et " + sumd + " Deniers est à payer, cliquer sur le lien ci-dessous si besoin.<br>";
    msgPayer += "<a id='payer-button' class='chat-card-button' data-somme-denier='" + sumtotald + "'>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", "rollMode"),
      content: content
    };

    if (["gmroll", "blindroll"].includes(chatData.rollMode)) chatData["whisper"] = ChatMessage.getWhisperRecipients("GM").map(u => u.id);
    if (chatData.rollMode === "blindroll") chatData["blind"] = true;
    else if (chatData.rollMode === "selfroll") chatData["whisper"] = [game.user];

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

    return chatData;    
  }

    /* -------------------------------------------- */
    static confirmerSuppressionSubacteur(actorSheet, li ) {
      let actorId = li.data("actor-id");
      let actor   = game.actors.get( actorId );
      let msgTxt = "<p>Etes vous certain de vouloir supprimer le lien vers ce véhicule/monture/suivant : " + actor.data.name +" ?</p>";
      let buttons = { 
        delete: {
            icon: '<i class="fas fa-check"></i>',
            label: "Supprimer le lien",
            callback: () => { 
              console.log("Delete : ", actorId);
              actorSheet.actor.removeSubacteur( actorId );
              li.slideUp(200, () => actorSheet.render(false));    
            }
          },
          cancel: {
            icon: '<i class="fas fa-times"></i>',
            label: "Annuler"
          }            
      }
      let d = new Dialog({
            title: "Confirmer la suppression du lien",
            content: msgTxt, 
            buttons: buttons, 
            default: "cancel"
      });
      d.render(true);
    }
    
  /* -------------------------------------------- */
  static async confirmerSuppression(actorSheet, li) {
      let itemId = li.data("item-id");
      let objet  = actorSheet.actor.items.find( item => item._id == itemId );
      let msgTxt = "<p>Etes vous certain de vouloir supprimer cet objet ?";
      let buttons = { delete: {
                        icon: '<i class="fas fa-check"></i>',
                        label: "Supprimer l'objet",
                        callback: () => { 
                          console.log("Delete : ", itemId);
                          actorSheet.actor.deleteOwnedItem(  itemId );
                          li.slideUp(200, () => actorSheet.render(false));    
                        }
                      },
                      cancel: {
                        icon: '<i class="fas fa-times"></i>',
                        label: "Annuler"
                      }            
                  }
      if ( objet.data.type == 'conteneur' && objet.data.data.contenu.length > 0) {
        msgTxt += "<br>Cet objet est aussi un conteneur avec du contenu : choisissez l'option de suppression";
        buttons['deleteall'] = { 
              icon: '<i class="fas fa-check"></i>',
              label: "Supprimer le conteneur et tout son contenu",
              callback: () => { 
                console.log("Delete : ", itemId);
                actorSheet.actor.deleteAllConteneur(  itemId );
                li.slideUp(200, () => actorSheet.render(false));    
              }
          }
      }
      msgTxt +=  "</p>";
      let d = new Dialog({
        title: "Confirmer la suppression",
        content: msgTxt, 
        buttons: buttons, 
        default: "cancel"
      });
      d.render(true);
  }
  
  /* -------------------------------------------- */
  static afficherHeuresChanceMalchance( heureNaissance ) {
    let ajustement = game.system.rdd.calendrier.getAjustementAstrologique(heureNaissance.toLowerCase());
    ChatMessage.create( { 
      content: `Pour l'heure ${game.system.rdd.calendrier.getCurrentHeure()}, le modificateur de Chance/Malchance est de : ${ajustement}.`, 
      whisper: ChatMessage.getWhisperRecipients("MJ")
      } );     
  }

}