import { ChatUtility } from "../chat-utility.js";
import { HIDE_DICE, renderTemplate, SYSTEM_RDD, SYSTEM_SOCKET_ID } from "../constants.js";
import { Misc } from "../misc.js";
import { RdDBonus } from "../rdd-bonus.js";
import { RdDResolutionTable } from "../rdd-resolution-table.js";
import { RdDRoll } from "../rdd-roll.js";
import { RdDRollTables } from "../rdd-rolltables.js";
import { ReglesOptionnelles } from "../settings/regles-optionnelles.js";
import { Targets } from "./targets.js";
import { RdDEmpoignade } from "./rdd-empoignade.js";
import { RdDRollResult } from "../rdd-roll-result.js";
import { EMPOIGNADE, RdDItemArme } from "../item/arme.js";
import { RdDItemCompetence } from "../item-competence.js";
import { RdDInitiative } from "./initiative.mjs";
import RollDialog from "../roll/roll-dialog.mjs";
import { PART_DEFENSE } from "../roll/roll-part-defense.mjs";
import { ROLL_TYPE_ATTAQUE, ROLL_TYPE_DEFENSE } from "../roll/roll-constants.mjs";
import { OptionsAvancees, ROLL_DIALOG_V2 } from "../settings/options-avancees.js";
import { MappingCreatureArme } from "../item/mapping-creature-arme.mjs";
import { RollBasicParts } from "../roll/roll-basic-parts.mjs";
import { Distance } from "./distance.mjs";

/* -------------------------------------------- */
export class PasseArme {

  /* -------------------------------------------- */
  static onSocketMessage(sockmsg) {
    switch (sockmsg.msg) {
      case "msg_encaisser": return PasseArme.onMsgEncaisser(sockmsg.data);
      case "msg_defense": return PasseArme.onMsgDefense(sockmsg.data);
      case "msg_defense_v2": return PasseArme.onMsgDefenseV2(sockmsg.data);
    }
  }

  /* -------------------------------------------- */
  static onUpdateCombat(combat, change, options, userId) {
    if (combat.round != 0 && combat.turns && combat.active) {
      PasseArme.combatNouveauTour(combat);
    }
  }

  /* -------------------------------------------- */
  static combatNouveauTour(combat) {
    if (Misc.isFirstConnectedGM()) {
      let turn = combat.turns.find(t => t.token?.id == combat.current.tokenId);
      if (turn?.actor) {
        // TODO Playaudio for player??
        PasseArme.displayActorCombatStatus(combat, turn.actor, turn.token);
      }
    }
  }

  /* -------------------------------------------- */
  static isActive() {
    return true;
  }

  /* -------------------------------------------- */
  static rddCombatTarget(target, attacker, attackerToken) {
    return new PasseArme(attacker, attackerToken?.id, target?.actor, target?.id, target)
  }

  /* -------------------------------------------- */
  static rddCombatForAttackerAndDefender(attackerId, attackerTokenId, defenderTokenId) {
    const attacker = game.actors.get(attackerId)
    const defenderToken = defenderTokenId ? canvas?.tokens?.get(defenderTokenId) : undefined
    let defender = defenderToken?.actor;
    let target = undefined
    if (!defenderTokenId || !defender) {
      console.warn(`PasseArme.rddCombatForAttackerAndDefender: appel avec defenderTokenId ${defenderTokenId} incorrect, ou pas de defender correspondant`);
      target = Targets.getTarget()
      if (!target) {
        return
      }
      defenderTokenId = target.id;
      defender = target.actor;
      if (!defenderTokenId || !defender) {
        return
      }
    }
    return new PasseArme(attacker, attackerTokenId, defender, defenderTokenId, target)
  }

  static rddCombatForAttackV2(attackerRoll) {
    return PasseArme.rddCombatForDefenseV2(RollBasicParts.prepareDefense(attackerRoll))
  }

  static rddCombatForDefenseV2(defenderRoll) {
    let defenderToken = canvas?.tokens?.get(defenderRoll.active.tokenId)
    if (defenderToken) {
      return PasseArme.rddCombatForAttackerAndDefender(defenderRoll.opponent.id, defenderRoll.opponent.tokenId, defenderRoll.active.tokenId)
    }
  }


  /* -------------------------------------------- */
  static onMsgEncaisser(msg) {
    let defender = canvas?.tokens?.get(msg.defenderToken.id)?.actor;
    if (Misc.isOwnerPlayer(defender)) {
      let attackerRoll = msg.attackerRoll;
      let attacker = msg.attackerId ? game.actors.get(msg.attackerId) : undefined;
      defender.encaisserDommages(attackerRoll.dmg, attacker, msg.attackerToken);
      const rddCombat = PasseArme.rddCombatForAttackerAndDefender(msg.attackerId, msg.attackerToken.id, msg.defenderToken.id);
      rddCombat?.removeChatMessageActionsPasseArme(attackerRoll.passeArme);
    }
  }

  /* -------------------------------------------- */
  static onMsgDefense(msg) {
    let defenderToken = canvas?.tokens?.get(msg.defenderToken.id)
    if (defenderToken && Misc.isFirstConnectedGM()) {
      const rddCombat = PasseArme.rddCombatForAttackerAndDefender(msg.attackerId, msg.attackerToken.id, msg.defenderToken.id)
      rddCombat?.removeChatMessageActionsPasseArme(msg.paramChatDefense.attackerRoll.passeArme)
      if (msg.defenderRoll.v2) {/* TODO: delete roll V1 */
        RollDialog.loadRollData(msg.paramChatDefense)
        rddCombat?._chatMessageDefenseV2(msg.paramChatDefense)
      } else {
        rddCombat?._chatMessageDefense(msg.paramChatDefense, msg.defenderRoll)
      }
    }
  }

  /* -------------------------------------------- */
  static _callJetDeVie(event) {
    let actorId = event.currentTarget.attributes['data-actorId'].value;
    let tokenId = event.currentTarget.attributes['data-tokenId'].value;
    let token = canvas?.tokens?.get(tokenId)
    const actor = token?.actor ?? game.actors.get(actorId);
    if (actor?.isOwner) {
      actor.jetDeVie();
    }
  }

  /* -------------------------------------------- */
  static registerChatCallbacks(html) {
    for (let button of [
      '.button-parade',
      '.button-esquive',
      '.button-encaisser',
      '.particuliere-attaque',
      '.appel-chance-defense',
      '.appel-destinee-defense',
      '.appel-chance-attaque',
      '.appel-destinee-attaque',
      '.echec-total-attaque',
      // '.appel-chance',
      // '.chat-encaissement',
      // '.resister-recul',
    ]) {
      $(html).on("click", button, event => {
        const rddCombat = PasseArme.rddCombatForAttackerAndDefender(
          event.currentTarget.attributes['data-attackerId']?.value,
          event.currentTarget.attributes['data-attackerTokenId']?.value,
          event.currentTarget.attributes['data-defenderTokenId']?.value);
        if (rddCombat) {
          rddCombat.onEvent(button, event);
          event.preventDefault();
        }
      });
    }
    $(html).on("click", 'a.chat-jet-vie', event => {
      event.preventDefault();
      PasseArme._callJetDeVie(event);
    });

  }

  /* -------------------------------------------- */
  constructor(attacker, attackerTokenId, defender, defenderTokenId, target) {
    this.attacker = attacker
    this.defender = defender
    this.target = target
    this.attackerId = this.attacker.id
    this.defenderId = this.defender.id
    this.attackerTokenId = attackerTokenId
    this.defenderTokenId = defenderTokenId
    this.attackerToken = Targets.getTokenData(attacker, attackerTokenId)
    this.defenderToken = Targets.getTokenData(defender, defenderTokenId, target)
  }

  /* -------------------------------------------- */
  async onEvent(button, event) {
    const chatMessage = ChatUtility.getChatMessage(event);
    const defenderRoll = ChatUtility.getMessageData(chatMessage, 'rollData');
    const attackerRoll = defenderRoll?.attackerRoll ?? ChatUtility.getMessageData(chatMessage, 'rollData');
    console.log('PasseArme', attackerRoll, defenderRoll);

    const armeParadeId = event.currentTarget.attributes['data-armeid']?.value;
    const competence = event.currentTarget.attributes['data-competence']?.value;
    const compId = event.currentTarget.attributes['data-compid']?.value;

    switch (button) {
      case '.particuliere-attaque': return await this.choixParticuliere(attackerRoll, event.currentTarget.attributes['data-mode'].value);
      case '.button-parade': return this.parade(attackerRoll, armeParadeId);
      case '.button-esquive': return this.esquive(attackerRoll, compId, competence);
      case '.button-encaisser': return this.encaisser(attackerRoll, defenderRoll);
      case '.echec-total-attaque': return this._onEchecTotal(attackerRoll);

      case '.appel-chance-attaque': return this.attacker.rollAppelChance(
        () => this.attaqueChanceuse(attackerRoll),
        () => this._onEchecTotal(attackerRoll));
      case '.appel-chance-defense': return this.defender.rollAppelChance(
        () => this.defenseChanceuse(attackerRoll, defenderRoll),
        () => this.afficherOptionsDefense(attackerRoll, defenderRoll, { defenseChance: true }));
      case '.appel-destinee-attaque': return this.attacker.appelDestinee(
        () => this.attaqueSignificative(attackerRoll));
      case '.appel-destinee-defense': return this.defender.appelDestinee(
        () => this.defenseDestinee(defenderRoll));
    }
  }

  /* -------------------------------------------- */
  attaqueChanceuse(attackerRoll) {
    ui.notifications.info("L'attaque est rejouée grâce à la chance")
    attackerRoll.essais.attaqueChance = true
    this.attaque(attackerRoll, attackerRoll.arme)
  }

  /* -------------------------------------------- */
  attaqueDestinee(attackerRoll) {
    ui.notifications.info('Attaque significative grâce à la destinée')
    RdDResolutionTable.significativeRequise(attackerRoll.rolled);
    this.removeChatMessageActionsPasseArme(attackerRoll.passeArme);
    this._onAttaqueNormale(attackerRoll);
  }

  /* -------------------------------------------- */
  defenseChanceuse(attackerRoll, defenderRoll) {
    ui.notifications.info("La défense est rejouée grâce à la chance")
    attackerRoll.essais.defenseChance = true;
    attackerRoll.essais.defense = false;
    this.removeChatMessageActionsPasseArme(attackerRoll.passeArme);
    this._sendMessageDefense(attackerRoll, defenderRoll, attackerRoll.essais);
  }

  /* -------------------------------------------- */
  defenseDestinee(defenderRoll) {
    if (defenderRoll) {
      ui.notifications.info('Défense significative grâce à la destinée')
      RdDResolutionTable.significativeRequise(defenderRoll.rolled);
      this.removeChatMessageActionsPasseArme(defenderRoll.passeArme);
      if (defenderRoll.arme) {
        this._onParadeNormale(defenderRoll);
      }
      else {
        this._onEsquiveNormale(defenderRoll);
      }
    }
    else {
      ui.notifications.warn("Appel à la destinée impossible, la passe d'armes est déjà terminée!")
    }
  }

  /* -------------------------------------------- */
  afficherOptionsDefense(attackerRoll, defenderRoll, essais) {
    ui.notifications.info("La chance n'est pas avec vous");
    this._sendMessageDefense(attackerRoll, defenderRoll, essais);
  }

  /* -------------------------------------------- */
  removeChatMessageActionsPasseArme(passeArme) {
    if (game.settings.get(SYSTEM_RDD, "supprimer-dialogues-combat-chat")) {
      ChatUtility.removeChatMessageContaining(`<div data-passearme="${passeArme}">`);
    }
  }

  /* -------------------------------------------- */
  static isEchec(rollData) {
    switch (rollData.ajustements.surprise.used) {
      case 'totale': return true;
      case 'demi': return !rollData.rolled.isSign;
    }
    return rollData.rolled.isEchec;
  }

  /* -------------------------------------------- */
  static isEchecTotal(rollData) {
    if (rollData.v2 /* roll V2*/) {
      // TODO: en cas de demi-surprise à l'attaque, tout échec est un echec total.
      // TODO: en cas de demi-surprise en défense, pas de changement à la règle de base
      return rollData.rolled.isETotal
    }
    if (rollData.mode == ROLL_TYPE_ATTAQUE && rollData.surprise == 'demi') {
      // échec normal à l'attaque en demi surprise
      return rollData.rolled.isEchec && rollData.rolled.code != 'notSign'
    }
    return rollData.rolled.isETotal
  }

  /* -------------------------------------------- */
  static isParticuliere(rollData) {
    if (rollData.v2 /* roll V2*/) {
      return rollData.rolled.isPart
    }
    if (rollData.attackerRoll || !rollData.ajustements.surprise.used) {
      return rollData.rolled.isPart
    }
    return false
  }

  /* -------------------------------------------- */
  static isReussite(rollData) {
    if (rollData.v2 /* roll V2*/) {
      return rollData.rolled.isSuccess
    }
    if (!rollData.ajustements.surprise.used) {
      return rollData.rolled.isSuccess
    }
    switch (rollData.ajustements.surprise.used) {
      case 'totale': return false
      case 'demi': return rollData.rolled.isSign
    }
    return rollData.rolled.isSuccess
  }

  /* -------------------------------------------- */
  async proposerAjustementTirLancer(rollData) {
    if (['tir', 'lancer'].includes(rollData.competence.system.categorie)) {
      if (this.defender.isEntiteBlurette()) {
        await ChatMessage.create({
          content: `<strong>La cible est une blurette, l'arme à distance sera perdue dans le blurêve`,
          whisper: ChatUtility.getGMs()
        })
      }
      else {
        const defenderToken = canvas?.tokens?.get(this.defenderTokenId)
        const info = foundry.utils.mergeObject(
          {
            rollData: rollData,
            attacker: _token,
            defender: defenderToken,
          },
          Distance.ajustements(_token, defenderToken, { arme: rollData.arme, main: rollData.competence.system.categorie })
        )
        await ChatMessage.create({
          content: await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-info-distance.hbs', info),
          whisper: ChatUtility.getGMs()
        })
      }
    }
  }

  async attaqueV2(options = undefined) {
    if (!await this.attacker.accorder(this.defender, 'avant-attaque')) {
      return
    }
    const rollData = {
      ids: {
        actorId: this.attackerId,
        actorTokenId: this.attackerTokenId,
        opponentId: this.defender.id,
        opponentTokenId: this.defenderTokenId,
      },
      type: { allowed: [ROLL_TYPE_ATTAQUE], current: ROLL_TYPE_ATTAQUE },
      selected: {},
      passeArme: foundry.utils.randomID(16),
    }
    if (options) {
      rollData.selected = {
        attaque: {
          comp: { id: options.comp.id },
          arme: { id: options.arme.id },
          main: options.main
        }
      }
    }
    await this.doRollAttaque(rollData)
  }

  async doRollAttaque(rollData, callbacks = []) {
    return await RollDialog.create(rollData, {
      onRollDone: RollDialog.onRollDoneClose,
      callbacks: [
        async (roll) => await this.onAttaqueV2(roll),
        ...callbacks
      ]
    })
  }

  async onAttaqueV2(attackerRoll) {
    if (!this.defender || !attackerRoll.rolled.isSuccess || attackerRoll.particulieres?.length > 1) {
      return
    }
    if (!await this.attacker.accorder(this.defender, 'avant-defense')) {
      return;
    }

    RollDialog.loadRollData(attackerRoll)

    const surpriseDefender = this.defender.getSurprise(true);
    const paramChatDefense = {
      attackerRoll: attackerRoll,
      isPossession: this.isPossession(attackerRoll),
      defender: this.defender,
      attacker: this.attacker,
      attackerId: this.attackerId,
      attackerToken: this.attackerToken,
      defenderToken: this.defenderToken,
      surprise: surpriseDefender,
    }

    if (Misc.isFirstConnectedGM()) {
      await this._chatMessageDefenseV2(paramChatDefense);
    }
    else {
      this._socketSendMessageDefenseV2(paramChatDefense);
    }
  }

  _socketSendMessageDefenseV2(paramChatDefense) {
    game.socket.emit(SYSTEM_SOCKET_ID, { msg: "msg_defense_v2", data: { paramChatDefense } })
  }

  static async onMsgDefenseV2(msg) {
    if (Misc.isFirstConnectedGM()) {
      const paramChatDefense = msg.paramChatDefense
      RollBasicParts.restore(paramChatDefense.attackerRoll)
      const rddCombat = PasseArme.rddCombatForAttackV2(paramChatDefense.attackerRoll)
      rddCombat?.removeChatMessageActionsPasseArme(paramChatDefense.attackerRoll.passeArme)
      await rddCombat?._chatMessageDefenseV2(paramChatDefense)
    }
  }

  async _chatMessageDefenseV2(paramDemandeDefense) {
    const attackerRoll = paramDemandeDefense.attackerRoll
    RollBasicParts.loadSurprises(attackerRoll)
    attackerRoll.dmg = RdDBonus.dmgRollV2(attackerRoll, attackerRoll.current.attaque)

    const defenseData = RollBasicParts.prepareDefense(attackerRoll)

    const choixDefense = await ChatMessage.create({
      // message privé: du défenseur à lui même (et aux GMs)
      speaker: ChatMessage.getSpeaker({ actor: this.defender, token: canvas?.tokens?.get(this.defenderTokenId) }),
      alias: this.attacker?.getAlias(),
      whisper: ChatUtility.getOwners(this.defender),
      content: await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-demande-defense.hbs', defenseData)
    });
    // flag pour garder les jets d'attaque/defense
    ChatUtility.setMessageData(choixDefense, 'demande-defense', true)
    ChatUtility.setMessageData(choixDefense, 'rollData', {
      ids: defenseData.ids,
      attackerRoll: RollDialog.saveParts(attackerRoll),
      passeArme: defenseData.passeArme
    })
  }

  /* -------------------------------------------- */
  async attaque(competence, arme, main) {
    if (!await this.attacker.accorder(this.defender, 'avant-attaque')) {
      return
    }
    if (OptionsAvancees.isUsing(ROLL_DIALOG_V2)) {
      return this.attaqueV2(
        { comp: competence, arme: arme, main: main }
      )
    }
    if (arme.system.cac == EMPOIGNADE) {
      RdDEmpoignade.onAttaqueEmpoignade(this.attacker, this.defender)
      return
    }
    RdDEmpoignade.checkEmpoignadeEnCours(this.attacker)

    let rollData = this._prepareAttaque(competence, arme)
    console.log("PasseArme.attaque >>>", rollData)
    if (arme) {
      await this.attacker.verifierForceMin(arme);
    }
    await this.proposerAjustementTirLancer(rollData)

    const dialog = await RdDRoll.create(this.attacker, rollData,
      { html: 'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-competence.hbs' },
      {
        name: 'jet-attaque',
        label: 'Attaque: ' + (arme?.name ?? competence.name),
        callbacks: [
          this.attacker.createCallbackExperience(),
          this.attacker.createCallbackAppelAuMoral(),
          { action: r => this.removeChatMessageActionsPasseArme(r.passeArme) },
          { action: async r => await this.attacker.incDecItemUse(arme.id, arme && !PasseArme.isParticuliere(r)) },
          { action: r => this._onAttaque(r) },
        ]
      });
    dialog.render(true);
  }

  /* -------------------------------------------- */
  _prepareAttaque(competence, arme) {
    let rollData = {
      mode: 'attaque',
      alias: this.attacker?.getAlias(),
      passeArme: foundry.utils.randomID(16),
      mortalite: arme?.system.mortalite,
      competence: competence,
      surprise: this.attacker.getSurprise(true),
      surpriseDefenseur: this.defender.getSurprise(true),
      sourceToken: this.attackerToken,
      targetToken: this.defenderToken,
      essais: {}
    };

    if (this.attacker.isCreatureOuEntite()) {
      MappingCreatureArme.setRollDataCreature(rollData);
    }
    else if (arme) {
      // Usual competence
      rollData.arme = RdDItemArme.armeUneOuDeuxMains(arme, RdDItemCompetence.isArmeUneMain(competence));
    }
    else {
      // sans armes: à mains nues
      rollData.arme = RdDItemArme.pugilat(this.attacker)
      rollData.arme.system.niveau = competence.system.niveau
      rollData.arme.system.initiative = RdDInitiative.getRollInitiative(this.attacker.system.carac['melee'].value, competence.system.niveau);
    }
    return rollData;
  }

  async _onAttaque(attackerRoll) {
    if (PasseArme.isParticuliere(attackerRoll)) {
      return await this._onAttaqueParticuliere(attackerRoll)
    }
    if (PasseArme.isReussite(attackerRoll)) {
      return await this._onAttaqueNormale(attackerRoll)
    }
    if (PasseArme.isEchecTotal(attackerRoll)) {
      return await this._onAttaqueEchecTotal(attackerRoll)
    }
    return await this._onAttaqueEchec(attackerRoll)
  }
  /* -------------------------------------------- */
  async _onAttaqueParticuliere(rollData) {
    const isMeleeDiffNegative = (rollData.competence.type == 'competencecreature' || rollData.selectedCarac.label == "Mêlée") && rollData.diffLibre < 0;
    // force toujours, sauf empoignade
    // finesse seulement en mélée, pour l'empoignade, ou si la difficulté libre est de -1 minimum
    // rapidité seulement en mêlée, si l'arme le permet, et si la difficulté libre est de -1 minimum
    const isForce = !rollData.arme.system.empoignade;
    const isFinesse = rollData.tactique != 'charge' && (rollData.arme.system.empoignade || isMeleeDiffNegative);
    const isRapide = rollData.tactique != 'charge' && !rollData.arme.system.empoignade && isMeleeDiffNegative && rollData.arme.system.rapide;
    // si un seul choix possible, le prendre
    if (isForce && !isFinesse && !isRapide) {
      return await this.choixParticuliere(rollData, "force");
    }
    else if (!isForce && isFinesse && !isRapide) {
      return await this.choixParticuliere(rollData, "finesse");
    }
    else if (!isForce && !isFinesse && isRapide) {
      return await this.choixParticuliere(rollData, "rapidite");
    }

    const choixParticuliere = await ChatMessage.create({
      alias: this.attacker.getAlias(),
      whisper: ChatUtility.getOwners(this.attacker),
      content: await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-demande-attaque-particuliere.hbs', {
        alias: this.attacker.getAlias(),
        attackerId: this.attackerId,
        attackerToken: this.attackerToken,
        defenderToken: this.defenderToken,
        isForce: isForce,
        isFinesse: isFinesse,
        isRapide: isRapide,
        passeArme: rollData.passeArme
      })
    });
    ChatUtility.setMessageData(choixParticuliere, 'rollData', rollData);
  }

  /* -------------------------------------------- */
  async _onAttaqueNormale(attackerRoll) {
    console.log("PasseArme.onAttaqueNormale >>>", attackerRoll);

    attackerRoll.dmg = RdDBonus.dmg(attackerRoll, this.attacker);
    let defenderRoll = { attackerRoll: attackerRoll, passeArme: attackerRoll.passeArme, show: {} }
    attackerRoll.show = {
      cible: this.defender?.getAlias() ?? 'la cible',
      isRecul: (attackerRoll.particuliere == 'force' || attackerRoll.tactique == 'charge')
    }
    await RdDRollResult.displayRollData(attackerRoll, this.attacker, 'chat-resultat-attaque.hbs');

    if (!await this.attacker.accorder(this.defender, 'avant-defense')) {
      return;
    }

    if (this.defender) {
      await this._sendMessageDefense(attackerRoll, defenderRoll);
    }
  }

  /* -------------------------------------------- */
  isPossession(attackerRoll) {
    const carac = attackerRoll.v2
      ? attackerRoll.current.carac?.label
      : attackerRoll.selectedCarac.label
    return carac?.toLowerCase() == 'possession';
  }

  /* -------------------------------------------- */
  async _sendMessageDefense(attackerRoll, defenderRoll, essaisPrecedents = undefined) {
    this.removeChatMessageActionsPasseArme(attackerRoll.passeArme);
    if (essaisPrecedents) {
      foundry.utils.mergeObject(attackerRoll.essais, essaisPrecedents, { overwrite: true });
    }

    // # utilisation esquive
    const corpsACorps = this.defender.getCompetenceCorpsACorps({ onMessage: it => console.info(it, this.defender) });
    const esquives = foundry.utils.deepClone(this.defender.getCompetencesEsquive())
    esquives.forEach(e => e.nbUsage = e?._id ? this.defender.getItemUse(e._id) : 0);

    const paramChatDefense = {
      passeArme: attackerRoll.passeArme,
      essais: attackerRoll.essais,
      isPossession: this.isPossession(attackerRoll),
      defender: this.defender,
      attacker: this.attacker,
      attackerId: this.attackerId,
      esquives: esquives,
      attackerToken: this.attackerToken,
      defenderToken: this.defenderToken,
      mainsNues: attackerRoll.dmg.mortalite != 'mortel' && corpsACorps,
      armes: this._filterArmesParade(this.defender, attackerRoll.competence, attackerRoll.arme),
      diffLibre: attackerRoll.ajustements?.diffLibre?.value ?? 0,
      attaqueParticuliere: attackerRoll.particuliere,
      attaqueCategorie: attackerRoll.competence.system.categorie,
      attaqueArme: attackerRoll.arme,
      surprise: this.defender.getSurprise(true),
      dmg: attackerRoll.dmg,
    };

    if (Misc.isFirstConnectedGM()) {
      await this._chatMessageDefense(paramChatDefense, defenderRoll);
    }
    else {
      this._socketSendMessageDefense(paramChatDefense, defenderRoll);
    }
  }

  /* -------------------------------------------- */
  async _chatMessageDefense(paramDemandeDefense, defenderRoll) {
    const choixDefense = await ChatMessage.create({
      // message privé: du défenseur à lui même (et aux GMs)
      speaker: ChatMessage.getSpeaker({ actor: this.defender, token: canvas?.tokens?.get(this.defenderTokenId) }),
      alias: this.attacker?.getAlias(),
      whisper: ChatUtility.getOwners(this.defender),
      content: await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-demande-defense-v1.hbs', paramDemandeDefense),
    });
    // flag pour garder les jets d'attaque/defense
    ChatUtility.setMessageData(choixDefense, 'rollData', defenderRoll);
  }

  /* -------------------------------------------- */
  _socketSendMessageDefense(paramChatDefense, defenderRoll) {
    // envoyer le message au destinataire
    game.socket.emit(SYSTEM_SOCKET_ID, {
      msg: "msg_defense", data: {
        attackerId: this.attacker?.id,
        attackerToken: this.attackerToken,
        defenderId: this.defender?.id,
        defenderToken: this.defenderToken,
        defenderRoll: defenderRoll,
        paramChatDefense: paramChatDefense,
        rollMode: true,
      }
    });
  }

  /* -------------------------------------------- */
  _filterArmesParade(defender, competence, armeAttaque) {
    let defenses = defender.items.filter(it => it.isParade())
    defenses = foundry.utils.deepClone(defenses)
    defenses.forEach(armeDefense => {
      armeDefense.nbUsage = defender.getItemUse(armeDefense._id)
      armeDefense.typeParade = RdDItemArme.defenseArmeParade(armeAttaque, armeDefense)
    })

    switch (competence.system.categorie) {
      case 'tir':
      case 'lancer':
        return defenses.filter(armeDefense => RdDItemArme.getCategorieParade(armeDefense) == 'boucliers')
      default:
        return defenses.filter(armeDefense => armeDefense.typeParade != '')
    }
  }

  /* -------------------------------------------- */
  async _onAttaqueEchecTotal(attackerRoll) {
    const choixEchecTotal = await ChatMessage.create({
      whisper: ChatUtility.getOwners(this.attacker),
      content: await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/chat-demande-attaque-etotal.hbs', {
        rolled: attackerRoll.rolled,
        attackerId: this.attackerId,
        attacker: this.attacker,
        attackerToken: this.attackerToken,
        defenderToken: this.defenderToken,
        essais: attackerRoll.essais
      })
    });
    ChatUtility.setMessageData(choixEchecTotal, 'rollData', attackerRoll);
  }

  /* -------------------------------------------- */
  async _onEchecTotal(rollData) {
    console.log("PasseArme._onEchecTotal >>>", rollData);

    const arme = rollData.arme;
    const avecArme = !['', 'sans-armes', 'armes-naturelles'].includes(arme?.system.categorie_parade ?? '');
    const action = (rollData.attackerRoll ? (arme ? "la parade" : "l'esquive") : "l'attaque");
    await ChatMessage.create(ChatUtility.adaptVisibility(
      { content: `<strong>Maladresse à ${action}!</strong> ` + await RdDRollTables.getMaladresse({ arme: avecArme }) },
      { actor: this.defender }))
  }

  /* -------------------------------------------- */
  async _onAttaqueEchec(attackerRoll) {
    console.log("PasseArme.onAttaqueEchec >>>", attackerRoll);
    await RdDRollResult.displayRollData(attackerRoll, this.attacker, 'chat-resultat-attaque.hbs');
  }

  /* -------------------------------------------- */
  async choixParticuliere(rollData, choix) {
    console.log("PasseArme.choixParticuliere >>>", rollData, choix);

    await this.attacker.incDecItemUse(rollData.arme.id, choix != "rapidite")

    this.removeChatMessageActionsPasseArme(rollData.passeArme);
    rollData.particuliere = choix;
    await this._onAttaqueNormale(rollData)
  }

  /* -------------------------------------------- */
  async parade(attackerRoll, armeParadeId) {
    const arme = this.defender.getArmeParade(armeParadeId);
    console.log("PasseArme.parade >>>", attackerRoll, armeParadeId, arme);
    const competence = arme?.system?.competence;
    if (competence == undefined) {
      console.error("Pas de compétence de parade associée à ", arme?.name, armeParadeId);
      return;
    }

    let rollData = this._prepareParade(attackerRoll, arme, competence);

    const dialog = await RdDRoll.create(this.defender, rollData,
      { html: 'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-competence.hbs' },
      {
        name: 'jet-parade',
        label: 'Parade: ' + (arme ? arme.name : rollData.competence.name),
        callbacks: [
          this.defender.createCallbackExperience(),
          this.defender.createCallbackAppelAuMoral(),
          { action: r => this.removeChatMessageActionsPasseArme(r.passeArme) },
          { action: async r => await this.defender.incDecItemUse(armeParadeId, !PasseArme.isParticuliere(r)) },
          { action: r => this._onParade(r) },
        ]
      });
    dialog.render(true);
  }

  async defenseV2(attackerRoll, callbacks = []) {

    await this.doRollDefense({
      ids: {
        actorId: this.defender.id,
        actorTokenId: this.defenderTokenId,
        opponentTokenId: this.attackerTokenId,
        opponentId: this.attackerId,
      },
      type: { allowed: [ROLL_TYPE_DEFENSE], current: ROLL_TYPE_DEFENSE },
      attackerRoll: attackerRoll,
      passeArme: attackerRoll.passeArme,
    }, callbacks)
  }

  async doRollDefense(rollData, callbacks = []) {
    return await RollDialog.create(rollData, {
      onRollDone: RollDialog.onRollDoneClose,
      callbacks: [
        async (roll) => {
          this.removeChatMessageActionsPasseArme(roll.passeArme);
          // defense:  esquive / arme de parade / competence de défense
          if (!PasseArme.isParticuliere(roll)) {
            await roll.active.actor.incDecItemUse(roll.current[PART_DEFENSE].defense?.id);
          }
          await this._onDefense(roll);
        },
        ...callbacks
      ]
    })
  }

  /* -------------------------------------------- */
  _prepareParade(attackerRoll, armeParade, competenceParade) {
    let defenderRoll = {
      mode: ROLL_TYPE_DEFENSE,
      alias: this.defender?.getAlias(),
      passeArme: attackerRoll.passeArme,
      diffLibre: attackerRoll.diffLibre,
      attackerToken: this.attackerToken,
      defenderToken: this.defenderToken,
      attackerRoll: attackerRoll,
      competence: this.defender.getCompetence(competenceParade),
      arme: armeParade,
      surprise: this.defender.getSurprise(true),
      needParadeSignificative: ReglesOptionnelles.isUsing('categorieParade') && RdDItemArme.needParadeSignificative(attackerRoll.arme, armeParade),
      needResist: RdDItemArme.needArmeResist(attackerRoll.arme, armeParade),
      carac: this.defender.system.carac,
      show: {}
    };

    if (this.defender.isCreatureOuEntite()) {
      MappingCreatureArme.setRollDataCreature(defenderRoll);
    }

    return defenderRoll;
  }

  async _onDefense(rollData) {
    const isEsquive = rollData.current[PART_DEFENSE].isEsquive
    const isParade = !isEsquive
    if (PasseArme.isReussite(rollData)) {
      if (isParade) {
        await this.computeDeteriorationArme(rollData)
        if (PasseArme.isParticuliere(rollData)) {
          await this.infoAttaquantDesarme(rollData)
        }
      }

    }
    this.removeChatMessageActionsPasseArme(rollData.passeArme)
  }

  async infoAttaquantDesarme(rollData) {
    if (/*TODO: parade?*/!rollData.attackerRoll?.particuliere) {
      // TODO: attaquant doit jouer résistance et peut être désarmé p132
      await ChatMessage.create(ChatUtility.adaptVisibility(
        { content: `(à gérer) L'attaquant doit jouer résistance et peut être désarmé (p132)` },
        { actor: this.defender }))
    }
  }

  async _onDefenseNormale(rollData) {
    console.log("PasseArme._onDefenseNormale >>>", rollData);
    await this.computeRecul(rollData);
    await this.computeDeteriorationArme(rollData);
    await RdDRollResult.displayRollData(rollData, this.defender, 'chat-resultat-parade.hbs');
    this.removeChatMessageActionsPasseArme(rollData.passeArme);
  }





  async _onParade(defenderRoll) {
    if (PasseArme.isReussite(defenderRoll)) {
      await this._onParadeNormale(defenderRoll)
      if (PasseArme.isParticuliere(defenderRoll)) {
        await this._onParadeParticuliere(defenderRoll)
      }
      return
    }
    await this._onParadeEchec(defenderRoll)
  }

  /* -------------------------------------------- */
  async _onParadeParticuliere(defenderRoll) {
    console.log("PasseArme._onParadeParticuliere >>>", defenderRoll);
    if (!defenderRoll.attackerRoll.isPart) {
      // TODO: attaquant doit jouer résistance et peut être désarmé p132
      await ChatMessage.create(ChatUtility.adaptVisibility(
        { content: `(à gérer) L'attaquant doit jouer résistance et peut être désarmé (p132)` },
        { actor: this.defender }))
    }
  }
  /* -------------------------------------------- */
  async _onParadeNormale(defenderRoll) {
    console.log("PasseArme._onParadeNormale >>>", defenderRoll);

    await this.computeRecul(defenderRoll);
    await this.computeDeteriorationArme(defenderRoll);
    await RdDRollResult.displayRollData(defenderRoll, this.defender, 'chat-resultat-parade.hbs');
    this.removeChatMessageActionsPasseArme(defenderRoll.passeArme);
  }

  /* -------------------------------------------- */
  async _onParadeEchec(defenderRoll) {
    console.log("PasseArme._onParadeEchec >>>", defenderRoll);

    await RdDRollResult.displayRollData(defenderRoll, this.defender, 'chat-resultat-parade.hbs');

    this.removeChatMessageActionsPasseArme(defenderRoll.passeArme);
    this._sendMessageDefense(defenderRoll.attackerRoll, defenderRoll, { defense: true });
  }

  /* -------------------------------------------- */
  async esquive(attackerRoll, compId, compName) {
    const esquive = this.defender.getCompetence(compId) ?? this.defender.getCompetence(compName)
    if (esquive == undefined) {
      ui.notifications.error(this.defender.getAlias() + " n'a pas de compétence " + compName);
      return;
    }
    console.log("PasseArme.esquive >>>", attackerRoll, esquive);
    let rollData = this._prepareEsquive(attackerRoll, esquive);

    const dialog = await RdDRoll.create(this.defender, rollData,
      { html: 'systems/foundryvtt-reve-de-dragon/templates/dialog-roll-competence.hbs' },
      {
        name: 'jet-esquive',
        label: 'Esquiver',
        callbacks: [
          this.defender.createCallbackExperience(),
          this.defender.createCallbackAppelAuMoral(),
          { action: async r => await this.defender.incDecItemUse(esquive.id, !PasseArme.isParticuliere(r)) },
          { action: r => this.removeChatMessageActionsPasseArme(r.passeArme) },
          { action: r => this._onEsquive(r) },
        ]
      });
    dialog.render(true);
  }

  /* -------------------------------------------- */
  _prepareEsquive(attackerRoll, competence) {
    let rollData = {
      mode: ROLL_TYPE_DEFENSE,
      alias: this.defender.getAlias(),
      passeArme: attackerRoll.passeArme,
      diffLibre: attackerRoll.diffLibre,
      attackerToken: this.attackerToken,
      defenderToken: this.defenderToken,
      attackerRoll: attackerRoll,
      competence: competence,
      surprise: this.defender.getSurprise(true),
      surpriseDefenseur: this.defender.getSurprise(true),
      carac: this.defender.system.carac,
      show: {}
    };

    if (this.defender.isCreatureOuEntite()) {
      MappingCreatureArme.setRollDataCreature(rollData);
    }
    return rollData;
  }

  async _onEsquive(defenderRoll) {
    if (PasseArme.isReussite(defenderRoll)) {
      await this._onEsquiveNormale(defenderRoll)
      if (PasseArme.isParticuliere(defenderRoll)) {
        await this._onEsquiveParticuliere(defenderRoll)
      }
      return
    }
    return await this._onEsquiveEchec(defenderRoll)
  }
  /* -------------------------------------------- */
  async _onEsquiveParticuliere(defenderRoll) {
    console.log("PasseArme._onEsquiveParticuliere >>>", defenderRoll);
    await ChatMessage.create(ChatUtility.adaptVisibility(
      { content: "<strong>Vous pouvez esquiver une deuxième fois!</strong>" },
      { actor: this.defender }))
  }

  /* -------------------------------------------- */
  async _onEsquiveNormale(defenderRoll) {
    console.log("PasseArme._onEsquiveNormal >>>", defenderRoll);
    await RdDRollResult.displayRollData(defenderRoll, this.defender, 'chat-resultat-esquive.hbs');
    this.removeChatMessageActionsPasseArme(defenderRoll.passeArme);
  }

  /* -------------------------------------------- */
  async _onEsquiveEchec(defenderRoll) {
    console.log("PasseArme._onEsquiveEchec >>>", defenderRoll);

    await RdDRollResult.displayRollData(defenderRoll, this.defender, 'chat-resultat-esquive.hbs');

    this.removeChatMessageActionsPasseArme(defenderRoll.passeArme);
    this._sendMessageDefense(defenderRoll.attackerRoll, defenderRoll, { defense: true })
  }

  /* -------------------------------------------- */
  async computeDeteriorationArme(defenderRoll) {
    if (!ReglesOptionnelles.isUsing('resistanceArmeParade')) {
      return;
    }
    const attackerRoll = defenderRoll.attackerRoll;
    // Est-ce une parade normale?
    if (defenderRoll.arme && attackerRoll && !defenderRoll.rolled.isPart) {
      // Est-ce que l'attaque est une particulière en force ou une charge
      if (defenderRoll.needResist || this._isForceOuCharge(attackerRoll, defenderRoll.v2)) {

        defenderRoll.show = defenderRoll.show || {}

        const dmg = attackerRoll.dmg.dmgArme + attackerRoll.dmg.dmgActor;
        let arme = defenderRoll.arme;
        let resistance = Misc.toInt(arme.system.resistance);
        if (arme.system.magique) {
          defenderRoll.show.deteriorationArme = 'resiste'; // Par défaut
          if (arme.system.resistance_magique == undefined) arme.system.resistance_magique = 0; // Quick fix
          if (dmg > arme.system.resistance_magique) { // Jet uniquement si dommages supérieur à résistance magique (cf. 274)
            // Jet de résistance de l'arme de parade (p.132)
            let resistRoll = await RdDResolutionTable.rollData({
              caracValue: resistance,
              finalLevel: - dmg,
              showDice: HIDE_DICE
            });
            if (!resistRoll.rolled.isSuccess) {
              let perteResistance = (dmg - arme.system.resistance_magique)
              resistance -= perteResistance;
              defenderRoll.show.deteriorationArme = resistance <= 0 ? 'brise' : 'perte';
              defenderRoll.show.perteResistance = perteResistance;
              this.defender.updateEmbeddedDocuments('Item', [{ _id: defenderRoll.arme.id, 'system.resistance': resistance }]);
            }
          }
        } else {
          // Jet de résistance de l'arme de parade (p.132)
          let resistRoll = await RdDResolutionTable.rollData({
            caracValue: resistance,
            finalLevel: - dmg,
            showDice: HIDE_DICE
          });
          if (resistRoll.rolled.isSuccess) { // Perte de résistance
            defenderRoll.show.deteriorationArme = 'resiste';
          } else {
            resistance -= dmg;
            defenderRoll.show.deteriorationArme = resistance <= 0 ? 'brise' : 'perte';
            defenderRoll.show.perteResistance = dmg;
            this.defender.updateEmbeddedDocuments('Item', [{ _id: defenderRoll.arme.id, 'system.resistance': resistance }]);
          }
        }
        // Si l'arme de parade n'est pas un bouclier, jet de désarmement (p.132)
        if (ReglesOptionnelles.isUsing('defenseurDesarme') && resistance > 0 && RdDItemArme.getCategorieParade(defenderRoll.arme) != 'boucliers') {
          let desarme = await RdDResolutionTable.rollData({
            caracValue: this.defender.getForce(),
            finalLevel: Misc.toInt(defenderRoll.competence.system.niveau) - dmg,
            showDice: HIDE_DICE
          });
          defenderRoll.show.desarme = desarme.rolled.isEchec
        }
      }
    }
  }

  /* -------------------------------------------- */
  async computeRecul(defenderRoll) { // Calcul du recul (p. 132)
    if (!ReglesOptionnelles.isUsing('recul')) {
      return
    }
    const attackerRoll = defenderRoll.attackerRoll;
    if (this._isForceOuCharge(attackerRoll, defenderRoll.v2)) {
      defenderRoll.show.recul = this.defender.encaisserRecul(this.attacker.getForce(), attackerRoll.dmg.dmgArme)
    }
  }

  _isForceOuCharge(attaque, isRollV2 = false /* TODO: delete roll V1 */) {
    return attaque.particuliere == 'force' || 'charge' == (isRollV2 ? attaque.tactique?.key : attaque.tactique)
  }


  /* -------------------------------------------- */
  async encaisser(attackerRoll, defenderRoll) {
    console.log("PasseArme.encaisser >>>", attackerRoll, defenderRoll);

    if (defenderRoll?.rolled && PasseArme.isEchecTotal(defenderRoll)) {
      this._onEchecTotal(defenderRoll);
    }

    await this.doRollEncaissement(attackerRoll, defenderRoll);
    this.removeChatMessageActionsPasseArme(attackerRoll.passeArme);
  }

  async doRollEncaissement(attackerRoll, defenderRoll) {
    if (Misc.isOwnerPlayer(this.defender)) {
      attackerRoll.attackerId = this.attackerId;
      attackerRoll.defenderTokenId = this.defenderToken.id;
      await this.computeRecul(defenderRoll);
      await this.defender.encaisserDommages(attackerRoll.dmg, this.attacker, defenderRoll?.show, this.attackerToken, this.defenderToken);
    }
    else { // envoi à un GM: les joueurs n'ont pas le droit de modifier les personnages qu'ils ne possèdent pas
      game.socket.emit(SYSTEM_SOCKET_ID, {
        msg: "msg_encaisser",
        data: {
          attackerId: this.attackerId,
          attackerRoll: attackerRoll,
          attackerToken: this.attackerToken,
          defenderToken: this.defenderToken
        }
      })
    }
  }

  /* -------------------------------------------- */
  static async displayActorCombatStatus(combat, actor, token) {
    if (!actor?.isActorCombat()) {
      return
    }
    const alias = token?.name ?? actor.getAlias();
    const blessuresGraves = actor.countBlessures(it => it.isGrave());
    const formData = {
      combatId: combat.id,
      alias: alias,
      etatGeneral: actor.getEtatGeneral(),
      isSonne: actor.isSonne(),
      blessuresStatus: actor.computeResumeBlessure(),
      SConst: actor.getSConst(),
      actorId: actor.id,
      actor: actor,
      tokenId: token.id,
      blessuresGraves: blessuresGraves,
      isCritique: actor.countBlessures(it => it.isCritique()) > 0
    }
    await ChatMessage.create({
      content: await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/chat-actor-turn-acteur.hbs`, formData),
      alias: alias
    })
    await ChatMessage.create({
      content: await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/chat-actor-turn-sante.hbs`, formData),
      whisper: ChatUtility.getOwners(actor),
      alias: alias
    })
  }
}

