import { YggdrasillUtility } from "./yggdrasill-utility.js";

/* -------------------------------------------- */
export class YggdrasillCombat extends Combat {
  
  /* -------------------------------------------- */
  async rollInitiative(ids, formula = undefined, messageOptions = {} ) {
    ids = typeof ids === "string" ? [ids] : ids;
    const currentId = this.combatant?.id;
    for (let cId = 0; cId < ids.length; cId++) {
      const c = this.combatants.get(ids[cId]);
      let initBonus = c.actor ? c.actor.getInitiativeScore() : 0;
      //console.log("Init for ", initBonus);
      const roll = c.getInitiativeRoll("1d10+"+initBonus);
      if ( !roll.total) {
        roll.evaluate( {async: false});
      }
      if (roll.total <= 0) roll.total = 0;
      //console.log("Compute init for", roll.total);
      let id = c.id;
      await this.updateEmbeddedDocuments("Combatant", [{ _id: id, initiative: roll.total }]);

      // Send a chat message
      let rollMode = messageOptions.rollMode || game.settings.get("core", "rollMode");
      let messageData = foundry.utils.mergeObject(
        {
          speaker: {
            scene: canvas.scene.id,
            actor: c.actor ? c.actor.id : null,
            token: c.token.id,
            alias: c.token.name,
            sound: CONFIG.sounds.dice,
          },
          flavor: `${c.token.name} a fait son jet d'Initiative (1d10+${initBonus})
          <br>
          `,
        },
        messageOptions
      );
      roll.toMessage(messageData, { rollMode, create: true });
    }

    return this;
  }

}
