
export default class RMSSToolsDiceRoller extends FormApplication {
  constructor(item, actor) {
    super();
    this.item = foundry.utils.duplicate(item);
    this.actor = actor;
    this.itemName = item.name;
    this.characterBonus = Number(item.system.total_bonus);
    this.stunnedModifier = actor.getStunnedModifier()
    this.rollType = [
      { value: "one_to_onehundred", text: "1-100", selected: false },
      { value: "open_ended", text: "Open-Ended", selected: true },
      { value: "high_open_ended", text: "High Open-Ended", selected: false },
      { value: "low_open_ended", text: "Low Open-Ended", selected: false }
    ];
  }

  static get defaultOptions() {
    return foundry.utils.mergeObject(super.defaultOptions, {
      classes: ["form"],
      title: "Rolemaster Dice Roller",
      popOut: true,
      width: 480,
      height: 440,
      template: "systems/fvtt-rolemaster-frp/templates/sheets/apps/app_dice_roller.html"
    });
  }

  getData() {
    // Send data to the template
    return {
      itemName: this.itemName,
      characterBonus: this.characterBonus,
      selectOptions: this.rollType,
      woundsModifier: this.actor.system.modifiers.woundsModifier,
      config: CONFIG.rmss,
      difficulty: 0,
      combatSituation: 0,
      lightningModifier: 0,
      darknessModifier: 0,
      hitsPerRound: 0,
      isStunned: this.actor.system.state.stunned,
      stunnedModifier: this.stunnedModifier
    };
  }

  activateListeners(html) {
    super.activateListeners(html);
  }

  async _updateObject(event, formData) {
    console.log("Rolling Dice");
    console.log(formData);
    console.log(event);
    this.roll(event.submitter?.dataset?.value, formData);
  }

  /* -------------------------------------------- */
  async showDiceSoNice(roll, rollMode) {
    if (game.modules.get("dice-so-nice")?.active) {
      if (game.dice3d) {
        let whisper = null;
        let blind = false;
        rollMode = rollMode ?? game.settings.get("core", "rollMode");
        switch (rollMode) {
          case "blindroll": //GM only
            blind = true;
          case "gmroll": //GM + rolling player
            whisper = this.getUsers(user => user.isGM);
            break;
          case "roll": //everybody
            whisper = this.getUsers(user => user.active);
            break;
          case "selfroll":
            whisper = [game.user.id];
            break;
        }
        await game.dice3d.showForRoll(roll, game.user, true, whisper, blind);
      }
    }
  }

  /* -------------------------------------------- */
  async roll(rollKey, formData) {
    let baseRoll = await new Roll("1d100").roll();
    await this.showDiceSoNice(baseRoll, game.settings.get("core", "rollMode"))
    let rollType = this.rollType.find(r => r.value == rollKey)?.text;
    let rollData = {
      name: this.itemName,
      rollKey: rollKey,
      rollType: rollType,
      difficulty: Number(formData.difficulty),
      combatSituation: Number(formData?.combatSituation || 0),
      lightningModifier: Number(formData?.lightningModifier || 0),
      darknessModifier: Number(formData?.darknessModifier || 0),
      characterBonus: Number(this.characterBonus),
      woundsModifier: Number(this.actor.system.modifiers.woundsModifier),
      hitsPerRound: Number(formData.hitsPerRound),
      isStunned: this.actor.system.state.stunned,
      stunnedModifier: this.stunnedModifier,
      rolls: [baseRoll],
    }
    if (baseRoll.result == 66) {
      rollData.content = "You rolled a 66!";
    }
    // Process the for low open ended rolls
    if (rollKey === "open_ended" || rollKey === "low_open_ended") {
      if (baseRoll.result < 6) {
        rollData.lowopen = true
        let newRoll = await new Roll("-1d100").roll();
        await this.showDiceSoNice(newRoll, game.settings.get("core", "rollMode"))
        rollData.rolls.push(newRoll);
        while (newRoll.result > 95) {
          newRoll = await new Roll("-1d100").roll();
          await this.showDiceSoNice(newRoll, game.settings.get("core", "rollMode"))
          rollData.rolls.push(newRoll);
        }
      }
    }
    // Process the for high open ended rolls
    if (rollKey === "open_ended" || rollKey === "high_open_ended") {
      if (baseRoll.result > 95) {
        rollData.highopen = true
        let newRoll = await new Roll("1d100").roll();
        await this.showDiceSoNice(newRoll, game.settings.get("core", "rollMode"))
        rollData.rolls.push(newRoll);
        while (newRoll.result > 95) {
          newRoll = await new Roll("1d100").roll();
          await this.showDiceSoNice(newRoll, game.settings.get("core", "rollMode"))
          rollData.rolls.push(newRoll);
        }
      }
    }
    // Compute total of rolls
    rollData.totalRolls = rollData.rolls.reduce((acc, roll) => Number(acc) + Number(roll.result), 0);
    rollData.totalFinal = rollData.totalRolls + Number(rollData.combatSituation) +  
      Number(rollData.lightningModifier) +
      Number(rollData.darknessModifier) +
      Number(this.actor.system.modifiers.woundsModifier) + 
      Number(formData.difficulty) + 
      Number(rollData.hitsPerRound) +
      Number(rollData.stunnedModifier) +
      Number(this.characterBonus);
    console.log(">>> Roll Data: ", rollData);

    // Define the Chat Message Template
    let chatTemplate = "systems/fvtt-rolemaster-frp/templates/chat/chat_dice_roll.html";

    // Pass the Data through to be used in the Chat Message
    let chatData = rollData
    // Render the Rolls to the Chat Window
    renderTemplate(chatTemplate, chatData).then((html) => {
      let chatOptions = {
        style: CONST.CHAT_MESSAGE_STYLES.ROLL,
        flavor: rollType,
        rollMode: game.settings.get("core", "rollMode"),
        content: html,
      };
      ChatMessage.create(chatOptions);
    });
  }

}
