import BoLBaseActorSheet from "./base-actor-sheet.mjs"

/**
 * Actor Sheet for BoL hordes using AppV2
 */
export default class BoLHordeSheet extends BoLBaseActorSheet {
  /** @override */
  static DEFAULT_OPTIONS = {
    ...super.DEFAULT_OPTIONS,
    classes: [...super.DEFAULT_OPTIONS.classes],
    actions: {
      ...super.DEFAULT_OPTIONS.actions,
    },
  }

  /** @override */
  static PARTS = {
    sheet: {
      template: "systems/bol/templates/actor/horde-sheet.hbs",
    },
  }

  /** @override */
  tabGroups = { primary: "stats" }

  /** @override */
  async _prepareContext() {
    const context = await super._prepareContext()
    const actor = this.document

    context.options = this.options
    context.editScore = this.options.editScore
    context.description = await foundry.applications.ux.TextEditor.implementation.enrichHTML(
      actor.system.details?.biography || "", { async: true }
    )

    console.log("HORDE (AppV2)", context)
    return context
  }

  /** @override */
  _activateListeners() {
    super._activateListeners()
    if (!this.isEditable) return

    // Item create via header dataset
    this.element.querySelectorAll(".item-create").forEach((el) => {
      el.addEventListener("click", (ev) => {
        ev.preventDefault()
        const header = ev.currentTarget
        const type = header.dataset.type
        const data = foundry.utils.duplicate(header.dataset)
        delete data.type
        this.actor.createEmbeddedDocuments("Item", [{ name: `New ${type}`, type, data }])
      })
    })

    // Create generic item
    this.element.querySelectorAll(".create_item").forEach((el) => {
      el.addEventListener("click", () => {
        this.actor.createEmbeddedDocuments("Item", [{ name: "Nouvel Equipement", type: "item" }], { renderSheet: true })
      })
    })
  }
}
