import * as mc from "@minecraft/server";
import * as ui from "@minecraft/server-ui";

const entries = {
  // misc
  start: {
    category: undefined,
    linksTo: ["entities", "items", "structures", "discord"],
  },
  // entities
  entities: {
    category: "start",
    linksTo: [/*"travellers", */ "wroughtnaut", "umvuthi", "umvuthana", "frostmaw", "sculptor", "bluff", "naga", "foliaath", "grottol", "lantern"],
  },
  frostmaw: {
    category: "entities",
    linksTo: ["ice_crystal"],
  },
  wroughtnaut: {
    category: "entities",
    linksTo: ["wroughtnaut_helmet", "wroughtnaut_axe"],
  },
  umvuthi: {
    category: "entities",
    linksTo: ["umvuthana", "sol_visage", "suns_blessing"],
  },
  umvuthana: {
    category: "entities",
    linksTo: ["umvuthi", "umvuthana_masks"],
  },
  naga: {
    category: "entities",
    linksTo: ["naga_fang", "naga_fang_dagger"],
  },
  foliaath: {
    category: "entities",
    linksTo: ["foliaath_seed"],
  },
  grottol: {
    category: "entities",
    linksTo: [],
  },
  lantern: {
    category: "entities",
    linksTo: [],
  },
  sculptor: {
    category: "entities",
    linksTo: ["earthrend_gauntlet", "sculptor_staff", "bluff"],
  },
  bluff: {
    category: "entities",
    linksTo: ["sculptor", "geomancy_armor"],
  },
  travellers: {
    category: "entities",
    linksTo: ["umvuthi", "sculptor", "wroughtnaut", "frostmaw"],
  },

  // items
  items: {
    category: "start",
    linksTo: [
      "ice_crystal",
      "wroughtnaut_helmet",
      "wroughtnaut_axe",
      "umvuthana_masks",
      "sol_visage",
      "suns_blessing",
      "naga_fang",
      "naga_fang_dagger",
      "blowgun",
      "foliaath_seed",
      "spear",
      "earthrend_gauntlet",
      "sculptor_staff",
      "geomancy_armor",
      "rake",
    ],
  },
  ice_crystal: {
    category: "items",
    linksTo: ["frostmaw"],
  },
  wroughtnaut_helmet: {
    category: "items",
    linksTo: ["wroughtnaut"],
  },
  wroughtnaut_axe: {
    category: "items",
    linksTo: ["wroughtnaut"],
  },
  umvuthana_masks: {
    category: "items",
    linksTo: ["umvuthana"],
  },
  sol_visage: {
    category: "items",
    linksTo: ["umvuthi", "umvuthana"],
  },
  suns_blessing: {
    category: "items",
    linksTo: ["umvuthi"],
  },
  naga_fang: {
    category: "items",
    linksTo: ["naga_fang_dagger", "blowgun", "naga"],
  },
  naga_fang_dagger: {
    category: "items",
    linksTo: ["naga_fang"],
  },
  blowgun: {
    category: "items",
    linksTo: ["naga_fang"],
  },
  foliaath_seed: {
    category: "items",
    linksTo: ["foliaath"],
  },
  spear: {
    category: "items",
    linksTo: ["spear"],
  },
  earthrend_gauntlet: {
    category: "items",
    linksTo: ["sculptor"],
  },
  sculptor_staff: {
    category: "items",
    linksTo: ["sculptor"],
  },
  geomancy_armor: {
    category: "items",
    linksTo: ["bluff"],
  },
  rake: {
    category: "items",
    linksTo: [],
  },
  // structures
  structures: {
    category: "start",
    linksTo: ["wroughtnaut_chamber", "umvuthana_grove", "monastery"],
  },
  wroughtnaut_chamber: {
    category: "structures",
    linksTo: ["wroughtnaut"],
  },
  umvuthana_grove: {
    category: "structures",
    linksTo: ["umvuthi", "umvuthana"],
  },
  monastery: {
    category: "structures",
    linksTo: ["bluff", "sculptor"],
  },

  discord: {
    category: "start",
    linksTo: [],
  },
};

export function openGuidebook(event) {
  openPage(event, "start");
  // const currentPage = event.source.getDynamicProperty("hf_mzs:current_page");
  // if (currentPage === undefined || entries[currentPage] === undefined) {
  //   event.source.setDynamicProperty("hf_mzs:current_page", "start");
  //   openPage(event, "start");
  //   return;
  // }
  // openPage(event, currentPage);
}
function openPage(event, page) {
  event.source.setDynamicProperty("hf_mzs:current_page", page);

  const title = `guidebook.hf_mzs.title_${page}`;
  const body = `guidebook.hf_mzs.body_${page}`;

  const entry = entries[page];

  const form = new ui.ActionFormData().title({ translate: title }).body({ translate: body, with: ["\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n"] });

  for (let i = 0; i < entry.linksTo.length; i++) {
    if (entry.linksTo[i] === "start") form.button({ translate: `guidebook.hf_mzs.button_${entry.linksTo[i]}` });
    else form.button({ translate: `guidebook.hf_mzs.button_${entry.linksTo[i]}` }, "textures/hf/mzs/guidebook/" + entry.linksTo[i]);
  }
  if (entry.category === "start") form.button({ translate: `guidebook.hf_mzs.button_${entry.category}` });
  else if (entry.category !== undefined) form.button({ translate: `guidebook.hf_mzs.button_${entry.category}` }, "textures/hf/mzs/guidebook/" + entry.category);

  form.show(event.source).then((response) => {
    if (entry.category !== undefined && response.selection === entry.linksTo.length) {
      openPage(event, entry.category);
      return;
    }
    if (response.selection >= 0) {
      openPage(event, entry.linksTo[response.selection]);
    }
  });
}

function disableCamera(event) {
  event.source.runCommand("/inputpermission set @s camera disabled");
  mc.system.runTimeout(() => {
    event.source.runCommand("/inputpermission set @s camera enabled");
  }, 20);
}
