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

export default class HawkmoonPersonnageSheet extends HawkmoonActorSheet {
  /** @override */
  static DEFAULT_OPTIONS = {
    ...super.DEFAULT_OPTIONS,
    classes: [...super.DEFAULT_OPTIONS.classes],
    window: {
      ...super.DEFAULT_OPTIONS.window,
      title: "SHEETS.Actor.personnage",
    },
    actions: {
      ...super.DEFAULT_OPTIONS.actions,
      openCellule: HawkmoonPersonnageSheet.#onOpenCellule,
    },
  }

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

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

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

    // Add personnage-specific data
    context.skills = actor.getSkills()
    context.armes = foundry.utils.duplicate(actor.getWeapons())
    context.monnaies = foundry.utils.duplicate(actor.getMonnaies())
    context.protections = foundry.utils.duplicate(actor.getArmors())
    context.historiques = foundry.utils.duplicate(actor.getHistoriques() || [])
    context.talents = foundry.utils.duplicate(actor.getTalents() || [])
    context.mutations = foundry.utils.duplicate(actor.getMutations() || [])
    context.talentsCell = this.#getCelluleTalents()
    context.celluleId = this.#getCelluleId()
    context.profils = foundry.utils.duplicate(actor.getProfils() || [])
    context.combat = actor.getCombatValues()
    context.equipements = foundry.utils.duplicate(actor.getEquipments())
    context.artefacts = foundry.utils.duplicate(actor.getArtefacts())
    context.richesse = actor.computeRichesse()
    context.coupDevastateur = actor.items.find(it => it.type == "talent" && it.name.toLowerCase() == "coup devastateur" && !it.system.used)
    context.valeurEquipement = actor.computeValeurEquipement()
    context.nbCombativite = actor.system.sante.nbcombativite
    context.combativiteList = HawkmoonUtility.getCombativiteList(actor.system.sante.nbcombativite)
    context.initiative = actor.getFlag("world", "last-initiative") || -1

    return context
  }

  /**
   * Get talents from cellules this actor is a member of
   * @returns {Array}
   * @private
   */
  #getCelluleTalents() {
    let talents = []
    for (let cellule of game.actors) {
      if (cellule.type == "cellule") {
        let found = cellule.system.members.includes(this.actor.id)
        if (found) {
          talents = talents.concat(cellule.getTalents())
        }
      }
    }
    return talents
  }

  /**
   * Get the ID of the cellule this actor is a member of
   * @returns {string|null}
   * @private
   */
  #getCelluleId() {
    for (let cellule of game.actors) {
      if (cellule.type == "cellule") {
        if (cellule.system.members.includes(this.actor.id)) {
          return cellule.id
        }
      }
    }
    return null
  }

  /**
   * Open cellule sheet
   * @param {Event} event
   * @param {HTMLElement} target
   * @private
   */
  static async #onOpenCellule(event, target) {
    const celluleId = target.dataset.celluleId
    if (!celluleId) return
    const cellule = game.actors.get(celluleId)
    if (cellule) cellule.sheet.render(true)
  }
}
