console.info("[SOCIETY] coinUi.js loaded");

const Numismatics = Java.loadClass(
  "dev.ithundxr.createnumismatics.Numismatics"
);

const getPlayerBalance = (player) => {
  const playerAccount = Numismatics.BANK.accounts.get(player.getUuid());
  return playerAccount ? playerAccount.getBalance() : 0;
};

PlayerEvents.tick((e) => {
  const player = e.player;
  const curios = player.nbt.ForgeCaps["curios:inventory"];
  if (player.age % 100 == 0 && curios.toString().includes("society:bank_meter")) {
    const balance = getPlayerBalance(player);
    const balanceText = balance
      .toString()
      .replace(/\B(?=(\d{3})+(?!\d))/g, ",");

    player.paint({
      coinDisplayDropShadow: {
        type: "text",
        x: 16.5,
        z: -1,
        y: 16,
        text: balanceText,
        color: "#000000",
        alignX: "left",
        alignY: "top",
      },
    });
    player.paint({
      coinDisplay: {
        type: "text",
        x: 1.5,
        y: 15,
        text: `:coin: ${balanceText}`,
        color: "#FFAA00",
        alignX: "left",
        alignY: "top",
      },
    });
  } else if (!curios.toString().includes("society:bank_meter")) {
    player.paint({
      coinDisplay: {
        type: "text",
        x: 1.5,
        y: 15,
        text: "",
        color: "#FFAA00",
        alignX: "left",
        alignY: "top",
      },
    });
    player.paint({
      coinDisplayDropShadow: {
        type: "text",
        x: 1.5,
        y: 15,
        text: "",
        color: "#FFAA00",
        alignX: "left",
        alignY: "top",
      },
    });
    player.paint({ coinDisplay: { remove: true } });
    player.paint({ coinDisplayDropShadow: { remove: true } });
  }
});
