import { ActionFormData, MessageFormData, ModalFormData } from "@minecraft/server-ui";
import { mobs, blocks, morphs } from "./data";
import { transform, getMorph, setMorphHelmet, getMorphUnlocked, lockMorph } from "./main";
import { actionBar, langText } from "./util";
import { failMorphLocked, successDisableUnlockAllMorphs, successLockAllMorphs, successUnlockAllMorphs } from "./player_communication";
function openMainMenu(player) {
  const mainMenuForm = new ActionFormData();
  const morph = getMorph(player);
  mainMenuForm.title(langText("squaredreams_bam.ui.main_menu.title"));
  mainMenuForm.body(langText("squaredreams_bam.ui.main_menu.body"));
  mainMenuForm.button(langText("squaredreams_bam.ui.main_menu.button_morph_entity"), "textures/sd/bam/ui/mobs");
  mainMenuForm.button(langText("squaredreams_bam.ui.main_menu.button_morph_block"), "textures/sd/bam/ui/blocks");
  if (!blocks.includes(morph))
    mainMenuForm.button(langText("squaredreams_bam.ui.main_menu.button_morph_options"), "textures/sd/bam/ui/variants");
  mainMenuForm.button(langText("squaredreams_bam.ui.main_menu.button_settings"), "textures/sd/bam/ui/settings");
  mainMenuForm.show(player).then((r) => {
    if (r.canceled)
      return;
    const unlockToggle = player.getDynamicProperty("squaredreams_bam:toggle.unlock_all");
    const nametagToggle = player.getDynamicProperty("squaredreams_bam:toggle.nametag_off");
    switch (r.selection) {
      case 0:
        openMorphEntitySelection(player);
        break;
      case 1:
        openMorphBlockSelection(player);
        break;
      case 2:
        if (blocks.includes(morph)) {
          openSettingsMenu(player, unlockToggle, nametagToggle);
          break;
        }
        const isBaby = player.getDynamicProperty("squaredreams_bam:toggle.baby");
        const variant = player.getDynamicProperty(`squaredreams_bam:variant.${morph.id}`);
        openMorphOptions(player, isBaby, variant);
        break;
      case 3:
        openSettingsMenu(player, unlockToggle, nametagToggle);
      default:
        break;
    }
  });
}
function openMorphEntitySelection(player, validMobs = mobs, filterString = "") {
  const morphSelectionForm = new ActionFormData();
  validMobs = validMobs.filter((x) => x.id !== "player");
  if (player.getDynamicProperty("squaredreams_bam:toggle.locked") ?? false)
    validMobs = validMobs.filter((x) => getMorphUnlocked(player, x));
  morphSelectionForm.title(langText("squaredreams_bam.ui.entity_morph.title"));
  if (filterString === "")
    morphSelectionForm.body(langText("squaredreams_bam.ui.entity_morph.body", ["\n"]));
  else
    morphSelectionForm.body(langText("squaredreams_bam.ui.entity_morph.body_filtered", ["\n", filterString]));
  morphSelectionForm.button(langText("squaredreams_bam.ui.morph.search_button"), "textures/sd/bam/ui/search");
  morphSelectionForm.button(langText(`squaredreams_bam.ui.button.morph.player.name`), `textures/sd/bam/ui/mobs/player`);
  validMobs.forEach((x) => {
    if (x.id === "player")
      return;
    if (getMorphUnlocked(player, x)) {
      morphSelectionForm.button(x.name, `textures/sd/bam/ui/mobs/${x.id}`);
    } else {
      morphSelectionForm.button(langText("squaredreams_bam.ui.button.morph.locked"), `textures/sd/bam/ui/locked`);
    }
  });
  morphSelectionForm.show(player).then((r) => {
    if (r.canceled || r.selection == void 0)
      return;
    if (r.selection === 0) {
      openMorphEntityFilter(player, filterString);
      return;
    }
    if (r.selection === 1) {
      transform(player, mobs[0]);
      return;
    }
    if (getMorphUnlocked(player, validMobs[r.selection - 2])) {
      transform(player, validMobs[r.selection - 2]);
    } else {
      failMorphLocked(player, validMobs[r.selection - 2]);
    }
    return;
  });
}
function openMorphBlockSelection(player, validBlocks = blocks, filterString = "") {
  const morphSelectionForm = new ActionFormData();
  if (player.getDynamicProperty("squaredreams_bam:toggle.locked") ?? false)
    validBlocks = validBlocks.filter((x) => getMorphUnlocked(player, x));
  morphSelectionForm.title(langText("squaredreams_bam.ui.block_morph.title"));
  if (filterString === "")
    morphSelectionForm.body(langText("squaredreams_bam.ui.block_morph.body", ["\n"]));
  else
    morphSelectionForm.body(langText("squaredreams_bam.ui.block_morph.body_filtered", ["\n", filterString]));
  morphSelectionForm.button(langText("squaredreams_bam.ui.morph.search_button"), "textures/sd/bam/ui/search");
  morphSelectionForm.button(langText(`squaredreams_bam.ui.button.morph.player.name`), `textures/sd/bam/ui/mobs/player`);
  validBlocks.forEach((x) => {
    if (getMorphUnlocked(player, x)) {
      morphSelectionForm.button(x.name, x.texture_path);
    } else {
      morphSelectionForm.button(langText("squaredreams_bam.ui.button.morph.locked"), `textures/sd/bam/ui/locked`);
    }
  });
  morphSelectionForm.show(player).then((r) => {
    if (r.canceled || r.selection == void 0)
      return;
    if (r.selection === 0) {
      openMorphBlockFilter(player, filterString);
      return;
    }
    if (r.selection === 1) {
      transform(player, mobs[0]);
      return;
    }
    if (getMorphUnlocked(player, validBlocks[r.selection - 2])) {
      transform(player, validBlocks[r.selection - 2]);
    } else {
      failMorphLocked(player, validBlocks[r.selection - 2]);
    }
    return;
  });
}
function openMorphEntityFilter(player, defaultString = "") {
  const filterMorphMenu = new ModalFormData();
  filterMorphMenu.title(langText("squaredreams_bam.ui.filter_menu.title"));
  filterMorphMenu.textField(langText("squaredreams_bam.ui.filter_menu.textbox_label"), langText("squaredreams_bam.ui.filter_menu.textbox_placeholder"), defaultString);
  filterMorphMenu.toggle(langText("squaredreams_bam.ui.filter_menu.locked_toggle"), player.getDynamicProperty("squaredreams_bam:toggle.locked") ?? false);
  filterMorphMenu.submitButton(langText("squaredreams_bam.ui.filter_menu.submit_button"));
  filterMorphMenu.show(player).then((r) => {
    if (r.canceled || r.formValues == void 0)
      return;
    const textField = r.formValues[0];
    const lockedToggle = r.formValues[1];
    if (lockedToggle) {
      player.setDynamicProperty("squaredreams_bam:toggle.locked", true);
    } else {
      player.setDynamicProperty("squaredreams_bam:toggle.locked", false);
    }
    if (textField !== "") {
      const searchString = textField.toLowerCase();
      const searchStringUnderscore = searchString.replace(/\s/g, "_");
      const mobsFiltered = mobs.filter(
        (x) => x.id.includes(searchStringUnderscore) || x.name.toLowerCase().includes(searchString)
      );
      openMorphEntitySelection(player, mobsFiltered, textField);
    } else {
      openMorphEntitySelection(player);
    }
  });
}
function openMorphBlockFilter(player, defaultString = "") {
  const filterMorphMenu = new ModalFormData();
  filterMorphMenu.title(langText("squaredreams_bam.ui.filter_menu.title"));
  filterMorphMenu.textField(langText("squaredreams_bam.ui.filter_menu.textbox_label"), langText("squaredreams_bam.ui.filter_menu.textbox_placeholder"), defaultString);
  filterMorphMenu.toggle(langText("squaredreams_bam.ui.filter_menu.locked_toggle"), player.getDynamicProperty("squaredreams_bam:toggle.locked") ?? false);
  filterMorphMenu.submitButton(langText("squaredreams_bam.ui.filter_menu.submit_button"));
  filterMorphMenu.show(player).then((r) => {
    if (r.canceled || r.formValues == void 0)
      return;
    const textField = r.formValues[0];
    const lockedToggle = r.formValues[1];
    if (lockedToggle) {
      player.setDynamicProperty("squaredreams_bam:toggle.locked", true);
    } else {
      player.setDynamicProperty("squaredreams_bam:toggle.locked", false);
    }
    if (textField !== "") {
      const searchString = textField.toLowerCase();
      const searchStringUnderscore = searchString.replace(/\s/g, "_");
      const blocksFiltered = blocks.filter(
        (x) => x.id.includes(searchStringUnderscore) || x.name.toLowerCase().includes(searchString)
      );
      openMorphBlockSelection(player, blocksFiltered, textField);
    } else {
      openMorphBlockSelection(player);
    }
  });
}
function openMorphOptions(player, isBaby = false, variant = 0) {
  const morphMenuForm = new ModalFormData();
  const morph = getMorph(player);
  morphMenuForm.title(langText("squaredreams_bam.ui.morph_menu.title"));
  morphMenuForm.toggle(langText("squaredreams_bam.ui.morph_menu.baby_toggle"), isBaby);
  if (morph.variants.length !== 0)
    morphMenuForm.dropdown(langText("squaredreams_bam.ui.morph_menu.variant_dropdown"), morph.variants.map((x) => langText(`squaredreams_bam.variant.${morph.id}.${x}`)), variant);
  morphMenuForm.submitButton(langText("squaredreams_bam.ui.morph_menu.submit_button"));
  morphMenuForm.show(player).then((r) => {
    if (r.canceled || r.formValues == void 0)
      return;
    const babyToggle = r.formValues[0];
    player.setDynamicProperty("squaredreams_bam:toggle.baby", babyToggle);
    if (babyToggle != isBaby) {
      setMorphHelmet(player, morph);
    }
    if (morph.variants.length !== 0) {
      var variantDropdown = r.formValues[1];
      player.setDynamicProperty(`squaredreams_bam:variant.${morph.id}`, variantDropdown);
      setMorphHelmet(player, morph);
    }
  });
}
function openSettingsMenu(player, unlockSetting, nametagOffSetting) {
  const settingsMenuForm = new ModalFormData();
  settingsMenuForm.title(langText("squaredreams_bam.ui.settings_menu.title"));
  settingsMenuForm.toggle(langText("squaredreams_bam.ui.settings_menu.unlock_toggle"), unlockSetting);
  settingsMenuForm.toggle(langText("squaredreams_bam.ui.settings_menu.nametag_toggle"), nametagOffSetting);
  settingsMenuForm.submitButton(langText("squaredreams_bam.ui.settings_menu.submit_button"));
  settingsMenuForm.show(player).then((r) => {
    if (r.canceled || r.formValues == void 0)
      return;
    const unlockToggle = r.formValues[0];
    const nametagOffToggle = r.formValues[1];
    if (nametagOffToggle && !nametagOffSetting) {
      player.setDynamicProperty("squaredreams_bam:toggle.nametag_off", true);
      actionBar(player, { translate: "squaredreams_bam.actionbar.nametag_off" });
      player.nameTag = "";
    }
    if (!nametagOffToggle && nametagOffSetting) {
      player.setDynamicProperty("squaredreams_bam:toggle.nametag_off", false);
      actionBar(player, { translate: "squaredreams_bam.actionbar.nametag_on" });
      player.nameTag = player.name;
    }
    if (unlockToggle && !unlockSetting) {
      openUnlockAllTrueConfirm(player);
      return;
    }
    if (!unlockToggle && unlockSetting) {
      openUnlockAllFalseConfirm(player);
    }
  });
}
function openUnlockAllTrueConfirm(player) {
  const confirmForm = new MessageFormData();
  confirmForm.title(langText("squaredreams_bam.ui.confirm_unlock_all_on.title"));
  confirmForm.body(langText("squaredreams_bam.ui.confirm_unlock_all_on.body", ["\n"]));
  confirmForm.button1(langText("squaredreams_bam.ui.confirm_unlock_all_on.no"));
  confirmForm.button2(langText("squaredreams_bam.ui.confirm_unlock_all_on.yes"));
  confirmForm.show(player).then((r) => {
    if (r.canceled || r.selection == void 0)
      return;
    if (r.selection) {
      player.setDynamicProperty("squaredreams_bam:toggle.unlock_all", true);
      successUnlockAllMorphs(player);
    } else {
      openSettingsMenu(player, true, player.getDynamicProperty("squaredreams_bam:toggle.nametag_off"));
    }
  });
}
function openUnlockAllFalseConfirm(player) {
  const confirmForm = new MessageFormData();
  confirmForm.title(langText("squaredreams_bam.ui.confirm_unlock_all_off.title"));
  confirmForm.body(langText("squaredreams_bam.ui.confirm_unlock_all_off.body", ["\n"]));
  confirmForm.button1(langText("squaredreams_bam.ui.confirm_unlock_all_off.no"));
  confirmForm.button2(langText("squaredreams_bam.ui.confirm_unlock_all_off.yes"));
  confirmForm.show(player).then((r) => {
    if (r.canceled || r.selection == void 0)
      return;
    if (r.selection) {
      player.setDynamicProperty("squaredreams_bam:toggle.unlock_all", false);
      openLockAllConfirm(player);
    } else {
      player.setDynamicProperty("squaredreams_bam:toggle.unlock_all", false);
      successDisableUnlockAllMorphs(player);
    }
  });
}
function openLockAllConfirm(player) {
  const confirmForm = new MessageFormData();
  confirmForm.title(langText("squaredreams_bam.ui.confirm_lock_all.title"));
  confirmForm.body(langText("squaredreams_bam.ui.confirm_lock_all.body", ["\n"]));
  confirmForm.button1(langText("squaredreams_bam.ui.confirm_lock_all.no"));
  confirmForm.button2(langText("squaredreams_bam.ui.confirm_lock_all.yes"));
  confirmForm.show(player).then((r) => {
    if (r.canceled || r.selection == void 0)
      return;
    if (r.selection) {
      for (const morph of morphs) {
        lockMorph(player, morph);
      }
      transform(player, morphs[0]);
      successLockAllMorphs(player);
    } else {
      successDisableUnlockAllMorphs(player);
    }
  });
}
export {
  openMainMenu
};
