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

export default class YggdrasillPersonnageSheet extends YggdrasillActorSheet {
  /** @override */
  static DEFAULT_OPTIONS = {
    ...super.DEFAULT_OPTIONS,
    classes: [...super.DEFAULT_OPTIONS.classes, "personnage"],
    window: {
      ...super.DEFAULT_OPTIONS.window,
      title: "Feuille de Personnage",
    },
  }

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

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

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

    // Prepare items by type
    context.competencesGenerales = actor.items.filter(i => i.type === 'competence' && i.system.categorie === 'generale')
    context.competencesMartiales = actor.items.filter(i => i.type === 'competence' && i.system.categorie === 'martiale')
    context.competencesMagiques = actor.items.filter(i => i.type === 'competence' && i.system.categorie === 'magique')
    context.dons = actor.items.filter(i => i.type === 'don')
    context.faiblesses = actor.items.filter(i => i.type === 'faiblesse')
    context.blessures = actor.items.filter(i => i.type === 'blessure')
    context.maladies = actor.items.filter(i => i.type === 'maladie')
    context.poisons = actor.items.filter(i => i.type === 'poison')
    context.prouesses = actor.items.filter(i => i.type === 'prouesse')
    context.sortsSejdr = actor.items.filter(i => i.type === 'sortsejdr')
    context.sortsGaldr = actor.items.filter(i => i.type === 'sortgaldr')
    context.runes = actor.items.filter(i => i.type === 'rune')
    context.armesCC = actor.items.filter(i => i.type === 'armecc')
    context.armesDist = actor.items.filter(i => i.type === 'armedist')
    context.armures = actor.items.filter(i => i.type === 'armure')
    context.boucliers = actor.items.filter(i => i.type === 'bouclier')
    context.equipements = actor.items.filter(i => i.type === 'equipement')
    context.monnaies = actor.items.filter(i => i.type === 'monnaie')
    context.effetsMagiques = actor.items.filter(i => i.type === 'effetmagique')

    // Prepare equipped items
    context.armesEquipees = actor.items.filter(i => (i.type === 'armecc' || i.type === 'armedist') && i.system.equipe)
    context.armureEquipee = actor.items.find(i => i.type === 'armure' && i.system.equipe)
    context.bouclierEquipe = actor.items.find(i => i.type === 'bouclier' && i.system.equipe)

    // Calculate total protection from equipped armors
    context.protectionTotal = context.armures
      .filter(a => a.system.equipe)
      .reduce((sum, a) => sum + (parseInt(a.system.protection) || 0), 0)

    // Calculate shield defense bonus from equipped shield
    context.dpBouclier = context.boucliers
      .filter(b => b.system.equipe)
      .reduce((sum, b) => sum + (parseInt(b.system.defensebonus) || 0), 0)

    // Calculate total encumbrance from equipements
    context.encTotal = context.equipements
      .reduce((sum, e) => sum + ((parseInt(e.system.quantite) || 0) * (parseInt(e.system.enc) || 0)), 0)

    // Options for selects
    context.optionsCarac = {}
    for (let i = 0; i <= 10; i++) {
      context.optionsCarac[i] = i.toString()
    }
    
    context.optionsBase = {}
    for (let i = 0; i <= 10; i++) {
      context.optionsBase[i] = i.toString()
    }

    // Options for bonus/malus (-10 to +10)
    context.optionsDMDP = []
    for (let i = -10; i <= 10; i++) {
      context.optionsDMDP.push({ value: i, text: i >= 0 ? `+${i}` : i.toString() })
    }

    // System fields for formInput helpers
    context.systemFields = actor.system.schema.fields

    // Enrich HTML fields
    context.enrichedDescription = await foundry.applications.ux.TextEditor.implementation.enrichHTML(actor.system.biodata?.description || "", { async: true })
    context.enrichedNotes = await foundry.applications.ux.TextEditor.implementation.enrichHTML(actor.system.biodata?.notes || "", { async: true })
    context.enrichedGMNotes = await foundry.applications.ux.TextEditor.implementation.enrichHTML(actor.system.biodata?.gmnotes || "", { async: true })
    context.enrichedTirageRunes = await foundry.applications.ux.TextEditor.implementation.enrichHTML(actor.system.biodata?.tiragerunes || "", { async: true })

    return context
  }
}
