import { ENTITE_BLURETTE, ENTITE_INCARNE } from "./constants.js";
import { RdDUtility } from "./rdd-utility.js";

/**
 * Extend the base Dialog entity by defining a custom window to perform roll.
 * @extends {Dialog}
 */
export class RdDEncaisser extends Dialog {

  static async encaisser(actor) {
    let html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/dialog-roll-encaisser.html',
      { ajustementsEncaissement: RdDUtility.getAjustementsEncaissement() }
    );
    new RdDEncaisser(html, actor).render(true);
  }

  /* -------------------------------------------- */
  constructor(html, actor) {
    let dialogConf = {
      title: "Jet d'Encaissement",
      content: html,
    }

    if (!actor.isEntite()) {
      dialogConf.default = "mortel";
      dialogConf.buttons = {
        "mortel": { label: "Mortel", callback: html => this.performEncaisser("mortel") },
        "non-mortel": { label: "Non-mortel", callback: html => this.performEncaisser("non-mortel") },
        "sonne": { label: "Sonné", callback: html => this.actor.setSonne() },
      };
    }
    else if (actor.isEntite([ENTITE_BLURETTE, ENTITE_INCARNE])) {
      dialogConf.default = "entiteincarnee"
      dialogConf.buttons = {
        "entiteincarnee": { label: "Entité incarnée", callback: html => this.performEncaisser("entiteincarnee") }
      }
    }

    let dialogOptions = {
      classes: ["rdd-roll-dialog"],
      width: 320,
      height: 'fit-content'
    }

    // Select proper roll dialog template and stuff
    super(dialogConf, dialogOptions);

    this.actor = actor;
    this.modifier = 0;
    this.encaisserSpecial = "aucun";
  }

  activateListeners(html) {
    super.activateListeners(html);
    this.html = html;

    this.html.find('[name="modificateurDegats"]').val("0");
    this.html.find('[name="modificateurDegats"]').change((event) => {
      this.modifier = event.currentTarget.value; // Update the selected bonus/malus
    });
    this.html.find('[name="encaisserSpecial"]').change((event) => {
      this.encaisserSpecial = event.currentTarget.value; // Update the selected bonus/malus
    });
  }

  /* -------------------------------------------- */
  performEncaisser(mortalite) {
    this.actor.encaisserDommages({
      dmg: {
        total: Number(this.modifier),
        ajustement: Number(this.modifier),
        encaisserSpecial: this.encaisserSpecial,
        mortalite: mortalite
      }
    })
  }
}