import { RMFRPUtility } from "./rmfrp-utility.js";
import { RMFRP_ATTACK_TABLES } from "./rmfrp-attack-tables.js";

export class RMFRPProcessTable {

  constructor() {
    // Load a reference to the rmfrp-tables.json file
    this.rmfrpTables = RMFRP_ATTACK_TABLES
    // Loop thru the tables and create a definition object, with key, name and min/ax value of each table
    this.rmfrpTablesDef = {}

    for (let table in this.rmfrpTables) {
      let tableData = this.rmfrpTables[table]
      // Search min and max values in the tableData
      let min = Number.MAX_SAFE_INTEGER;
      let minKey = "";
      let max = Number.MIN_SAFE_INTEGER;
      let maxKey = "";
      for (let resultData of tableData) {
        let element = resultData["result"];
        if (Number(element.result)) {
          let value = Number(element.result)
          if (value > max) {
            max = value
            maxKey = element.result
          }
          if (value < min) {
            min = value
            minKey = element.result
          } else {
            let range = element.result.split("-");
            if (Number(range[0]) > max) {
              max = Number(range[0])
              maxKey = element.result
            }
            if (Number(range[1]) < min) {
              min = Number(range[1])
              minKey = element.result
            }
          }
        }
      }
      this.rmfrpTablesDef[table] = {
        key: table,
        name: RMFRPUtility.capitalizeFirstLetters(table.replace(/_/g, " ")),
        min: min,
        minKey: minKey,
        max: max,
        maxKey: maxKey
      };
    }
    // Sort rmfrpTablesDef by name 
    this.rmfrpTablesDef = Object.values(this.rmfrpTablesDef).sort((a, b) => a.name.localeCompare(b.name))
  }

  getTableDef() {
    return this.rmfrpTablesDef;
  }

  // Get roll result from table
  getAttackRollResult(tableKey, roll, armorValue) {
    roll = Number(roll);
    if (isNaN(roll)) {
      return undefined;
    }
    let table = this.rmfrpTables[tableKey]
    let tableDef = this.rmfrpTablesDef[tableKey]
    if (!table || !tableDef) {
      return undefined;
    }

    // Check min and max values
    if (roll < tableDef.min) {
      // return the min value
      return table.find((element) => {
        let elem = element.result == tableDef.minKey;
        return elem[String(armorValue)];
      });
    }
    if (roll > tableDef.max) {
      // return the min value
      return table.find((element) => {
        let elem = element.result == tableDef.maxKey;
        return elem[String(armorValue)];
      });
    }

    return table.find((element) => {
      if (Number(element.result) && Number(element.result) == Number(roll)) {
        return element[String(armorValue)];
      } else {
        // Split the result into a range
        let range = element.result.split("-");
        if (Number(roll) >= Number(range[0]) && Number(roll) <= Number(range[1])) {
          return element[String(armorValue)];
        }
      }
    });
  }

  getFumbleDef() {
    let fumbles = []
    for (let key in CONFIG.rmfrp.fumbles) {
      fumbles.push({ key: key, name: RMFRPUtility.capitalizeFirstLetters(key.replace(/_/g, " ")) })
    }
    return fumbles
  }

  getCriticalDef() {
    let criticals = []
    for (let key in CONFIG.rmfrp.criticals) {
      criticals.push({ key: key, name: RMFRPUtility.capitalizeFirstLetters(key.replace(/_/g, " ")) })
    }
    return criticals
  }

}
