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

world.beforeEvents.itemUse.subscribe((data) => {
  let player = data.source;
  if (data.itemStack.typeId == "txd_pirate1:guide_book")
    system.run(() => txd_main(player));
  
  function txd_main() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    const form = new ActionFormData()
    .title({ translate: "txd_pirate1.guide.main.title" })
    .body({ translate: "txd_pirate1.guide.main.body", with: ["\n"] })
    .button(
      { translate: "txd_pirate1.guide.getstarted.button" },
      "textures/tomaxed/pirate1/ui/getstarted"
    )
    .button(
      { translate: "txd_pirate1.guide.crafts.button" },
      "textures/tomaxed/pirate1/ui/crafts"
    )
    .button(
      { translate: "txd_pirate1.guide.ships.button" },
      "textures/tomaxed/pirate1/ui/ships"
    )
    .button(
      { translate: "txd_pirate1.guide.parrots.button" },
      "textures/tomaxed/pirate1/ui/parrots"
    )
    .button(
      { translate: "txd_pirate1.guide.captain.button" },
      "textures/tomaxed/pirate1/ui/captain"
    )
    .button(
      { translate: "txd_pirate1.guide.exit.button" },
      "textures/tomaxed/pirate1/ui/exit"
    );
    form.show(player).then((r) => {
      if (r.selection == 0) getstarted(player);
      if (r.selection == 1) crafts(player);
      if (r.selection == 2) ships(player);
      if (r.selection == 3) parrots(player);
      if (r.selection == 4) captain(player);
    });
  }
  //get started section
  function getstarted() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    const form = new ActionFormData()
    .title({ translate: "txd_pirate1.guide.getstarted.title" })
    .body({ translate: "txd_pirate1.guide.getstarted.body", with: ["\n"] })
    .button(
      { translate: "txd_pirate1.guide.getstarted_step1.button" },
      "textures/tomaxed/pirate1/ui/getstarted_1"
    )
    .button(
      { translate: "txd_pirate1.guide.getstarted_step2.button" },
      "textures/tomaxed/pirate1/ui/getstarted_2"
    )
    .button(
      { translate: "txd_pirate1.guide.getstarted_step3.button" },
      "textures/tomaxed/pirate1/ui/getstarted_3"
    )
    .button(
      { translate: "txd_pirate1.guide.back.button" },
      "textures/tomaxed/pirate1/ui/back"
    );
    form.show(player).then((r) => {
      if (r.selection == 0) getstarted_step1(player);
      if (r.selection == 1) getstarted_step2(player);
      if (r.selection == 2) getstarted_step3(player);
      if (r.selection == 3) txd_main(player);
    });
  }
  //Crafts section
  function crafts() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    const form = new ActionFormData()
    .title({ translate: "txd_pirate1.guide.crafts.title" })
    .body({ translate: "txd_pirate1.guide.crafts.body", with: ["\n"] })
    .button(
      { translate: "txd_pirate1.guide.getcraftbook.button" },
      "textures/tomaxed/pirate1/ui/getcraftbook"
    )
    .button(
      { translate: "txd_pirate1.guide.back.button" },
      "textures/tomaxed/pirate1/ui/back"
    );
    form.show(player).then((r) => {
      if (r.selection == 0) getcraftbook(player);
      if (r.selection == 1) txd_main(player);
    });
  }
  //ships section
  function ships() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    const form = new ActionFormData()
    .title({ translate: "txd_pirate1.guide.ships.title" })
    .body({ translate: "txd_pirate1.guide.ships.body", with: ["\n"] })
    .button(
      { translate: "txd_pirate1.guide.ships_howtouse.button" },
      "textures/tomaxed/pirate1/ui/ships_howtouse"
    )
    .button(
      { translate: "txd_pirate1.guide.ships_sloop.button" },
      "textures/tomaxed/pirate1/ui/ships_sloop"
    )
    .button(
      { translate: "txd_pirate1.guide.ships_brigantine.button" },
      "textures/tomaxed/pirate1/ui/ships_brigantine"
    )
    .button(
      { translate: "txd_pirate1.guide.ships_skeletonpirate.button" },
      "textures/tomaxed/pirate1/ui/ships_skeletonpirate"
    )
    .button(
      { translate: "txd_pirate1.guide.back.button" },
      "textures/tomaxed/pirate1/ui/back"
    );
    form.show(player).then((r) => {
      if (r.selection == 0) ships_howtouse(player);
      if (r.selection == 1) ships_sloop(player);
      if (r.selection == 2) ships_brigantine(player);
      if (r.selection == 3) ships_skeletonpirate(player);
      if (r.selection == 4) txd_main(player);
    });
  }
  //parrots section
  function parrots() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    const form = new ActionFormData()
    .title({ translate: "txd_pirate1.guide.parrots.title" })
    .body({ translate: "txd_pirate1.guide.parrots.body", with: ["\n"] })
    .button(
      { translate: "txd_pirate1.guide.back.button" },
      "textures/tomaxed/pirate1/ui/back"
    );
    form.show(player).then((r) => {
      if (r.selection == 0) txd_main(player);
    });
  }
  //captain and items
  function captain() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    const form = new ActionFormData()
    .title({ translate: "txd_pirate1.guide.captain.title" })
    .body({ translate: "txd_pirate1.guide.captain.body", with: ["\n"] })
    .button(
      { translate: "txd_pirate1.guide.captain1.button" },
      "textures/tomaxed/pirate1/ui/captain1"
    )
    .button(
      { translate: "txd_pirate1.guide.captain2.button" },
      "textures/tomaxed/pirate1/ui/captain2"
    )
    .button(
      { translate: "txd_pirate1.guide.back.button" },
      "textures/tomaxed/pirate1/ui/back"
    );
    form.show(player).then((r) => {
      if (r.selection == 0) captain1(player);
      if (r.selection == 1) captain2(player);
      if (r.selection == 2) txd_main(player);
    });
  }
  
  //getting started - step 1
  function getstarted_step1() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    const form = new ActionFormData()
    .title({ translate: "txd_pirate1.guide.getstarted_step1.title" })
    .body({ translate: "txd_pirate1.guide.getstarted_step1.body", with: ["\n"] })
    .button(
      { translate: "txd_pirate1.guide.getcraftbook.button" },
      "textures/tomaxed/pirate1/ui/getcraftbook"
    )
    .button(
      { translate: "txd_pirate1.guide.back.button" },
      "textures/tomaxed/pirate1/ui/back"
    );
    form.show(player).then((r) => {
      if (r.selection == 0) getcraftbook(player);
      if (r.selection == 1) getstarted(player);
    });
  }
  //getting started - step 2
  function getstarted_step2() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    const form = new ActionFormData()
    .title({ translate: "txd_pirate1.guide.getstarted_step2.title" })
    .body({ translate: "txd_pirate1.guide.getstarted_step2.body", with: ["\n"] })
    .button(
      { translate: "txd_pirate1.guide.back.button" },
      "textures/tomaxed/pirate1/ui/back"
    );
    form.show(player).then((r) => {
      if (r.selection == 0) getstarted(player);
    });
  }
  //getting started - step 3
  function getstarted_step3() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    const form = new ActionFormData()
    .title({ translate: "txd_pirate1.guide.getstarted_step3.title" })
    .body({ translate: "txd_pirate1.guide.getstarted_step3.body", with: ["\n"] })
    .button(
      { translate: "txd_pirate1.guide.back.button" },
      "textures/tomaxed/pirate1/ui/back"
    );
    form.show(player).then((r) => {
      if (r.selection == 0) getstarted(player);
    });
  }
  //captain 1
  function captain1() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    const form = new ActionFormData()
    .title({ translate: "txd_pirate1.guide.captain1.title" })
    .body({ translate: "txd_pirate1.guide.captain1.body", with: ["\n"] })
    .button(
      { translate: "txd_pirate1.guide.back.button" },
      "textures/tomaxed/pirate1/ui/back"
    );
    form.show(player).then((r) => {
      if (r.selection == 0) captain(player);
    });
  }
  //captain 2
  function captain2() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    const form = new ActionFormData()
    .title({ translate: "txd_pirate1.guide.captain2.title" })
    .body({ translate: "txd_pirate1.guide.captain2.body", with: ["\n"] })
    .button(
      { translate: "txd_pirate1.guide.back.button" },
      "textures/tomaxed/pirate1/ui/back"
    );
    form.show(player).then((r) => {
      if (r.selection == 0) captain(player);
    });
  }
  //ships - howtouse
  function ships_howtouse() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    const form = new ActionFormData()
    .title({ translate: "txd_pirate1.guide.ships_howtouse.title" })
    .body({ translate: "txd_pirate1.guide.ships_howtouse.body", with: ["\n"] })
    .button(
      { translate: "txd_pirate1.guide.back.button" },
      "textures/tomaxed/pirate1/ui/back"
    );
    form.show(player).then((r) => {
      if (r.selection == 0) ships(player);
    });
  }
  //ships - sloop
  function ships_sloop() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    const form = new ActionFormData()
    .title({ translate: "txd_pirate1.guide.ships_sloop.title" })
    .body({ translate: "txd_pirate1.guide.ships_sloop.body", with: ["\n"] })
    .button(
      { translate: "txd_pirate1.guide.back.button" },
      "textures/tomaxed/pirate1/ui/back"
    );
    form.show(player).then((r) => {
      if (r.selection == 0) ships(player);
    });
  }
  //ships - brigantine
  function ships_brigantine() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    const form = new ActionFormData()
    .title({ translate: "txd_pirate1.guide.ships_brigantine.title" })
    .body({ translate: "txd_pirate1.guide.ships_brigantine.body", with: ["\n"] })
    .button(
      { translate: "txd_pirate1.guide.back.button" },
      "textures/tomaxed/pirate1/ui/back"
    );
    form.show(player).then((r) => {
      if (r.selection == 0) ships(player);
    });
  }
//ships - skeletonpirate
function ships_skeletonpirate() {
  player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
  const form = new ActionFormData()
  .title({ translate: "txd_pirate1.guide.ships_skeletonpirate.title" })
  .body({ translate: "txd_pirate1.guide.ships_skeletonpirate.body", with: ["\n"] })
  .button(
    { translate: "txd_pirate1.guide.back.button" },
    "textures/tomaxed/pirate1/ui/back"
  );
  form.show(player).then((r) => {
    if (r.selection == 0) ships(player);
  });
}

  //craft book give
  function getcraftbook() {
    player.playSound("item.book.page_turn", { pitch: 1.0, volume: 1.0 });
    player.runCommand("give @s txd_pirate1:craft_book01");
  }
});
