import { MournbladeCYD2Utility } from "../mournblade-cyd2-utility.js";

/**
 * Dialogue de jet de dé pour MournbladeCYD2 - Version DialogV2
 */
export class MournbladeCYD2RollDialog {

  /**
   * Create and display the roll dialog
   * @param {MournbladeCYD2Actor} actor
   * @param {Object} rollData
   */
  static async create(actor, rollData) {
    const context = {
      ...rollData,
      difficulte: String(rollData.difficulte || 0),
      img: actor.img,
      name: actor.name,
      config: game.system.mournbladecyd2.config,
      attributs: game.system.mournbladecyd2.config.attributs,
    };

    const content = await foundry.applications.handlebars.renderTemplate(
      "systems/fvtt-mournblade-cyd-2-0/templates/roll-dialog-generic.hbs",
      context
    );

    return foundry.applications.api.DialogV2.wait({
      window: { title: "Test de Capacité", icon: "fa-solid fa-dice-d20" },
      classes: ["mournblade-cyd2-roll-dialog"],
      position: { width: 470 },
      modal: false,
      content,
      buttons: [
        {
          action: "rolld10",
          label: "Lancer 1d10",
          icon: "fa-solid fa-dice-d10",
          default: true,
          callback: (event, button, dialog) => {
            MournbladeCYD2RollDialog._updateRollDataFromForm(rollData, button.form, actor);
            rollData.mainDice = "d10";
            MournbladeCYD2Utility.rollMournbladeCYD2(rollData);
          }
        },
        {
          action: "rolld20",
          label: "Lancer 1d20",
          icon: "fa-solid fa-dice-d20",
          callback: (event, button, dialog) => {
            MournbladeCYD2RollDialog._updateRollDataFromForm(rollData, button.form, actor);
            rollData.mainDice = "d20";
            MournbladeCYD2Utility.rollMournbladeCYD2(rollData);
          }
        },
        {
          action: "cancel",
          label: "Annuler",
          icon: "fa-solid fa-times"
        }
      ]
    });
  }

  /* -------------------------------------------- */
  static _updateRollDataFromForm(rollData, form, actor) {
    const el = form.elements;
    const getVal = (name) => el[name]?.value;
    const getChecked = (name) => {
      const e = form.querySelector(`#${name}`);
      return e ? e.checked : false;
    };

    if (el.modificateur) rollData.modificateur = Number(getVal("modificateur"));
    if (el["bonus-malus-context"]) rollData.bonusMalusContext = Number(getVal("bonus-malus-context"));
    if (el.difficulte) rollData.difficulte = Number(getVal("difficulte"));
    if (el.attrKey) rollData.attrKey = String(getVal("attrKey"));
    if (el.attrKey2) rollData.attrKey2 = String(getVal("attrKey2"));
    if (el["select-maitrise"]) rollData.maitriseId = String(getVal("select-maitrise"));
    if (el["competence-talents"]) {
      const sel = el["competence-talents"];
      rollData.selectedTalents = Array.from(sel.selectedOptions).map(o => o.value);
    }
    if (el["taille-cible"]) rollData.tailleCible = String(getVal("taille-cible"));
    if (el["tireur-deplacement"]) rollData.tireurDeplacement = String(getVal("tireur-deplacement"));
    if (el["cible-couvert"]) rollData.cibleCouvert = String(getVal("cible-couvert"));
    if (el["distance-tir"]) rollData.distanceTir = String(getVal("distance-tir"));
    if (el["soutiens"]) rollData.soutiens = Number(getVal("soutiens"));
    if (el["runemode"]) rollData.runemode = String(getVal("runemode"));
    if (el["runeame"]) rollData.runeame = Number(getVal("runeame"));

    rollData.defenseurAuSol = getChecked("defenseur-au-sol");
    rollData.defenseurAveugle = getChecked("defenseur-aveugle");
    rollData.defenseurDeDos = getChecked("defenseur-de-dos");
    rollData.defenseurRestreint = getChecked("defenseur-restreint");
    rollData.defenseurImmobilise = getChecked("defenseur-immobilise");
    rollData.attaquantsMultiples = getChecked("attaquants-multiple");
    rollData.ambidextre1 = getChecked("ambidextre-1");
    rollData.ambidextre2 = getChecked("ambidextre-2");
    rollData.feinte = getChecked("feinte");
    rollData.attaqueCharge = getChecked("attaque-charge");
    rollData.chargeCavalerie = getChecked("charge-cavalerie");
    rollData.contenir = getChecked("contenir");
    rollData.attaqueDesarme = getChecked("attaque-desarme");
    rollData.cibleDeplace = getChecked("tireur-cible-deplace");
    rollData.cibleCaC = getChecked("cible-cac");
    rollData.cibleconsciente = getChecked("cibleconsciente");
  }
}
