import HawkmoonActorSheet from "./base-actor-sheet.mjs"
import { HawkmoonUtility } from "../../hawkmoon-utility.js"

export default class HawkmoonCreatureSheet extends HawkmoonActorSheet {
  /** @override */
  static DEFAULT_OPTIONS = {
    ...super.DEFAULT_OPTIONS,
    classes: [...super.DEFAULT_OPTIONS.classes],
    window: {
      ...super.DEFAULT_OPTIONS.window,
      title: "SHEETS.Actor.creature",
    },
  }

  /** @override */
  static PARTS = {
    sheet: {
      template: "systems/fvtt-hawkmoon-cyd/templates/creature-sheet.hbs",
    },
  }

  /** @override */
  tabGroups = { primary: "principal" }

  /** @override */
  async _prepareContext() {
    const context = await super._prepareContext()
    const actor = this.document

    // Add creature-specific data
    context.skills = actor.getSkills ? actor.getSkills() : []
    context.armes = foundry.utils.duplicate(actor.getWeapons ? actor.getWeapons() : [])
    context.protections = foundry.utils.duplicate(actor.getArmors ? actor.getArmors() : [])
    context.combat = actor.getCombatValues ? actor.getCombatValues() : {}
    context.equipements = foundry.utils.duplicate(actor.getEquipments ? actor.getEquipments() : [])
    context.talents = foundry.utils.duplicate(actor.getTalents ? actor.getTalents() : [])
    context.talentsCell = this.#getCelluleTalents()
    context.nbCombativite = actor.system.sante?.nbcombativite || 0
    context.combativiteList = HawkmoonUtility.getCombativiteList(actor.system.sante?.nbcombativite || 0)
    context.initiative = actor.getFlag("world", "last-initiative") || -1
    context.enrichedDescription = await foundry.applications.ux.TextEditor.implementation.enrichHTML(actor.system.biodata?.description || "", { async: true })
    context.enrichedHabitat = await foundry.applications.ux.TextEditor.implementation.enrichHTML(actor.system.biodata?.habitat || "", { async: true })

    return context
  }

  /**
   * Get talents from attached cellule
   * @private
   */
  #getCelluleTalents() {
    const celluleId = this.actor.system?.details?.celluleid
    if (!celluleId) return []

    const cellule = game.actors.get(celluleId)
    if (!cellule) return []

    return foundry.utils.duplicate(cellule.getTalents?.() || [])
  }
}
