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: 360 },
      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.elements, actor);
            rollData.mainDice = "1d10";
            MournbladeCYD2Utility.rollMournbladeCYD2(rollData);
          }
        },
        {
          action: "rolld20",
          label: "Lancer 1d20",
          icon: "fa-solid fa-dice-d20",
          callback: (event, button, dialog) => {
            MournbladeCYD2RollDialog._updateRollDataFromForm(rollData, button.form.elements, actor);
            rollData.mainDice = "1d20";
            MournbladeCYD2Utility.rollMournbladeCYD2(rollData);
          }
        },
        {
          action: "cancel",
          label: "Annuler",
          icon: "fa-solid fa-times"
        }
      ]
    });
  }

  /* -------------------------------------------- */
  static _updateRollDataFromForm(rollData, elements, actor) {
    if (elements.modificateur) rollData.modificateur = Number(elements.modificateur.value);
    if (elements.difficulte) rollData.difficulte = Number(elements.difficulte.value);
    if (elements.attrKey) rollData.attrKey = String(elements.attrKey.value);
    if (elements.attrKey2) rollData.attrKey2 = String(elements.attrKey2.value);
    if (elements["select-maitrise"]) rollData.maitriseId = String(elements["select-maitrise"].value);
    if (elements["competence-talents"]) {
      const sel = elements["competence-talents"];
      rollData.selectedTalents = Array.from(sel.selectedOptions).map(o => o.value);
    }
    if (elements["taille-cible"]) rollData.tailleCible = String(elements["taille-cible"].value);
    if (elements["tireur-deplacement"]) rollData.tireurDeplacement = String(elements["tireur-deplacement"].value);
    if (elements["cible-couvert"]) rollData.cibleCouvert = String(elements["cible-couvert"].value);
    if (elements["distance-tir"]) rollData.distanceTir = String(elements["distance-tir"].value);
  }
}
