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

export default class LesOubliesCreatureSheet extends LesOubliesActorSheet {
  static DEFAULT_OPTIONS = {
    ...super.DEFAULT_OPTIONS,
    classes: [...super.DEFAULT_OPTIONS.classes, "creature"],
    actions: {
      ...super.DEFAULT_OPTIONS.actions,
      switchTab: LesOubliesCreatureSheet.#onSwitchTab,
    },
    window: {
      ...super.DEFAULT_OPTIONS.window,
      title: "TYPES.Actor.creature",
    },
  }

  static PARTS = {
    sheet: {
      template: "systems/fvtt-les-oublies/templates/actor-creature-sheet-v5.hbs",
    },
  }

  _activeTab = "overview"

  #getTabs() {
    const tabs = {
      overview: { id: "overview", label: "Aperçu", icon: "fa-solid fa-dragon" },
      aptitudes: { id: "aptitudes", label: "Aptitudes", icon: "fa-solid fa-book-open" },
      combat: { id: "combat", label: "Combat & équipement", icon: "fa-solid fa-shield-halved" },
      notes: { id: "notes", label: "Notes", icon: "fa-solid fa-feather-pointed" },
    }

    for (const tab of Object.values(tabs)) {
      tab.active = this._activeTab === tab.id
      tab.cssClass = tab.active ? "active" : ""
    }

    return tabs
  }

  async _prepareContext() {
    const context = await super._prepareContext()
    context.tabs = this.#getTabs()
    context.derived = this.document.getDerivedOverview()
    context.skillGroups = this.document.getGroupedCompetences()
    context.spells = this.document.getEmbeddedItems("sortilege")
    context.weapons = this._prepareWeaponEntries(this.document.getEmbeddedItems("arme"))
    context.armors = this.document.getEmbeddedItems("armure")
    context.equipment = this.document.getEmbeddedItems("equipement")
    return context
  }

  static #onSwitchTab(event, target) {
    const tab = target.dataset.tab
    if (!tab || this._activeTab === tab) return
    this._activeTab = tab
    this.render()
  }
}
