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

world.afterEvents.itemUse.subscribe((data) => {
  if (data.itemStack.typeId == "arcedge_bap:guide_book") {
    mainPage(data.source);
  }
});

function mainPage(player) {
  let form = new ActionFormData();
  form.title("arcedge_bap.guide_book.title");
  form.body("");
  form.header("arcedge_bap.guide_book.mainPage.header");
  form.label("arcedge_bap.guide_book.mainPage.label1");
  form.divider();
  form.label("arcedge_bap.guide_book.mainPage.label2");
  form.divider();
  form.label("arcedge_bap.guide_book.mainPage.label3");
  form.button("arcedge_bap.guide_book.mainPage.button1");
  form.button("arcedge_bap.guide_book.mainPage.button2");
  form.button("arcedge_bap.guide_book.mainPage.button3");
  form.button("arcedge_bap.guide_book.mainPage.button4");
  form.button("arcedge_bap.guide_book.mainPage.button5");
  form.button("arcedge_bap.guide_book.mainPage.button6");
  form.divider();
  form.label("arcedge_bap.guide_book.changelog");
  form.show(player).then((response) => {
    if (response.selection == 0) craftingPage(player);
    if (response.selection == 1) dnaPage(player);
    if (response.selection == 2) abilitiesPage(player);
    if (response.selection == 3) buildPetPage(player);
    if (response.selection == 4) traderPage(player);
    if (response.selection == 5) healBreedPage(player);
  });
}

function craftingPage(player) {
  let form = new ActionFormData();
  form.title("arcedge_bap.guide_book.title");
  form.body("");
  form.header("arcedge_bap.guide_book.craftingPage.header");
  form.label("arcedge_bap.guide_book.craftingPage.label1");
  form.divider();
  form.label("arcedge_bap.guide_book.craftingPage.label2");
  form.divider();
  form.label("arcedge_bap.guide_book.craftingPage.label3");
  form.divider();
  form.label("arcedge_bap.guide_book.backLabel");
  form.button("arcedge_bap.guide_book.backButton");
  form.show(player).then((response) => {
    if (response.selection == 0) mainPage(player);
  });
}

function dnaPage(player) {
  let form = new ActionFormData();
  form.title("arcedge_bap.guide_book.title");
  form.body("");
  form.header("arcedge_bap.guide_book.dnaPage.header");
  form.label("arcedge_bap.guide_book.dnaPage.label1");
  form.divider();
  form.label("arcedge_bap.guide_book.backLabel");
  form.button("arcedge_bap.guide_book.backButton");
  form.show(player).then((response) => {
    if (response.selection == 0) mainPage(player);
  });
}

function abilitiesPage(player) {
  let form = new ActionFormData();
  form.title("arcedge_bap.guide_book.title");
  form.body("");
  form.header("arcedge_bap.guide_book.abilitiesPage.header");
  form.label("arcedge_bap.guide_book.abilitiesPage.label1");
  form.divider();
  form.label("arcedge_bap.guide_book.abilitiesPage.label6");
  form.divider();
  form.label("arcedge_bap.guide_book.abilitiesPage.label2");
  buddyList.forEach((element) => {
    const displayName = element
      .replace(/_/g, " ")
      .split(" ")
      .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
      .join(" ");

    form.button(
      {
        translate: "arcedge_bap.guide_book.abilitiesPage.ability",
        with: [displayName],
      },
      "textures/arcedge/bap/icon/" + element + "_head"
    );
  });
  form.divider();
  form.label("arcedge_bap.guide_book.backLabel");
  form.button("arcedge_bap.guide_book.backButton");
  form.show(player).then((response) => {
    if (response.selection < 16) buddyAbilityPage(player, response.selection);
    if (response.selection == 16) mainPage(player);
  });
}

function buddyAbilityPage(player, index) {
  const displayName = buddyList[index]
    .replace(/_/g, " ")
    .split(" ")
    .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
    .join(" ");
  let form = new ActionFormData();
  form.title("arcedge_bap.guide_book.title");
  form.body("");
  form.header({
    translate: "arcedge_bap.guide_book.abilitiesPage.ability",
    with: ["§e" + displayName],
  });
  form.label("arcedge_bap.guide_book.abilitiesPage.label4");
  form.label("arcedge_bap.guide_book.ability." + [index + 1]);
  form.divider();
  form.label("arcedge_bap.guide_book.abilitiesPage.label5");
  form.button("arcedge_bap.guide_book.backButton");
  form.show(player).then((response) => {
    if (response.selection == 0) {
      abilitiesPage(player);
    }
  });
}

function buildPetPage(player) {
  let form = new ActionFormData();
  form.title("arcedge_bap.guide_book.title");
  form.body("");
  form.header("arcedge_bap.guide_book.buildPetPage.header");
  form.label("arcedge_bap.guide_book.buildPetPage.label1");
  form.divider();
  form.label("arcedge_bap.guide_book.backLabel");
  form.button("arcedge_bap.guide_book.backButton");
  form.show(player).then((response) => {
    if (response.selection == 0) mainPage(player);
  });
}

function traderPage(player) {
  let form = new ActionFormData();
  form.title("arcedge_bap.guide_book.title");
  form.body("");
  form.header("arcedge_bap.guide_book.traderPage.header");
  form.label("arcedge_bap.guide_book.traderPage.label1");
  form.divider();
  form.label("arcedge_bap.guide_book.traderPage.label2");
  form.divider();
  form.label("arcedge_bap.guide_book.backLabel");
  form.button("arcedge_bap.guide_book.backButton");
  form.show(player).then((response) => {
    if (response.selection == 0) mainPage(player);
  });
}

function healBreedPage(player) {
  let form = new ActionFormData();
  form.title("arcedge_bap.guide_book.title");
  form.body("");
  form.header("arcedge_bap.guide_book.healBreedPage.header");
  form.label("arcedge_bap.guide_book.healBreedPage.label1");
  form.divider();
  form.label("arcedge_bap.guide_book.healBreedPage.label2");
  form.divider();
  form.label("arcedge_bap.guide_book.backLabel");
  form.button("arcedge_bap.guide_book.backButton");
  form.show(player).then((response) => {
    if (response.selection == 0) mainPage(player);
  });
}

const buddyList = [
  "axolotl",
  "dolphin",
  "elephant",
  "flamingo",
  "frog",
  "hermit_crab",
  "lion",
  "lizard",
  "mouse",
  "panther",
  "parrot",
  "polar_bear",
  "teddy_bear",
  "tiger",
  "turtle",
  "wolf",
];
