import { ChatUtility } from "./chat-utility.js";
import { HIDE_DICE, SHOW_DICE } from "./constants.js";
import { Misc } from "./misc.js";

const imgHeures = [1, 2, 3, 4, 5, 6, 7, 9, 9, 10, 11, 12].map(heure => {
  if (heure < 10) {
    heure = '0' + heure;
  }
  return `<img src="systems/foundryvtt-reve-de-dragon/icons/heures/hd${heure}.webp" class="dice-img" />`
})

const imgSigneDragon = imgHeures[4]

/** De pour les jets de rencontre */
export class DeTMR extends Die {
  /** @override */
  static DENOMINATION = "t";

  static diceSoNiceData(system) {
    return {
      type: "dt",
      font: "HeuresDraconiques",
      fontScale: 0.8,
      labels: ['1', '2', '3', '4', '5', '6', 'd', '0'],
      system: system
    }
  }

  constructor(termData) {
    termData.faces = 8;
    super(termData);
  }

  async evaluate(options) {
    await super.evaluate(options)
    await this.reroll('r=8', { recursive: true })
    return this;
  }

  get total() {
    return this.values.map(it => Misc.modulo(it, 8)).reduce(Misc.sum(), 0);
  }

  getResultLabel(diceTerm) {
    switch (diceTerm.result) {
      case 7: return imgSigneDragon;
    }
    return diceTerm.result.toString();
  }
}

/** DeDraconique pour le D8 sans limite avec 8=>0 */
export class DeDraconique extends Die {
  /** @override */
  static DENOMINATION = "r";

  static diceSoNiceData(system) {
    return {
      type: "dr",
      font: "HeuresDraconiques",
      fontScale: 0.8,
      labels: ['1', '2', '3', '4', '5', '6', 'd', '0'],
      system: system
    }
  }

  constructor(termData) {
    termData.faces = 8;
    super(termData);
  }

  async evaluate(options) {
    await super.evaluate(options);
    await this.explode("x=7");
    return this;
  }

  get total() {
    return this.values.filter(it => it != 8).reduce(Misc.sum(), 0);
  }

  getResultLabel(diceTerm) {
    switch (diceTerm.result) {
      case 7: return imgSigneDragon
      case 8: return '0';
    }
    return diceTerm.result.toString();
  }
}

/** De 12 avec les heures */
export class DeHeure extends Die {

  /** @override */
  static DENOMINATION = "h";

  static diceSoNiceData(system) {
    return {
      type: "dh",
      font: "HeuresDraconiques",
      fontScale: 1.2,
      labels: ['v', 'i', 'f', 'o', 'd', 'e', 'l', 's', 'p', 'a', 'r', 'c'],
      system: system
    }
  }

  constructor(termData) {
    termData.faces = 12;
    super(termData);
  }

  getResultLabel(diceTerm) {
    return imgHeures[diceTerm.result - 1]
  }
}

export class RdDDice {
  static init() {
    CONFIG.Dice.terms[DeTMR.DENOMINATION] = DeTMR
    CONFIG.Dice.terms[DeDraconique.DENOMINATION] = DeDraconique
    CONFIG.Dice.terms[DeHeure.DENOMINATION] = DeHeure
  }

  static onReady() {
    if (game.modules.get("dice-so-nice")?.active) {
      if (game.settings.get("core", "noCanvas")) {
        ui.notifications.warn("Dice So Nice! n'affichera pas de dés car vous avez coché l'option de Foundry 'Scène de jeu désactivé' 'Disable Game Canvas' ");
      }
    }
  }

  static diceSoNiceReady(dice3d) {
    dice3d.DiceFactory.systems.keys().forEach(system => {
      dice3d.addDicePreset(DeTMR.diceSoNiceData(system));
      dice3d.addDicePreset(DeDraconique.diceSoNiceData(system));
      dice3d.addDicePreset(DeHeure.diceSoNiceData(system));
    })
  }

  static async rollHeure(options = { showDice: HIDE_DICE }) {
    return await RdDDice.rollTotal("1dh", options) - 1
  }

  static async rollTotal(formula, options = { showDice: HIDE_DICE }) {
    return (await RdDDice.roll(formula, options)).total;
  }

  static async roll(formula, options = { showDice: SHOW_DICE, rollMode: undefined }) {
    const roll = new Roll(RdDDice._formulaOrFake(formula, options));
    await roll.evaluate();
    await this.showDiceSoNice(roll, options);
    return roll;
  }

  static async rollOneOf(array) {
    if (array == undefined || array.length == 0) {
      return undefined;
    }
    const roll = await RdDDice.rollTotal(`1d${array.length}`);
    return array[roll - 1];
  }

  /* -------------------------------------------- */
  static async showDiceSoNice(roll, options) {
    if (options.showDice == HIDE_DICE || !game.modules.get("dice-so-nice")?.active || !game.dice3d) {
      return;
    }

    let { whisper, blind } = ChatUtility.applyRollMode({}, options?.rollMode);
    if (options.forceDiceResult?.total) {
      let terms = await RdDDice._getForcedTerms(options);
      if (terms) {
        await game.dice3d.show({ throws: [{ dice: terms }] })
        return;
      }
    }
    await game.dice3d.showForRoll(roll, game.user, true, whisper, blind);
  }

  static _formulaOrFake(formula, options) {
    if (options?.forceDiceResult?.total) {
      options.forceDiceResult.formula = formula;
      return options.forceDiceResult.total.toString()
    }
    return formula;
  }

  static async _getForcedTerms(options) {
    const total = options.forceDiceResult.total;
    switch (options.forceDiceResult.formula) {
      case '1d100':
        return terms1d100(total);
      case "2d10":
        return await terms2d10(total);
    }
    return undefined;

    function terms1d100(total) {
      const unites = total % 10;
      const dizaines = Math.floor(total / 10);
      return [
        { type: "d100", result: dizaines, resultLabel: dizaines * 10, vectors: [], options: {}, d100Result: total },
        { type: "d10", result: unites, resultLabel: unites, vectors: [], options: {}, d100Result: total }
      ];
    }

    async function terms2d10(total) {
      if (total > 20 || total < 2) { return undefined }
      const first = await RdDDice.fakeD10(Math.min(10, total - 1));
      const second = total - first;
      return [
        { type: "d10", result: first, resultLabel: first, vectors: [], options: {} },
        { type: "d10", result: second, resultLabel: second, vectors: [], options: {} }
      ];
    }
  }

  static async fakeD10(faces) {
    let roll = new Roll(`1d${faces}`);
    await roll.evaluate();
    return roll.total;
  }
}