import { renderTemplate } from "./constants.js"
import { Misc } from "./misc.js"

export class DialogConsommer extends foundry.applications.api.DialogV2 {
  static async create(actor, item) {
    const consommerData = DialogConsommer.prepareData(actor, item)
    const content = await renderTemplate("systems/foundryvtt-reve-de-dragon/templates/dialog-item-consommer.hbs", consommerData)
    const dialog = new DialogConsommer(actor, item, consommerData, content)
    dialog.render({ force: true })
    return dialog
  }

  constructor(actor, item, consommerData, content) {
    const btnName = consommerData.buttonName
    super({
      title: consommerData.title,
      content,
      classes: ["dialogconsommer"],
      position: { width: 350, height: 'fit-content' },
      buttons: [
        { label: btnName, action: btnName, callback: () => this.onConsommer() }
      ]
    })
    this.actor = actor
    this.item = item
    this.consommerData = consommerData
  }

  _onRender(context) {
    super._onRender(context)
    const el = this.element
    el.querySelector(".se-forcer")?.addEventListener("change", event => this.setSeForcer(event))
    el.querySelector(".consommer-doses")?.addEventListener("change", event => this.selectDoses(event))
  }

  async onConsommer() {
    const el = this.element
    el.querySelector(".se-forcer")?.dispatchEvent(new Event("change"))
    el.querySelector(".consommer-doses")?.dispatchEvent(new Event("change"))
    await this.actor.consommer(this.item, this.consommerData.choix)
  }

  static prepareData(actor, item) {
    let consommerData = {
      item: foundry.utils.deepClone(item),
      cuisine: actor.getCompetence("cuisine"),
      choix: { doses: 1, seForcer: false }
    }
    switch (item.type) {
      case "herbe": case "faune":
        consommerData.title = "Manger une portion crue: "
        consommerData.buttonName = "Manger"
        break
      case "nourritureboisson":
        consommerData.title = item.system.boisson ? "Boire une dose: " : "Manger une portion: "
        consommerData.buttonName = item.system.boisson ? "Boire" : "Manger"
        break
      case "potion":
        consommerData.title = "Boire la potion: "
        consommerData.buttonName = "Boire"
        break
    }
    consommerData.title += item.name
    DialogConsommer.calculDoses(consommerData, item)
    return consommerData
  }

  static calculDoses(consommer, item) {
    switch (item.type) {
      case "herbe": case "faune":
        consommer.totalSust = consommer.choix.doses
        consommer.totalDesaltere = 0
        consommer.choix.sust = 1
        consommer.choix.quantite = 0
        consommer.choix.encombrement = Misc.keepDecimals(consommer.item.system.encombrement / item.system.sust, 2)
        return
      case "nourritureboisson":
        consommer.choix.sust = consommer.item.system.sust
        consommer.choix.quantite = consommer.choix.doses
        consommer.choix.encombrement = 0
        consommer.totalSust = Misc.keepDecimals(consommer.choix.doses * (consommer.item.system.sust ?? 0), 2)
        consommer.totalDesaltere = consommer.item.system.boisson
          ? Misc.keepDecimals(consommer.choix.doses * (consommer.item.system.desaltere ?? 0), 2)
          : 0
        break
      case "potion":
        consommer.totalSust = 0
        consommer.totalDesaltere = 0
    }
  }

  setSeForcer(event) {
    this.consommerData.choix.seForcer = event.currentTarget.checked
  }

  selectDoses(event) {
    this.consommerData.choix.doses = Number(event.currentTarget.value)
    DialogConsommer.calculDoses(this.consommerData, this.item)
    const el = this.element
    const sust = el.querySelector(".total-sust")
    if (sust) sust.textContent = this.consommerData.totalSust
    const des = el.querySelector(".total-desaltere")
    if (des) des.textContent = this.consommerData.totalDesaltere
  }
}
