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

export default class WastelandCompetenceSheet extends WastelandItemSheet {
  /** @override */
  static DEFAULT_OPTIONS = {
    classes: ["competence"],
    position: {
      width: 620,
    },
    window: {
      contentClasses: ["competence-content"],
    },
    actions: {
      "add-predilection": this.#onAddPredilection,
      "delete-predilection": this.#onDeletePredilection
    }
  }

  /** @override */
  static PARTS = {
    main: {
      template: "systems/fvtt-wasteland/templates/item-competence-sheet.hbs",
    },
  }

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

  #getTabs() {
    const tabs = {
      details: { id: "details", group: "primary", label: "Détails" },
      description: { id: "description", group: "primary", label: "Description" }
    }
    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()
    return context
  }

  /**
   * Handle adding a new predilection
   * @param {PointerEvent} event - The triggering event
   * @param {HTMLElement} target - The button element
   */
  static async #onAddPredilection(event, target) {
    const predilections = foundry.utils.duplicate(this.document.system.predilections)
    predilections.push({ name: "Nouvelle prédilection", used: false })
    await this.document.update({ "system.predilections": predilections })
  }

  /**
   * Handle deleting a predilection
   * @param {PointerEvent} event - The triggering event
   * @param {HTMLElement} target - The delete button element
   */
  static async #onDeletePredilection(event, target) {
    const index = parseInt(target.dataset.index)
    const predilections = foundry.utils.duplicate(this.document.system.predilections)
    predilections.splice(index, 1)
    await this.document.update({ "system.predilections": predilections })
  }
}
