import { log, LogTypes as LT } from '@Log4Deno';

import { ExecutedRoll, RollModifiers, RollSet, SumOverride } from 'artigen/dice/dice.d.ts';
import { generateRoll } from 'artigen/dice/randomRoll.ts';
import { getRollConf } from 'artigen/dice/getRollConf.ts';

import { loggingEnabled } from 'artigen/utils/logFlag.ts';
import { compareOrigIdx, compareRolls, compareRollsReverse } from 'artigen/utils/sortFuncs.ts';

import { flagRoll } from 'artigen/utils/diceFlagger.ts';
import { getLoopCount, loopCountCheck } from 'artigen/managers/loopManager.ts';
import { generateRollVals } from 'artigen/utils/rollValCounter.ts';

// roll(rollStr, modifiers) returns RollSet
// roll parses and executes the rollStr
export const executeRoll = (rollStr: string, modifiers: RollModifiers): ExecutedRoll => {
  /* Roll Capabilities
   * Deciphers and rolls a single dice roll set
   *
   * Check the README.md of this project for details on the roll options.  I gave up trying to keep three places updated at once.
   */

  // Make entire roll lowercase for ease of parsing
  rollStr = rollStr.toLowerCase();

  // Turn the rollStr into a machine readable rollConf
  const rollConf = getRollConf(rollStr, modifiers.customDiceShapes);

  // Roll the roll
  const rollSet: RollSet[] = [];
  /* Roll will contain objects of the following format:
   * 	{
   *	 	origIdx: 0,
   *		roll: 0,
   *		dropped: false,
   * 		rerolled: false,
   * 		exploding: false,
   * 		critHit: false,
   * 		critFail: false
   * 	}
   *
   * Each of these is defined as following:
   * 	{
   * 		origIdx: The original index of the roll
   *		roll: The resulting roll on this die in the set
   *		dropped: This die is to be dropped as it was one of the dy lowest dice
   * 		rerolled: This die has been rerolled as it matched rz, it is replaced by the very next die in the set
   * 		exploding: This die was rolled as the previous die exploded (was a crit hit)
   * 		critHit: This die matched csq[-u], max die value used if cs not used
   * 		critFail: This die rolled a nat 1, a critical failure
   * 	}
   */

  // Initialize a template rollSet to copy multiple times
  const getTemplateRoll = (): RollSet => ({
    type: rollConf.type,
    origIdx: 0,
    roll: 0,
    size: 0,
    dropped: false,
    rerolled: false,
    exploding: false,
    critHit: false,
    critFail: false,
    isComplex: rollConf.drop.on ||
      rollConf.keep.on ||
      rollConf.dropHigh.on ||
      rollConf.keepLow.on ||
      rollConf.critScore.on ||
      rollConf.critFail.on ||
      rollConf.exploding.on ||
      rollConf.success.on ||
      rollConf.fail.on,
    matchLabel: '',
    success: false,
    fail: false,
  });

  // Initial rolling, not handling reroll or exploding here
  for (let i = 0; i < rollConf.dieCount; i++) {
    loggingEnabled && log(LT.LOG, `${getLoopCount()} Handling ${rollConf.type} ${rollStr} | Initial rolling ${i} of ${JSON.stringify(rollConf)}`);
    // If loopCount gets too high, stop trying to calculate infinity
    loopCountCheck('executeRoll.ts - handling initial rolling');

    // Copy the template to fill out for this iteration
    const rolling = getTemplateRoll();
    // If maximizeRoll is on, set the roll to the dieSize, else if nominalRoll is on, set the roll to the average roll of dieSize, else generate a new random roll
    rolling.roll = generateRoll(rollConf, modifiers);
    rolling.size = rollConf.dieSize;
    // Set origIdx of roll
    rolling.origIdx = i;

    flagRoll(rollConf, rolling, modifiers.customDiceShapes);

    loggingEnabled && log(LT.LOG, `${getLoopCount()} Roll done ${JSON.stringify(rolling)}`);

    // Push the newly created roll and loop again
    rollSet.push(rolling);
  }

  // If needed, handle rerolling and exploding dice now
  if (rollConf.reroll.on || rollConf.exploding.on) {
    let minMaxOverride = 0;
    for (let i = 0; i < rollSet.length; i++) {
      loggingEnabled && log(LT.LOG, `${getLoopCount()} Handling ${rollConf.type} ${rollStr} | Handling rerolling and exploding ${JSON.stringify(rollSet[i])}`);
      // If loopCount gets too high, stop trying to calculate infinity
      loopCountCheck('executeRoll.ts - handling rerolling and exploding');

      // This big boolean statement first checks if reroll is on, if the roll is within the reroll range, and finally if ro is ON, make sure we haven't already rerolled the roll
      if (rollConf.reroll.on && rollConf.reroll.nums.includes(rollSet[i].roll) && (!rollConf.reroll.once || !rollSet[i ? i - 1 : i].rerolled)) {
        // If we need to reroll this roll, flag its been replaced and...
        rollSet[i].rerolled = true;

        // Copy the template to fill out for this iteration
        const newReroll = getTemplateRoll();
        newReroll.size = rollConf.dieSize;
        if (modifiers.maxRoll && !minMaxOverride) {
          // If maximizeRoll is on and we've entered the reroll code, dieSize is not allowed, determine the next best option and always return that
          mmMaxLoop: for (let m = rollConf.dieSize - 1; m > 0; m--) {
            loopCountCheck('executeRoll.ts - maximizeRoll');

            if (!rollConf.reroll.nums.includes(m)) {
              minMaxOverride = m;
              break mmMaxLoop;
            }
          }
        } else if (modifiers.minRoll && !minMaxOverride) {
          // If minimizeRoll is on and we've entered the reroll code, 1 is not allowed, determine the next best option and always return that
          mmMinLoop: for (let m = rollConf.dPercent.on ? 1 : 2; m <= rollConf.dieSize; m++) {
            loopCountCheck('executeRoll.ts - minimizeRoll');

            if (!rollConf.reroll.nums.includes(m)) {
              minMaxOverride = m;
              break mmMinLoop;
            }
          }
        }

        if (modifiers.maxRoll || modifiers.minRoll) {
          newReroll.roll = minMaxOverride;
        } else {
          // If nominalRoll is on, set the roll to the average roll of dieSize, otherwise generate a new random roll
          newReroll.roll = generateRoll(rollConf, modifiers);
        }

        flagRoll(rollConf, newReroll, modifiers.customDiceShapes);

        loggingEnabled && log(LT.LOG, `${getLoopCount()} Roll done ${JSON.stringify(newReroll)}`);

        // Slot this new roll in after the current iteration so it can be processed in the next loop
        rollSet.splice(i + 1, 0, newReroll);
      } else if (
        rollConf.exploding.on &&
        !rollSet[i].rerolled &&
        (rollConf.exploding.nums.length ? rollConf.exploding.nums.includes(rollSet[i].roll) : rollSet[i].critHit) &&
        (!rollConf.exploding.once || !rollSet[i].exploding)
      ) {
        // If we have exploding.nums set, use those to determine the exploding range, and make sure if !o is on, make sure we don't repeatedly explode
        // If it exploded, we keep both, so no flags need to be set

        // Copy the template to fill out for this iteration
        const newExplodingRoll = getTemplateRoll();
        // If maximizeRoll is on, set the roll to the dieSize, else if nominalRoll is on, set the roll to the average roll of dieSize, else generate a new random roll
        newExplodingRoll.roll = generateRoll(rollConf, modifiers);
        newExplodingRoll.size = rollConf.dieSize;
        // Always mark this roll as exploding
        newExplodingRoll.exploding = true;

        flagRoll(rollConf, newExplodingRoll, modifiers.customDiceShapes);

        loggingEnabled && log(LT.LOG, `${getLoopCount()} Roll done ${JSON.stringify(newExplodingRoll)}`);

        // Slot this new roll in after the current iteration so it can be processed in the next loop
        rollSet.splice(i + 1, 0, newExplodingRoll);
      }
    }
  }

  // If penetrating is on, do the decrements
  if (rollConf.exploding.penetrating) {
    for (const penRoll of rollSet) {
      loggingEnabled && log(LT.LOG, `${getLoopCount()} Handling ${rollConf.type} ${rollStr} | Handling penetrating explosions ${JSON.stringify(penRoll)}`);
      // If loopCount gets too high, stop trying to calculate infinity
      loopCountCheck('executeRoll.ts - penetrating explosion');

      // If the die was from an explosion, decrement it by one
      if (penRoll.exploding) {
        penRoll.roll--;
      }
    }
  }

  // Handle compounding explosions
  if (rollConf.exploding.compounding) {
    for (let i = 0; i < rollSet.length; i++) {
      loggingEnabled && log(LT.LOG, `${getLoopCount()} Handling ${rollConf.type} ${rollStr} | Handling compounding explosions ${JSON.stringify(rollSet[i])}`);
      // If loopCount gets too high, stop trying to calculate infinity
      loopCountCheck('executeRoll.ts - compounding explosion');

      // Compound the exploding rolls, including the exploding flag and
      if (rollSet[i].exploding) {
        rollSet[i - 1].roll = rollSet[i - 1].roll + rollSet[i].roll;
        rollSet[i - 1].exploding = true;
        rollSet[i - 1].critFail = rollSet[i - 1].critFail || rollSet[i].critFail;
        rollSet[i - 1].critHit = rollSet[i - 1].critHit || rollSet[i].critHit;
        rollSet.splice(i, 1);
        i--;
      }
    }
  }

  // If we need to handle the drop/keep flags
  if (rollConf.drop.on || rollConf.keep.on || rollConf.dropHigh.on || rollConf.keepLow.on) {
    // Count how many rerolled dice there are if the reroll flag was on
    let rerollCount = 0;
    if (rollConf.reroll.on) {
      for (let j = 0; j < rollSet.length; j++) {
        loopCountCheck('executeRoll.ts - count rerolls');

        loggingEnabled && log(LT.LOG, `${getLoopCount()} Handling ${rollConf.type} ${rollStr} | Setting originalIdx on ${JSON.stringify(rollSet[j])}`);
        rollSet[j].origIdx = j;

        if (rollSet[j].rerolled) {
          rerollCount++;
        }
      }
    }

    // Order the rolls from least to greatest (by RollSet.roll)
    rollSet.sort(compareRolls);

    // Determine how many valid rolls there are to drop from (may not be equal to dieCount due to exploding)
    const validRolls = rollSet.length - rerollCount;
    let dropCount = 0;

    // For normal drop and keep, simple subtraction is enough to determine how many to drop
    // Protections are in to prevent the dropCount from going below 0 or more than the valid rolls to drop
    if (rollConf.drop.on) {
      dropCount = rollConf.drop.count;
      if (dropCount > validRolls) {
        dropCount = validRolls;
      }
    } else if (rollConf.keep.on) {
      dropCount = validRolls - rollConf.keep.count;
      if (dropCount < 0) {
        dropCount = 0;
      }
    } // For inverted drop and keep, order must be flipped to greatest to least before the simple subtraction can determine how many to drop
    // Protections are in to prevent the dropCount from going below 0 or more than the valid rolls to drop
    else if (rollConf.dropHigh.on) {
      rollSet.reverse();
      dropCount = rollConf.dropHigh.count;
      if (dropCount > validRolls) {
        dropCount = validRolls;
      }
    } else if (rollConf.keepLow.on) {
      rollSet.reverse();
      dropCount = validRolls - rollConf.keepLow.count;
      if (dropCount < 0) {
        dropCount = 0;
      }
    }

    // Now its time to drop all dice needed
    let i = 0;
    while (dropCount > 0 && i < rollSet.length) {
      loopCountCheck('executeRoll.ts - dropping/keeping');

      loggingEnabled && log(LT.LOG, `${getLoopCount()} Handling ${rollConf.type} ${rollStr} | Dropping dice ${dropCount} ${JSON.stringify(rollSet[i])}`);
      // Skip all rolls that were rerolled
      if (!rollSet[i].rerolled) {
        rollSet[i].dropped = true;
        dropCount--;
      }
      i++;
    }

    // Finally, return the rollSet to its original order
    rollSet.sort(compareOrigIdx);
  }

  // Handle OVA dropping/keeping
  if (rollConf.type === 'ova') {
    const rollVals: Array<number> = generateRollVals(rollConf, rollSet, rollStr, false);

    // Find max value, using lastIndexOf to use the greatest die size max in case of duplicate maximums
    const maxRoll = rollVals.lastIndexOf(Math.max(...rollVals)) + 1;

    // Drop all dice that are not a part of the max
    for (const ovaRoll of rollSet) {
      loopCountCheck('executeRoll.ts - OVA');

      loggingEnabled &&
        log(LT.LOG, `${getLoopCount()} Handling ${rollConf.type} ${rollStr} | checking if this roll should be dropped ${ovaRoll.roll} | to keep: ${maxRoll}`);
      if (ovaRoll.roll !== maxRoll) {
        ovaRoll.dropped = true;
        ovaRoll.critFail = false;
        ovaRoll.critHit = false;
      }
    }
  }

  const sumOverride: SumOverride = {
    on: rollConf.match.returnTotal,
    value: 0,
  };
  if (rollConf.match.on) {
    const rollVals: Array<number> = generateRollVals(rollConf, rollSet, rollStr, true).map((count) => (count >= rollConf.match.minCount ? count : 0));
    const labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
    let labelIdx = 0;
    const rollLabels: Array<string> = rollVals.map((count) => {
      loopCountCheck('executeRoll.ts - matching');

      if (labelIdx >= labels.length) {
        throw new Error(`TooManyLabels_${labels.length}`);
      }

      if (count) {
        return labels[labelIdx++];
      }
      return '';
    });

    loggingEnabled && log(LT.LOG, `${getLoopCount()} Handling ${rollConf.type} ${rollStr} | current match state: ${rollVals} | ${rollLabels}`);

    // Apply labels
    for (const roll of rollSet) {
      loopCountCheck('executeRoll.ts - labeling matches');

      loggingEnabled && log(LT.LOG, `${getLoopCount()} Handling ${rollConf.type} ${rollStr} | trying to add a label to ${JSON.stringify(roll)}`);
      if (rollLabels[roll.roll - 1]) {
        roll.matchLabel = rollLabels[roll.roll - 1];
      } else if (rollConf.match.returnTotal) {
        roll.dropped = true;
      }
    }

    loggingEnabled && log(LT.LOG, `${getLoopCount()} Handling ${rollConf.type} ${rollStr} | labels added: ${JSON.stringify(rollSet)}`);

    if (rollConf.match.returnTotal) {
      sumOverride.value = rollVals.filter((count) => count !== 0).length;
    }
  }

  if (rollConf.sort.on) {
    rollSet.sort(rollConf.sort.direction === 'a' ? compareRolls : compareRollsReverse);
  }

  return {
    rollSet,
    sumOverride,
    countSuccessOverride: rollConf.success.on,
    countFailOverride: rollConf.fail.on,
  };
};
