import MournbladeCYD2ActorSheetV2 from "./base-actor-sheet.mjs";
import { MournbladeCYD2Utility } from "../../mournblade-cyd2-utility.js";

export default class MournbladeCYD2CreatureSheet extends MournbladeCYD2ActorSheetV2 {

  /** @override */
  static DEFAULT_OPTIONS = {
    ...super.DEFAULT_OPTIONS,
    classes: [...super.DEFAULT_OPTIONS.classes, "creature"],
    window: {
      ...super.DEFAULT_OPTIONS.window,
      title: "SHEETS.Actor.creature",
    },
  };

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

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

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

    context.skills = actor.getSkills?.() ?? [];
    context.combativiteList = MournbladeCYD2Utility.getCombativiteList(actor.system.sante?.nbcombativite || 0);
    context.ameList = MournbladeCYD2Utility.getAmeList(actor.system.ame.nbame, actor.getAmeMax?.() ?? 0);
    context.ameMaxList = MournbladeCYD2Utility.getAmeMaxList(actor.system.ame.nbame);
    context.armes = foundry.utils.duplicate(actor.getWeapons?.() ?? []);
    context.protections = foundry.utils.duplicate(actor.getArmors?.() ?? []);
    context.runes = foundry.utils.duplicate(actor.getRunes?.() ?? []);
    context.combat = actor.getCombatValues?.() ?? {};
    context.equipements = foundry.utils.duplicate(actor.getEquipments?.() ?? []);
    context.monnaies = foundry.utils.duplicate(actor.getMonnaies?.() ?? []);
    context.talents = foundry.utils.duplicate(actor.getTalents?.() ?? []);
    context.traitsChaotiques = foundry.utils.duplicate(actor.getTraitsChaotiques?.() ?? []);
    context.traitsEspeces = foundry.utils.duplicate(actor.getTraitsEspeces?.() ?? []);
    context.protectionTotal = actor.getProtectionTotal?.() ?? 0;
    context.adversiteTotal = (actor.system.adversite?.bleue || 0) + (actor.system.adversite?.rouge || 0) + (actor.system.adversite?.noire || 0);
    
    // Utiliser les valeurs manuelles si elles existent, sinon les valeurs calculées
    context.initiative = actor.system.combat?.inittotal !== undefined ? actor.system.combat.inittotal : (context.combat?.initTotal ?? 0);
    context.combat.defenseTotal = actor.system.combat?.defensetotal !== undefined ? actor.system.combat.defensetotal : context.combat.defenseTotal;
    context.protectionTotal = actor.system.combat?.protectiontotal !== undefined ? actor.system.combat.protectiontotal : context.protectionTotal;
    
    return context;
  }
}
