import { ActionFormData } from "@minecraft/server-ui";

/**
 * Paged / multi-box PC UI (ActionFormData) that matches the server_form.json layout:
 * 0–5   party
 * 6–11  spacers
 * 12–13 arrows
 * 14–17 actions
 * 18–47 pc slots (30)
 *
 * IMPORTANT: We do NOT modify slotDataManager. We keep selectedSlot semantics compatible:
 * - party selection sets selectedSlot = 0..5
 * - pc selection sets selectedSlot = 10 + absolutePcIndex
 * This preserves existing withdraw/deposit logic that expects pcIndex = selectedSlot - 10.
 */
export function openPCPaged(utils, player, opts = {}) {
  const PAGE_SIZE = opts.pageSize ?? 30;
  const MAX_BOXES = opts.maxBoxes ?? 30; // 30 boxes = 900 slots

  const pcAll = utils.getPCPokemons(player) ?? [];
  const total = pcAll.length;

  let page = player.getDynamicProperty("pr:pc_page");
  if (typeof page !== "number" || page < 0) page = 0;

  const maxPage = Math.min(
    MAX_BOXES - 1,
    total === 0 ? 0 : Math.floor((total - 1) / PAGE_SIZE)
  );

  if (page > maxPage) page = maxPage;
  player.setDynamicProperty("pr:pc_page", page);

  const form = new ActionFormData();
  form.title(`Pokémon Storage PC — Box ${page + 1}/${maxPage + 1}`);

  // 0–5 Party buttons
  for (let i = 0; i < 6; i++) {
    const p = utils.getPokemonFromSlot(player, i);
    form.button(p ? `${p.name} (Lvl ${p.level})` : "Empty Slot");
  }

  // 6–11 spacers (keeps the 6x6 UI grid aligned)
  for (let i = 0; i < 6; i++) form.button(" ");

  // 12–13 arrows
  form.button("◀ Previous Box");
  form.button("Next Box ▶");

  // 14–17 actions
  form.button("Withdraw");
  form.button("Deposit");
  form.button("View Summary");
  form.button("Close");

  // 18–47 PC slots (paged)
  const start = page * PAGE_SIZE;
  const slice = pcAll.slice(start, start + PAGE_SIZE);

  for (let i = 0; i < PAGE_SIZE; i++) {
    const poke = slice[i];
    form.button(poke ? `${poke.name} (Lvl ${poke.level})` : "Empty Slot");
  }

  return form.show(player).then((res) => {
    if (res.canceled) return;

    const sel = res.selection;

    // arrows
    if (sel === 12 && page > 0) {
      player.setDynamicProperty("pr:pc_page", page - 1);
      return openPCPaged(utils, player, opts);
    }
    if (sel === 13 && page < maxPage) {
      player.setDynamicProperty("pr:pc_page", page + 1);
      return openPCPaged(utils, player, opts);
    }

    // actions
    if (sel === 14) {
      utils.withdrawPokemonFromPC(player);
      player.setDynamicProperty("pr:selectedSlot", -1);
      return openPCPaged(utils, player, opts);
    }
    if (sel === 15) {
      // Use our expanded-capacity deposit if installed; else fallback
      if (typeof utils.depositPokemonToPC_Expanded === "function") {
        utils.depositPokemonToPC_Expanded(player, MAX_BOXES * PAGE_SIZE);
      } else {
        utils.depositPokemonToPC(player);
      }
      player.setDynamicProperty("pr:selectedSlot", -1);
      return openPCPaged(utils, player, opts);
    }
    if (sel === 16) {
      // Your current addon doesn't implement summary here; keep behavior harmless
      player.setDynamicProperty("pr:selectedSlot", -1);
      return openPCPaged(utils, player, opts);
    }
    if (sel === 17) {
      player.setDynamicProperty("pr:selectedSlot", -1);
      return;
    }

    // party selection 0–5
    if (sel >= 0 && sel <= 5) {
      player.setDynamicProperty("pr:selectedSlot", sel);
      return openPCPaged(utils, player, opts);
    }

    // pc selection 18–47 => selectedSlot = 10 + absolute index (keeps withdraw logic)
    if (sel >= 18 && sel <= 47) {
      const absolutePcIndex = (page * PAGE_SIZE) + (sel - 18);
      player.setDynamicProperty("pr:selectedSlot", 10 + absolutePcIndex);
      return openPCPaged(utils, player, opts);
    }

    // spacers do nothing
  });
}
