import { Misc } from "./misc.js";

export class RdDRollDialogEthylisme extends foundry.applications.api.DialogV2 {
  constructor(html, rollData, actor, onRoll) {
    super({
      title: "Test d'éthylisme",
      content: html,
      classes: ["rdd-roll-dialog"],
      position: { width: 400, height: 'fit-content' },
      buttons: [
        { label: "Test d'éthylisme", action: "rollButton", callback: () => onRoll(this.rollData) }
      ]
    })
    this.rollData = rollData
    this.actor = actor
  }

  _onRender(context) {
    super._onRender(context)
    this.bringToFront()
    const el = this.element
    el.querySelector(".force-alcool")?.addEventListener("change", event => {
      this.rollData.forceAlcool = Misc.toInt(event.currentTarget.value)
      this.updateRollResult()
    })
    const fa = el.querySelector(".force-alcool")
    if (fa) fa.value = Misc.toInt(this.rollData.forceAlcool)
    this.updateRollResult()
  }

  async updateRollResult() {
    const el = this.element
    const textEl = el.querySelector(".roll-ethylisme")
    if (textEl) {
      textEl.textContent = this.rollData.vie + " / " + Misc.toSignedString(Number(this.rollData.etat) + Number(this.rollData.forceAlcool) + this.rollData.diffNbDoses)
    }
    el.querySelector(".table-resolution")?.remove()
  }
}
