import { RdDItemArme } from "./item/arme.js";
import { RdDItemCompetence } from "./item-competence.js";
import { RdDItemMeditation } from "./item-meditation.js";
import { RdDItemSort } from "./item-sort.js";
import { Misc } from "./misc.js";
import { RdDBonus } from "./rdd-bonus.js";
import { RdDCarac } from "./rdd-carac.js";
import { RdDPossession } from "./combat/rdd-possession.js";
import { RdDUtility } from "./rdd-utility.js";
import { ReglesOptionnelles } from "./settings/regles-optionnelles.js";

/**
 * tous les ajustements pouvant s'appliquer.
 * un ajustement se compose de `function(rollData, actor)` :
 * - isVisible: indique si l'ajustement sera visible (pour les cas où il peut être sélectionné)
 * - isUsed: évalue si l'ajustement indique s'il est actif ou non
 * - getLabel: un libellé texte pour l'affichage de l'ajustement
 * - getValue: une valeur numérique correspondant à un modificateur entier
 * - getDescr: une valeur textuelle pour les informations non numériques (demi-surprise, bonus de case, ...)
 */
export const referenceAjustements = {
  competence: {
    isUsed: (rollData, actor) => rollData.competence,
    getLabel: (rollData, actor) => rollData.competence?.name,
    getValue: (rollData, actor) => rollData.competence?.system?.niveau,
  },
  diffLibre: {
    isUsed: (rollData, actor) => rollData.diffLibre != undefined,
    getLabel: (rollData, actor) => rollData.selectedSort?.name ?? rollData.attackerRoll ?? RdDPossession.isDefensePossession(rollData) ? 'Imposée' : 'Libre',
    getValue: (rollData, actor) => rollData.selectedSort
      ? RdDItemSort.getDifficulte(rollData.selectedSort, rollData.diffLibre)
      : rollData.diffLibre ?? rollData.competence?.system.default_diffLibre ?? 0
  },
  diffConditions: {
    isUsed: (rollData, actor) => rollData.diffConditions != undefined,
    getLabel: (rollData, actor) => 'Conditions',
    getValue: (rollData, actor) => rollData.diffConditions
  },
  etat: {
    isUsed: (rollData, actor) => !RollDataAjustements.isIgnoreEtatGeneral(rollData),
    getLabel: (rollData, actor) => 'Etat général',
    getValue: (rollData, actor) => actor.getEtatGeneral({ ethylisme: rollData.forceAlcool != undefined })
  },
  malusArmure: {
    isVisible: (rollData, actor) => RdDCarac.isAgiliteOuDerobee(rollData.selectedCarac?.label),
    isUsed: (rollData, actor) => RdDCarac.isAgiliteOuDerobee(rollData.selectedCarac?.label),
    getLabel: (rollData, actor) => 'Malus armure',
    getValue: (rollData, actor) => actor.getMalusArmure()
  },
  encTotal: {
    isVisible: (rollData, actor) => RdDCarac.isAgiliteOuDerobee(rollData.selectedCarac?.label) && RdDItemCompetence.isMalusEncombrementTotal(rollData.competence?.name),
    isUsed: (rollData, actor) => !rollData.oeuvre && RdDCarac.isAgiliteOuDerobee(rollData.selectedCarac?.label) && RdDItemCompetence.isMalusEncombrementTotal(rollData.competence?.name) && rollData.use?.encTotal,
    getLabel: (rollData, actor) => 'Encombrement total',
    getValue: (rollData, actor) => -actor.getEncTotal()
  },
  surenc: {
    isVisible: (rollData, actor) => RdDCarac.isActionPhysique(rollData.selectedCarac?.label) && actor.isSurenc(),
    isUsed: (rollData, actor) => rollData.use.surenc && RdDCarac.isActionPhysique(rollData.selectedCarac?.label),
    getLabel: (rollData, actor) => 'Sur-encombrement',
    getValue: (rollData, actor) => actor.computeMalusSurEncombrement()
  },
  astrologique: {
    isVisible: (rollData, actor) => actor.isPersonnage() && ReglesOptionnelles.isUsing("astrologie") && RdDCarac.isChance(rollData.selectedCarac?.label),
    isUsed: (rollData, actor) =>  RdDCarac.isChance(rollData.selectedCarac?.label) && rollData.use.astrologique,
    getLabel: (rollData, actor) => 'Astrologique',
    getValue: (rollData, actor) => actor.ajustementAstrologique()
  },
  moral: {
    isVisible: (rollData, actor) => actor.isPersonnage() && RdDCarac.isActionPhysique(rollData.selectedCarac?.label) && rollData.use?.moral,
    isUsed: (rollData, actor) => rollData.use.moral,
    getLabel: (rollData, actor) => 'Appel au moral',
    getValue: (rollData, actor) => 1
  },
  moralTotal: {
    isUsed: (rollData, actor) => RdDCarac.isVolonte(rollData.selectedCarac?.label),
    getLabel: (rollData, actor) => 'Moral',
    getValue: (rollData, actor) => actor.getMoralTotal()
  },
  coeur: {
    isVisible: (rollData, actor) => actor.isPersonnage() && RdDCarac.isVolonte(rollData.selectedCarac?.label),
    isUsed: (rollData, actor) => rollData.use.coeur != undefined,
    getLabel: (rollData, actor) => 'Ajustement de c&oelig;ur',
    getValue: (rollData, actor) => -2 * (rollData.use.coeur?.coeur ?? 0)
  },


  
  meditation: {
    isUsed: (rollData, actor) => rollData.meditation,
    getLabel: (rollData, actor) => 'Méditation',
    getValue: (rollData, actor) => RdDItemMeditation.calculDifficulte(rollData)
  },
  ethylismeAlcool: {
    isVisible: (rollData, actor) => rollData.nbDoses != undefined,
    isUsed: (rollData, actor) => rollData.nbDoses != undefined,
    getLabel: (rollData, actor) => "Doses déjà bues: ",
    getValue: (rollData, actor) => - rollData.nbDoses,
  },
  ethylismeDoses: {
    isVisible: (rollData, actor) => rollData.nbDoses != undefined,
    isUsed: (rollData, actor) => rollData.nbDoses != undefined,
    getLabel: (rollData, actor) => "Force de l'alcool: ",
    getValue: (rollData, actor) => rollData.forceAlcool,
  },
  ethylisme: {
    isVisible: (rollData, actor) => rollData.ethylisme != undefined,
    isUsed: (rollData, actor) => rollData.ethylisme != undefined,
    getLabel: (rollData, actor) => "Ethylisme - " + RdDUtility.getNomEthylisme(rollData.ethylisme),
    getValue: (rollData, actor) => rollData.ethylisme,
  },
  facteurSign: {
    isUsed: (rollData, actor) => rollData.diviseurSignificative > 1,
    getLabel: (rollData, actor) => Misc.getFractionOneN(rollData.diviseurSignificative),
    getDescr: (rollData, actor) => rollData.diviseurSignificative > 1 ? `Facteur significative <span class="rdd-diviseur">&times;${Misc.getFractionOneN(rollData.diviseurSignificative)}</span>` : ''
  },
  isEcaille: {
    isVisible: (rollData, actor) => rollData.arme?.system.magique && Number(rollData.arme?.system.ecaille_efficacite) > 0,
    isUsed: (rollData, actor) => rollData.arme?.system.magique && Number(rollData.arme?.system.ecaille_efficacite) > 0,
    getLabel: (rollData, actor) => "Ecaille d'Efficacité: ",
    getValue: (rollData, actor) => rollData.arme?.system.magique ? Math.max(Number(rollData.arme?.system.ecaille_efficacite), 0) : 0,
  },
  tactique: {
    isUsed: (rollData, actor) => rollData.tactique,
    getLabel: (rollData, actor) => RdDBonus.find(rollData.tactique).label,
    getValue: (rollData, actor) => RdDBonus.find(rollData.tactique).attaque,
  },
  finesse: {
    isUsed: (rollData, actor) => RdDBonus.isDefenseAttaqueFinesse(rollData),
    getDescr: (rollData, actor) => 'Attaque particulière en finesse',
  },
  surprise: {
    isUsed: (rollData, actor) => actor.getSurprise(rollData.passeArme),
    getDescr: (rollData, actor) => RdDBonus.find(actor.getSurprise()).label
  },
  attaqueDefenseurSurpris: {
    isUsed: (rollData, actor) => rollData.surpriseDefenseur,
    getLabel: (rollData, actor) => RdDBonus.find(rollData.surpriseDefenseur).label + (rollData.attackerRoll ? '' : ' défenseur'),
    getValue: (rollData, actor) => RdDBonus.find(rollData.surpriseDefenseur).attaque,
  },
  armeParade: {
    isUsed: (rollData, actor) => RdDItemArme.needParadeSignificative(rollData.attackerRoll?.arme, rollData.arme),
    getDescr: (rollData, actor) => rollData.attackerRoll && rollData.arme ? `${RdDItemArme.getNomCategorieParade(rollData.attackerRoll?.arme)} vs ${RdDItemArme.getNomCategorieParade(rollData.arme)}` : ''
  },
  tailleempoignade: {
    isVisible: (rollData, actor) => rollData.isEmpoignade,
    isUsed: (rollData, actor) => rollData.isEmpoignade,
    getLabel: (rollData, actor) => "Malus de taille",
    getValue: (rollData, actor) => rollData.malusTaille,
  },
  
  rituel: {
    isUsed: (rollData, actor) => actor.isPersonnage() && ReglesOptionnelles.isUsing("astrologie") && rollData.selectedSort?.system.isrituel,
    getLabel: (rollData, actor) => 'Astrologique',
    getValue: (rollData, actor) => actor.ajustementAstrologique()
  },
  bonusCase: {
    isUsed: (rollData, actor) => rollData.selectedSort && rollData.tmr.coord,
    getDescr: (rollData, actor) => rollData.selectedSort && rollData.tmr.coord ? `Bonus de case: ${RdDItemSort.getCaseBonus(rollData.selectedSort, rollData.tmr.coord, actor)}%` : ''
  },
  rencontreTMR: {
    isVisible: (rollData, actor) => rollData.tmr && rollData.rencontre?.name,
    isUsed: (rollData, actor) => rollData.tmr && rollData.rencontre?.name,
    getLabel: (rollData, actor) => rollData.rencontre?.name,
    getValue: (rollData, actor) => - (rollData.rencontre?.system.force ?? 0)
  },
}

export class RollDataAjustements {

  /* -------------------------------------------- */
  static calcul(rollData, actor) {
    // s'assurer de la correction des infos rollData
    foundry.utils.mergeObject(rollData, { ajustements: {}, use: {} }, { overwrite: false })

    for (let key in referenceAjustements) {
      const reference = referenceAjustements[key];
      rollData.ajustements[key] = {
        visible: reference.isVisible && reference.isVisible(rollData, actor),
        used: reference.isUsed(rollData, actor),
        label: reference.getLabel && reference.getLabel(rollData, actor),
        value: reference.getValue && reference.getValue(rollData, actor),
        descr: reference.getDescr && reference.getDescr(rollData, actor)
      }
    }
    rollData.finalLevel = RollDataAjustements.sum(rollData.ajustements)
  }

  /* -------------------------------------------- */
  static sum(ajustements) {
    let sum = 0;
    for (var key in ajustements) {
      if (ajustements[key].used && !ajustements[key].descr) {
        sum += parseInt(ajustements[key].value);
      }
    }
    return sum;
  }

  static isIgnoreEtatGeneral(rollData) {
    const selectedCarac = rollData.selectedCarac;
    return !selectedCarac ||
      rollData.ethylisme ||
      RdDCarac.isChance(selectedCarac?.label) ||
      (RdDCarac.isReve(selectedCarac?.label) && !rollData.competence);
  }

}
