import { TeDeumUtility } from "../common/tedeum-utility.js";

const { HandlebarsApplicationMixin } = foundry.applications.api

/* -------------------------------------------- */
export class TeDeumRollDialog extends HandlebarsApplicationMixin(foundry.applications.api.ApplicationV2) {

  static DEFAULT_OPTIONS = {
    classes: ["fvtt-te-deum", "te-deum-roll-dialog"],
    window: { title: "Lancer !", resizable: false },
    position: { width: 540 },
    actions: {
      roll: TeDeumRollDialog.#onRoll,
      cancel: TeDeumRollDialog.#onCancel,
    }
  }

  static PARTS = {
    content: { template: "systems/fvtt-te-deum/templates/dialogs/roll-dialog-generic.hbs" }
  }

  /* -------------------------------------------- */
  constructor(actor, rollData, options = {}) {
    super(options)
    this.actor = actor
    this.rollData = rollData
  }

  /* -------------------------------------------- */
  static async create(actor, rollData) {
    const dialog = new TeDeumRollDialog(actor, rollData)
    dialog.render(true)
    return dialog
  }

  /* -------------------------------------------- */
  async _prepareContext() {
    return { ...this.rollData }
  }

  /* -------------------------------------------- */
  async refreshDialog() {
    this.render()
  }

  /* -------------------------------------------- */
  static #onRoll(event, target) {
    TeDeumUtility.rollTeDeum(this.rollData)
    this.close()
  }

  /* -------------------------------------------- */
  static #onCancel(event, target) {
    this.close()
  }

  /* -------------------------------------------- */
  _onRender(context, options) {
    super._onRender(context, options)

    const html = this.element

    html.querySelector('#bonusMalusPerso')?.addEventListener('change', (event) => {
      this.rollData.bonusMalusPerso = Number(event.currentTarget.value)
    })
    html.querySelector('#roll-allonge')?.addEventListener('change', (event) => {
      this.rollData.allongeId = event.currentTarget.value
    })
    html.querySelector('#roll-main-gauche')?.addEventListener('change', (event) => {
      this.rollData.isMainGauche = event.currentTarget.checked
    })
    html.querySelector('#roll-difficulty')?.addEventListener('change', (event) => {
      this.rollData.difficulty = String(event.currentTarget.value) || "pardefaut"
    })
    html.querySelector('#roll-attaque-ciblee')?.addEventListener('change', (event) => {
      this.rollData.attaqueCiblee = event.currentTarget.value || "0"
    })
    html.querySelector('#roll-bonus-malus')?.addEventListener('change', (event) => {
      this.rollData.bonusMalus = event.currentTarget.value || "0"
    })
    html.querySelector('#roll-enable-providence')?.addEventListener('change', (event) => {
      this.rollData.enableProvidence = event.currentTarget.checked
    })
    html.querySelector('#roll-portee-tir')?.addEventListener('change', (event) => {
      this.rollData.porteeTir = event.currentTarget.value
      this.rollData.difficulty = game.system.tedeum.config.ARME_PORTEES[this.rollData.porteeTir].difficulty
      this.rollData.porteeLabel = game.system.tedeum.config.ARME_PORTEES[this.rollData.porteeTir].label
      this.refreshDialog()
    })
    html.querySelector('#roll-tir-viser')?.addEventListener('change', (event) => {
      this.rollData.isViser = event.currentTarget.checked
    })
    html.querySelector('#roll-tir-mouvement')?.addEventListener('change', (event) => {
      this.rollData.isMouvement = event.currentTarget.checked
    })
    html.querySelector('#roll-charge-a-pied')?.addEventListener('change', (event) => {
      this.rollData.isChargeAPied = event.currentTarget.checked
    })
    html.querySelector('#roll-charge-a-cheval')?.addEventListener('change', (event) => {
      this.rollData.isChargeACheval = event.currentTarget.checked
    })
  }
}
