import { VadentisUtility } from "./vadentis-utility.js";

/* -------------------------------------------- */
export class VadentisCombat extends Combat {
  
  /* -------------------------------------------- */
  async rollInitiative(ids, formula = undefined, messageOptions = {} ) {
    console.log("Initiative is requested !!!");

    ids = typeof ids === "string" ? [ids] : ids;
    const currentId = this.combatant._id;
    for (let cId = 0; cId < ids.length; cId++) {
      const c = this.getCombatant(ids[cId]);
      let initBonus = c.actor ? c.actor.getInitiativeScore() : 0;
      console.log("Init for ", initBonus);
      const roll = super._getInitiativeRoll(c, "1d20+"+initBonus);

      if (roll.total <= 0) roll.total = 0;
      console.log("Compute init for", roll.total);
      await this.updateEmbeddedEntity("Combatant", { _id: c._id, initiative: roll.total });

      // Send a chat message
      let rollMode = messageOptions.rollMode || game.settings.get("core", "rollMode");
      let messageData = 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 (1d20+${initBonus})
          <br>
          `,
        },
        messageOptions
      );
      roll.toMessage(messageData, { rollMode, create: true });
    }

    return this;
  }

}
