// ============================================================================
//  luckydrop_test_cycle_TESTING.js   —   TESTING ONLY — NEVER SHIP IN A RELEASE
// ----------------------------------------------------------------------------
//  Drop-testing tool for ANY lucky block: forces what the block drops so every
//  entry can be inspected without RNG.
//
//  CONFIG (edit + /reload):
//    ENABLED      false = completely inert (safe default). true = active.
//    TARGET_BLOCK the block to control, e.g. "lucky:yummyluckyblock",
//                 "lucky:chaosluckyblock", "lucky:lucky_block"...
//    KEY_HINT     substring to find the block's key in the drops registry
//                 (usually the block path, e.g. "yummy").
//    LOCK_DROP    "" = CYCLE mode: each break gives the NEXT drop in file
//                 order (1/N, 2/N, ...). Non-empty = LOCK mode: every break
//                 gives the first drop whose definition contains this text
//                 (e.g. "cheesecake_a_la_merde" or "white_concrete hollow").
//
//  How it works: on the Forge BreakEvent (fired BEFORE the lucky block resolves
//  its drop in playerDestroy, which reads LuckyRegistry.getDrops().get(sourceId)
//  live), we narrow that registry list to a single drop for the imminent
//  resolution, then restore the full list one tick later. The engine builds its
//  own drop context — we only pick.
//
//  NOTES: survival only (drop pipeline skips creative). /reload after edits.
// ============================================================================

const ENABLED = false;
const TARGET_BLOCK = "lucky:yummyluckyblock";
const KEY_HINT = "yummy";
const LOCK_DROP = "";

let LR_KEY = null; // resolved LuckyRegistry.getDrops() key
let FULL = null;   // cached full List<WeightedDrop>
let cycleIdx = 0;  // rotation counter (cycle mode only)

BlockEvents.broken((event) => {
  let LuckyRegistry, ArrayList, drops, cur, pick, one, total, idxNow, it, k, str, label;
  if (!ENABLED) return;
  if (!event.block || ("" + event.block.id) != TARGET_BLOCK) return;

  try {
    LuckyRegistry = Java.loadClass("mod.lucky.common.LuckyRegistry");
    ArrayList = Java.loadClass("java.util.ArrayList");
    drops = LuckyRegistry.INSTANCE.getDrops();

    // resolve the registry key once (its namespacing may differ from the block id)
    if (LR_KEY == null || !drops.containsKey(LR_KEY)) {
      LR_KEY = null;
      it = drops.keySet().iterator();
      while (it.hasNext()) {
        k = it.next();
        if (("" + k).indexOf(KEY_HINT) >= 0) { LR_KEY = k; break; }
      }
    }
    if (LR_KEY == null) return;

    cur = drops.get(LR_KEY);
    // refresh the cache ONLY from a real (multi-entry) list, never from our singleton
    if (cur != null && cur.size() > 1) FULL = new ArrayList(cur);
    if (FULL == null || FULL.size() == 0) return;
    total = FULL.size();

    if (LOCK_DROP != "") {
      // locked mode: always the drop whose definition contains LOCK_DROP
      pick = null;
      it = FULL.iterator();
      while (it.hasNext()) {
        k = it.next();
        if (("" + k.getDropString()).indexOf(LOCK_DROP) >= 0) { pick = k; break; }
      }
      if (pick == null) { console.warn("[drop-test] LOCK_DROP not found: " + LOCK_DROP); return; }
    } else {
      idxNow = ((cycleIdx % total) + total) % total;
      cycleIdx = cycleIdx + 1;
      pick = FULL.get(idxNow);
    }

    // narrow the choice to this one drop for the imminent resolution...
    one = new ArrayList();
    one.add(pick);
    drops.put(LR_KEY, one);
    // ...then restore the full list next tick so the registry stays clean (best effort)
    try { event.server.scheduleInTicks(1, () => { drops.put(LR_KEY, FULL); }); } catch (ignore) {}

    if (event.player) {
      str = "" + pick.getDropString();
      if (str.length > 120) str = str.substring(0, 120) + "...";
      if (LOCK_DROP != "") {
        label = "[Drop Test] LOCKED -> " + str;
      } else {
        label = "[Drop Test] " + (idxNow + 1) + "/" + total
              + "  luck=" + pick.getLuck() + "  " + str;
      }
      event.player.tell(Text.gold(label));
    }
  } catch (err) {
    console.warn("[drop-test] " + err);
  }
});
