export class RdDTMRRencontreDialog extends foundry.applications.api.DialogV2 {
  constructor(actor, rencontre, tmr) {
    const buttons = [
      { label: "Se dérober", action: "derober", callback: () => this.onButtonAction("derober") },
      { label: "Maîtriser", action: "maitriser", callback: () => this.onButtonAction("maitriser") }
    ]
    if ((rencontre.system.refoulement ?? 0) == 0) {
      buttons.push({ label: "Ignorer", action: "ignorer", callback: () => this.onButtonAction("ignorer") })
    }
    else {
      buttons.push({ label: "Refouler", action: "refouler", callback: () => this.onButtonAction("refouler") })
    }
    super({
      title: "Rencontre en TMR!",
      content: "Vous rencontrez un " + rencontre.name + " de force " + rencontre.system.force + "<br>",
      buttons,
      classes: ["tmrrencdialog"],
      position: { width: 320, height: 'fit-content' }
    })
    this.toClose = false
    this.tmr = tmr
    this.actor = actor
    this.rencontre = rencontre
  }

  async onButtonAction(action) {
    this.toClose = true
    await this.actor.tmrApp?.restoreTMRAfterAction()
    this.actor.tmrApp?.onActionRencontre(action, this.tmr, this.rencontre)
  }

  async close() {
    if (this.actor.tmrApp) {
      if (this.toClose) {
        return await super.close()
      }
      else {
        ui.notifications.info("Vous devez résoudre la rencontre.")
        return this.actor.tmrApp.forceTMRContinueAction()
      }
    }
    else {
      return await super.close()
    }
  }
}
