import PrismRPGItemSheet from "./base-item-sheet.mjs"

export default class PrismRPGEquipmentSheet extends PrismRPGItemSheet {
  /** @override */
  static DEFAULT_OPTIONS = {
    classes: ["equipment"],
    position: {
      width: 600,
    },
    window: {
      contentClasses: ["equipment-content"],
    },
  }

  /** @override */
  static PARTS = {
    main: {
      template: "systems/fvtt-prism-rpg/templates/equipment.hbs",
    },
  }

  /** @override */
  tabGroups = {
    primary: "details",
  }

  /**
   * Prepare an array of form header tabs.
   * @returns {Record<string, Partial<ApplicationTab>>}
   */
  #getTabs() {
    const tabs = {
      details: { id: "details", group: "primary", label: "PRISMRPG.Label.details" },
      description: { id: "description", group: "primary", label: "PRISMRPG.Label.description" },
      effects: { id: "effects", group: "primary", label: "PRISMRPG.Label.effects" },
    }
    for (const v of Object.values(tabs)) {
      v.active = this.tabGroups[v.group] === v.id
      v.cssClass = v.active ? "active" : ""
    }
    return tabs
  }

  /** @override */
  async _prepareContext() {
    const context = await super._prepareContext()
    context.tabs = this.#getTabs()
    context.enrichedDescription = await foundry.applications.ux.TextEditor.implementation.enrichHTML(this.document.system.description, { async: true })

    // Enrich passive description if equipment is a kit
    if (this.document.system.isKit && this.document.system.passiveDescription) {
      context.enrichedPassiveDescription = await foundry.applications.ux.TextEditor.implementation.enrichHTML(this.document.system.passiveDescription, { async: true })
    }

    // Enrich descriptions for all special activations
    context.enrichedSpecialActivations = await Promise.all(
      this.document.system.specialActivations.map(async (activation) => ({
        ...activation,
        enrichedDescription: await foundry.applications.ux.TextEditor.implementation.enrichHTML(activation.description, { async: true })
      }))
    )

    return context
  }

  /** @override */
  _onRender(context, options) {
    super._onRender(context, options)

    // Add event listeners for special activation management
    this.element.querySelectorAll('[data-action="add-special-activation"]').forEach(el => {
      el.addEventListener("click", this._onAddSpecialActivation.bind(this))
    })

    this.element.querySelectorAll('[data-action="delete-special-activation"]').forEach(el => {
      el.addEventListener("click", this._onDeleteSpecialActivation.bind(this))
    })
  }

  async _onAddSpecialActivation(event) {
    event.preventDefault()
    const specialActivations = [...this.document.system.specialActivations]
    specialActivations.push({ name: "", description: "" })
    await this.document.update({ "system.specialActivations": specialActivations })
  }

  async _onDeleteSpecialActivation(event) {
    event.preventDefault()
    const index = parseInt(event.currentTarget.closest("[data-activation-index]").dataset.activationIndex)
    const specialActivations = this.document.system.specialActivations.filter((_, i) => i !== index)
    await this.document.update({ "system.specialActivations": specialActivations })
  }
}
