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

export default class VermineCreatureSheetV2 extends VermineBaseActorSheet {

  static DEFAULT_OPTIONS = {
    classes: ["creature"],
    position: { width: 700, height: 650 },
    window: { contentClasses: ["creature-content"] }
  }

  static PARTS = {
    main: { template: "systems/vermine2047/templates/actor/appv2/creature-main.hbs" },
    tabs: { template: "templates/generic/tab-navigation.hbs" },
    info: { template: "systems/vermine2047/templates/actor/appv2/creature-info.hbs" },
    stats: { template: "systems/vermine2047/templates/actor/appv2/creature-stats.hbs" },
    combat: { template: "systems/vermine2047/templates/actor/appv2/creature-combat.hbs" },
    effects: { template: "systems/vermine2047/templates/actor/appv2/creature-effects.hbs" }
  }

  tabGroups = { sheet: "info" }

  #getTabs() {
    const tabs = {
      info: { id: "info", group: "sheet", icon: "fas fa-info-circle", label: "VERMINE.information" },
      stats: { id: "stats", group: "sheet", icon: "fas fa-chart-bar", label: "VERMINE.stats" },
      combat: { id: "combat", group: "sheet", icon: "fas fa-sword", label: "VERMINE.combat" },
      effects: { id: "effects", group: "sheet", icon: "fas fa-magic", label: "UI.effects.name" }
    }
    for (const v of Object.values(tabs)) {
      v.active = this.tabGroups[v.group] === v.id
      v.cssClass = v.active ? "active" : ""
    }
    return tabs
  }

  async _prepareContext() {
    const context = await super._prepareContext()
    context.tabs = this.#getTabs()
    return context
  }

  changeTab(tab, group, options = {}) {
    super.changeTab(tab, group, options)
    if (group === "sheet") {
      const main = this.element?.querySelector('[data-group="sheet"][data-tab="main"]')
      if (main) main.classList.add("active")
    }
  }

  async _preparePartContext(partId, context) {
    const doc = this.document
    switch (partId) {
      case "main":
        context.patternOptions = CONFIG.VERMINE.creaturePatternLevels
        context.roleOptions = CONFIG.VERMINE.creatureRoleLevels
        context.sizeOptions = CONFIG.VERMINE.creatureSizeLevels
        context.packOptions = CONFIG.VERMINE.creaturePackLevels
        break
      case "info":
        context.tab = context.tabs.info
        break
      case "stats":
        context.tab = context.tabs.stats
        context.patternLabel = doc.system.pattern?.value ? CONFIG.VERMINE.creaturePatternLevels[doc.system.pattern.value]?.label : ""
        context.sizeLabel = doc.system.size?.value || ""
        context.roleLabel = doc.system.role?.value ? CONFIG.VERMINE.creatureRoleLevels[doc.system.role.value]?.label : ""
        context.packLabel = doc.system.pack?.value || game.i18n.localize("VERMINE.none")
        break
      case "combat":
        context.tab = context.tabs.combat
        break
      case "effects":
        context.tab = context.tabs.effects
        const { prepareActiveEffectCategories } = await import("../../system/effects.mjs")
        context.effects = prepareActiveEffectCategories(doc.effects)
        break
    }
    return context
  }
}
