import { CompendiumTable, CompendiumTableHelpers, SystemCompendiums } from "./settings/system-compendiums.js";

export class RdDRollTables {

  /* -------------------------------------------- */
  static async genericGetTableResult(tableName, toChat) {
    let table = RdDRollTables.getWorldTable(tableName) ?? (await RdDRollTables.getSystemTable(tableName));
    const draw = await table.draw({ displayChat: toChat, rollMode: "gm" });
    return draw.results.length > 0 ? draw.results[0] : undefined;
  }

  static getWorldTable(tableName) {
    return game.tables.find(table => table.name.toLowerCase() == tableName.toLowerCase());
  }

  static async getSystemTable(tableName) {
    const pack = SystemCompendiums.getPack("tables-diverses");
    const index = await pack.getIndex();
    const entry = index.find(e => e.name === tableName);
    return await pack.getDocument(entry.id);
  }

  /* -------------------------------------------- */
  static async drawItemFromRollTable(tableName, toChat = false) {
    const drawResult = await RdDRollTables.genericGetTableResult(tableName, toChat);
    const pack = game.packs.get(drawResult.documentCollection)
    return await pack.getDocument(drawResult.documentId)
  }

  /* -------------------------------------------- */
  static async drawTextFromRollTable(tableName, options = {}) {
    const drawResult = await RdDRollTables.genericGetTableResult(tableName, options.toChat);
    return drawResult.text;
  }

  /* -------------------------------------------- */
  static async getCompetence(toChat = false, rollMode = "gm") {
    if (toChat == 'liste') {
      return await RdDRollTables.listOrRoll('competences', 'Item', ['competence'], toChat, rollMode, it => 1);
    }
    else {
      return await RdDRollTables.drawItemFromRollTable("Détermination aléatoire de compétence", toChat);
    }
  }

  /* -------------------------------------------- */
  static async getSouffle(toChat = false, rollMode = "gm") {
    return await RdDRollTables.listOrRoll('souffles-de-dragon', 'Item', ['souffle'], toChat, rollMode);
  }

  /* -------------------------------------------- */
  static async getQueue(toChat = false, rollMode = "gm") {
    return await RdDRollTables.listOrRoll('queues-de-dragon', 'Item', ['queue'], toChat, rollMode);
  }

  static async getDesirLancinant(toChat = false, rollMode = "gm") {
    return await RdDRollTables.listOrRoll('queues-de-dragon', 'Item', ['queue'], toChat, rollMode,
      it => it.system.frequence,
      it => it.system.categorie == 'lancinant');
  }

  static async getIdeeFixe(toChat = false, rollMode = "gm") {
    return await RdDRollTables.listOrRoll('queues-de-dragon', 'Item', ['queue'], toChat, rollMode,
      it => it.system.frequence,
      it => it.system.categorie == 'ideefixe');
  }

  /* -------------------------------------------- */
  static async getTeteHR(toChat = false, rollMode = "gm") {
    return await RdDRollTables.listOrRoll('tetes-de-dragon-pour-haut-revants', 'Item', ['tete'], toChat, rollMode);
  }

  /* -------------------------------------------- */
  static async getTete(toChat = false, rollMode = "gm") {
    return await RdDRollTables.listOrRoll('tetes-de-dragon-pour-tous-personnages', 'Item', ['tete'], toChat, rollMode);
  }

  /* -------------------------------------------- */
  static async getOmbre(toChat = false, rollMode = "gm") {
    return await RdDRollTables.listOrRoll('ombres-de-thanatos', 'Item', ['ombre'], toChat, rollMode);
  }

  /* -------------------------------------------- */
  static async getTarot(toChat = true, rollMode = "gm") {
    return await RdDRollTables.listOrRoll('tarot-draconique', 'Item', ['tarot'], toChat, rollMode);
  }

  /* -------------------------------------------- */
  static async listOrRoll(compendium, type, subTypes, toChat, rollMode,
    itemFrequence = it => it.system.frequence,
    filter = it => true) {
    const table = new CompendiumTable(compendium, type, subTypes);
    if (toChat == 'liste') {
      return await table.toChatMessage(itemFrequence, filter);
    }
    const row = await table.getRandom(itemFrequence, filter);
    if (row) {
      await CompendiumTableHelpers.tableRowToChatMessage(row, type, { rollMode: rollMode });
      return row.document;
    }
    return undefined;
  }

  /* -------------------------------------------- */
  static async getMaladresse(options = { toChat: false, arme: false }) {
    return await RdDRollTables.drawTextFromRollTable(
      options.arme ? "Maladresse armé" : "Maladresses non armé",
      options)
  }

}
