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

export default class VermineCharacterSheetV2 extends VermineBaseActorSheet {

  static DEFAULT_OPTIONS = {
    classes: ["character"],
    position: { width: 860, height: 720 },
    window: { contentClasses: ["character-content"] },
    actions: {
      addSpecialty: VermineCharacterSheetV2.#onAddSpecialty
    }
  }

  static PARTS = {
    main: { template: "systems/vermine2047/templates/actor/appv2/character-main.hbs" },
    tabs: { template: "templates/generic/tab-navigation.hbs" },
    abilities: { template: "systems/vermine2047/templates/actor/appv2/character-abilities.hbs" },
    totem: { template: "systems/vermine2047/templates/actor/appv2/character-totem.hbs" },
    equipment: { template: "systems/vermine2047/templates/actor/appv2/character-equipment.hbs" },
    stories: { template: "systems/vermine2047/templates/actor/appv2/character-stories.hbs" },
    combat: { template: "systems/vermine2047/templates/actor/appv2/character-combat.hbs" }
  }

  tabGroups = { sheet: "abilities" }

  #getTabs() {
    const tabs = {
      abilities: { id: "abilities", group: "sheet", icon: "fas fa-address-card", label: "VERMINE.tabs.abilities" },
      totem: { id: "totem", group: "sheet", icon: "fas fa-star", label: "VERMINE.tabs.totem" },
      equipment: { id: "equipment", group: "sheet", icon: "fas fa-hammer", label: "VERMINE.tabs.equipment" },
      stories: { id: "stories", group: "sheet", icon: "fas fa-book-open-reader", label: "VERMINE.tabs.stories" },
      combat: { id: "combat", group: "sheet", icon: "fas fa-medal", label: "VERMINE.tabs.combat" }
    }
    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
  }

  async _preparePartContext(partId, context) {
    const doc = this.document
    context.systemFields = doc.system.schema.fields
    switch (partId) {
      case "main": break
      case "abilities":
        context.tab = context.tabs.abilities
        break
      case "totem":
        context.tab = context.tabs.totem
        context.abilities = doc.itemTypes.ability.filter(i => i.system.type !== "totem")
        context.totem_abilities = doc.itemTypes.ability.filter(i => i.system.type === "totem")
        context.specialties = doc.itemTypes.specialty
        context.backgrounds = doc.itemTypes.background
        context.traumas = doc.itemTypes.trauma
        context.evolutions = doc.itemTypes.evolution
        break
      case "equipment":
        context.tab = context.tabs.equipment
        context.gear = doc.itemTypes.item
        context.weapons = doc.itemTypes.weapon
        context.defenses = doc.itemTypes.defense
        context.vehicles = doc.itemTypes.vehicle
        break
      case "stories":
        context.tab = context.tabs.stories
        break
      case "combat":
        context.tab = context.tabs.combat
        const { prepareActiveEffectCategories } = await import("../../system/effects.mjs")
        context.effects = prepareActiveEffectCategories(doc.effects)
        break
    }
    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")
    }
  }

  static async #onAddSpecialty(event, target) {
    const skillKey = target.dataset.skill
    const name = game.i18n.localize("ITEMS.new_specialty")
    const itemData = { name, type: "specialty" }
    if (skillKey) itemData.system = { skill: skillKey }
    await this.document.createEmbeddedDocuments("Item", [itemData])
  }
}
