
// ===== Poké Revival PC PATCH =====
// Drop-in helper to add paged PC + Summary without rewriting your whole file.
// HOW TO USE:
// 1) Place this file next to pokemonUtils.js
// 2) Import and wire the exported functions as noted at the bottom.
//
// This file assumes your existing PokemonUtils class methods exist:
// - getPokemonFromSlot
// - getPCPokemons
// - withdrawPokemonFromPC
// - depositPokemonToPC
// - movePokemonFromPC
// - openPC (will be overridden if you choose)
//
// You can also copy-paste the functions below directly into pokemonUtils.js
// if you prefer a full replacement.

import { ActionFormData } from "@minecraft/server-ui";
import { slotDataManager } from "./slotDataManager.js";
import { buildOffensiveSummary, formatTyping } from "./typeEffectiveness.js";

export function showPokemonSummary(utils, player) {
  const kind = player.getDynamicProperty("pr_rp:selected_kind");

  let pokemonData = null;

  if (kind === "party") {
    const slot = player.getDynamicProperty("pr_rp:selected_party");
    pokemonData = utils.getPokemonFromSlot(player, slot);
  }

  if (kind === "pc") {
    const pcIndex = player.getDynamicProperty("pr_rp:selected_pc");
    pokemonData = slotDataManager.getPokemonFromPC(player, pcIndex);
  }

  if (!pokemonData) {
    player.sendMessage("No Pokémon selected.");
    return;
  }

  const form = new ActionFormData();
  form.title(`${pokemonData.name} — Summary`);

  const typingText = formatTyping(pokemonData.types);
  const offensive = buildOffensiveSummary(pokemonData.types);

  form.body(
`Typing: ${typingText}
Strong Against: ${offensive.strongAgainst}
Weak Against: ${offensive.weakTo}
Immune: ${offensive.immuneTo}

Level: ${pokemonData.level}
HP: ${pokemonData.health}
Attack: ${pokemonData.attack}
Defense: ${pokemonData.defense}
Speed: ${pokemonData.speed}
Sp. Atk: ${pokemonData.spAttack}
Sp. Def: ${pokemonData.spDefense}

Moves:
• ${pokemonData.move1 || "-"}
• ${pokemonData.move2 || "-"}
• ${pokemonData.move3 || "-"}
• ${pokemonData.move4 || "-"}`
  );
  form.button("Back");
  form.show(player).then(() => {
    utils.openPC(player);
  });
}

export function openPCPaged(utils, player) {
  const formPC = new ActionFormData();

  const pageSize = 30;
  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 = total === 0 ? 0 : Math.floor((total - 1) / pageSize);
  if (page > maxPage) page = maxPage;
  player.setDynamicProperty("pr:pc_page", page);

  // This title is what the UI label reads to show "Box 1/3"
  formPC.title(`Pokémon Storage PC — Box ${page + 1}/${maxPage + 1}`);

  // Party slots (0–5)
  for (let i = 0; i < 6; i++) {
    const pokemon = utils.getPokemonFromSlot(player, i);
    if (pokemon) {
      formPC.button(`${pokemon.name} (Lvl ${pokemon.level})`);
    } else {
      formPC.button("Empty Slot");
    }
  }

  // Spacer buttons (6–11) to align with the 6x6 visual grid
  for (let i = 0; i < 6; i++) {
    formPC.button(" ");
  }

  const start = page * pageSize;
  const pcSlice = pcAll.slice(start, start + pageSize);

  // Arrows (12–13) — text here becomes the hover tooltip in server_form
  formPC.button("◀ Previous Box");
  formPC.button("Next Box ▶");

  // Actions (14–17)
  formPC.button("Withdraw");
  formPC.button("Deposit");
  formPC.button("View Summary");
  formPC.button("Close");

  // PC slots (18–47)
  for (let i = 0; i < pageSize; i++) {
    const poke = pcSlice[i];
    if (poke) formPC.button(`${poke.name} (Lvl ${poke.level})`);
    else formPC.button("Empty Slot");
  }

  formPC.show(player).then(res => {
    if (res.canceled) return;
    const i = res.selection;

    if (i === 12 && page > 0) {
      player.setDynamicProperty("pr:pc_page", page - 1);
      return openPCPaged(utils, player);
    }
    if (i === 13 && page < maxPage) {
      player.setDynamicProperty("pr:pc_page", page + 1);
      return openPCPaged(utils, player);
    }
    if (i === 14) {
      utils.withdrawPokemonFromPC(player);
      return openPCPaged(utils, player);
    }
    if (i === 15) {
      utils.depositPokemonToPC(player);
      return openPCPaged(utils, player);
    }
    if (i === 16) {
      return showPokemonSummary(utils, player);
    }
    if (i === 17) return;

    if (i >= 0 && i <= 5) {
      player.setDynamicProperty("pr_rp:selected_kind", "party");
      player.setDynamicProperty("pr_rp:selected_party", i);
      player.setDynamicProperty("pr_rp:selected_pc", -1);
      return openPCPaged(utils, player);
    }

    if (i >= 18 && i <= 47) {
      const pcIndex = (page * pageSize) + (i - 18);
      player.setDynamicProperty("pr_rp:selected_kind", "pc");
      player.setDynamicProperty("pr_rp:selected_pc", pcIndex);
      player.setDynamicProperty("pr_rp:selected_party", -1);
      return openPCPaged(utils, player);
    }
  });
}


// ===== WIRING =====
// In pokemonUtils.js:
// 1) import { openPCPaged, showPokemonSummary } from "./pokemonUtils_PC_PATCH.js";
// 2) replace openPC(player) { openPCPaged(this, player); }
// 3) remove old Summary handler and let this handle it.
