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

export default class VermineNpcSheetV2 extends VermineBaseActorSheet {

  // La fiche PNJ s'ouvre en mode édition : profil et niveaux (Menace,
  // Expérience, Rôle) se règlent dès l'ouverture (le mode jeu reste accessible
  // via le toggle).
  _sheetMode = this.constructor.SHEET_MODES.EDIT

  static DEFAULT_OPTIONS = {
    classes: ["npc"],
    position: { width: 750, height: 680 },
    window: { contentClasses: ["npc-content"] }
  }

  static PARTS = {
    main: { template: "systems/vermine2047/templates/actor/appv2/npc-main.hbs", scrollable: [""] },
    tabs: { template: "templates/generic/tab-navigation.hbs" },
    traits: { template: "systems/vermine2047/templates/actor/appv2/npc-traits.hbs", scrollable: [""] },
    equipment: { template: "systems/vermine2047/templates/actor/appv2/npc-equipment.hbs", scrollable: [""] },
    combat: { template: "systems/vermine2047/templates/actor/appv2/npc-combat.hbs", scrollable: [""] },
    notes: { template: "systems/vermine2047/templates/actor/appv2/npc-notes.hbs", scrollable: [""] }
  }

  tabGroups = { sheet: "traits" }

  #getTabs() {
    const tabs = {
      traits: { id: "traits", group: "sheet", icon: "fas fa-dice", label: "VERMINE.traits" },
      equipment: { id: "equipment", group: "sheet", icon: "fas fa-hammer", label: "VERMINE.tabs.equipment" },
      combat: { id: "combat", group: "sheet", icon: "fas fa-sword", label: "VERMINE.combat" },
      notes: { id: "notes", group: "sheet", icon: "fas fa-sticky-note", label: "IDENTITY.notes" }
    }
    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
    switch (partId) {
      case "main":
        context.npcThreatOptions = CONFIG.VERMINE.npcThreatLevels
        context.npcExperienceOptions = CONFIG.VERMINE.npcExperienceLevels
        context.npcRoleOptions = CONFIG.VERMINE.npcRoleLevels
        context.totemOptions = CONFIG.VERMINE.totems
        break
      case "traits":
        context.tab = context.tabs.traits
        break
      case "equipment":
        context.tab = context.tabs.equipment
        context.weapons = doc.itemTypes.weapon
        context.defenses = doc.itemTypes.defense
        break
      case "combat":
        context.tab = context.tabs.combat
        context.equippedWeapons = doc.itemTypes.weapon.filter(i => i.system.equipped)
        context.equippedDefenses = doc.itemTypes.defense.filter(i => i.system.equipped)
        const { prepareActiveEffectCategories } = await import("../../system/effects.mjs")
        context.effects = prepareActiveEffectCategories(doc.effects)
        break
      case "notes":
        context.tab = context.tabs.notes
        break
    }
    return context
  }

  /** @override - Le PNJ n'a ni caractéristiques ni compétences : les jets
   * rapides utilisent la valeur d'Attaque (dialogue de combat) ou les valeurs
   * dérivées Action / Spécialité / Réaction (dialogue PNJ simplifié). */
  async _onRoll(event) {
    event.preventDefault()
    const el = event.currentTarget
    const type = el.dataset.type
    if (!type) return
    if (type === "attack") {
      const { default: CombatDialog } = await import("../../system/dialogs/combatDialog.mjs")
      const dialog = await CombatDialog.create({ actorId: this.document.id })
      if (dialog) dialog.render(true)
      return
    }
    if (["action", "specialty", "reaction"].includes(type)) {
      const { default: NpcRollDialog } = await import("../../system/dialogs/npcRollDialog.mjs")
      const dialog = await NpcRollDialog.create({ actorId: this.document.id, rolltype: type })
      if (dialog) dialog.render(true)
      return
    }
    return super._onRoll(event)
  }
}
