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

const quests = [
  {
    title: "§l§eQuest 1§f: Defeat Mothlley",
    description: "Defeat Mothlley and claim your title as the queen of the monsters.",
    completionTag: "mothlley_killed",
    rewardTag: "mothlley_rewarded",
    achievementTitle: "§f[§aAchievement Unlocked§f] §eQueen of the Monsters!",
    successSound: "kaiju.quest_complete",
    failureSound: "kaiju.quest_not_complete",
    alreadyCompletedSound: "kaiju.quest_already_completed",
  },
  {
    title: "§l§eQuest 2§f: Defeat Behemoohoglin",
    description: "Defeat Behemoohoglin and become the ruler of the wild.",
    completionTag: "behemoohoglin_killed",
    rewardTag: "behemoohoglin_rewarded",
    achievementTitle: "§f[§aAchievement Unlocked§f] §eSlayer of the Behemoth!",
    successSound: "kaiju.quest_complete",
    failureSound: "kaiju.quest_not_complete",
    alreadyCompletedSound: "kaiju.quest_already_completed",
  },
  {
    title: "§l§eQuest 3§f: Defeat Rophan",
    description: "Destroy the terrifying Rophan and protect your land.",
    completionTag: "rophan_killed",
    rewardTag: "rophan_rewarded",
    achievementTitle: "§f[§aAchievement Unlocked§f] §eSlayer of the Sky Devil!",
    successSound: "kaiju.quest_complete",
    failureSound: "kaiju.quest_not_complete",
    alreadyCompletedSound: "kaiju.quest_already_completed",
  },
  {
    title: "§l§eQuest 4§f: Revive Golem Kong",
    description: "Revive the mighty Golem Kong",
    completionTag: "golem_kong_killed",
    rewardTag: "golem_kong_rewarded",
    achievementTitle: "§f[§aAchievement Unlocked§f] §eYou Bow to No One!",
    successSound: "kaiju.quest_complete",
    failureSound: "kaiju.quest_not_complete",
    alreadyCompletedSound: "kaiju.quest_already_completed",
  },
  {
    title: "§l§eQuest 5§f: Defeat Wardenzilla",
    description: "Defeat Wardenzilla, the Raging Titan.",
    completionTag: "wardenzilla_killed",
    rewardTag: "wardenzilla_rewarded",
    achievementTitle: "§f[§aAchievement Unlocked§f] §eThe King Of The Monster!",
    successSound: "kaiju.quest_complete",
    failureSound: "kaiju.quest_not_complete",
    alreadyCompletedSound: "kaiju.quest_already_completed",
  },
  {
    title: "§l§eQuest 6§f: Create A Nuke",
    description: "create a nuke using wardenzilla core and activated it",
    completionTag: "nuke_creator",
    rewardTag: "nuke_rewarded",
    achievementTitle: "§f[§aAchievement Unlocked§f] §eI Have Become Death The Destroy of World",
    successSound: "kaiju.quest_complete",
    failureSound: "kaiju.quest_not_complete",
    alreadyCompletedSound: "kaiju.quest_already_completed",
  },
];

function showQuestBook(player) {
  player.runCommandAsync(`playsound kaiju.menu_click @s`);

  const form = new ActionFormData().title("§lQuest Book").body("Select a quest to view details\nyou will receive XP every time\nyou completed a quest");

  quests.forEach((quest) => {
    form.button(quest.title);
  });

  form.show(player).then((response) => {
    if (response.canceled) return;
    let selectedQuest = quests[response.selection];
    showQuestDetails(player, selectedQuest);
  });
}

function showQuestDetails(player, quest) {
  player.runCommandAsync(`playsound kaiju.menu_click @s`); 

  const form = new ActionFormData()
    .title(quest.title)
    .body(quest.description)
    .button("Submit Quest")
    .button("Back");

  form.show(player).then((response) => {
    if (response.canceled) return;

    if (response.selection === 0) {
      if (player.hasTag(quest.completionTag)) {
        if (!player.hasTag(quest.rewardTag)) {
          system.run(async () => {
            await player.runCommandAsync(`playsound ${quest.successSound} @a`);
            player.sendMessage(`${quest.achievementTitle}`);
            await player.runCommandAsync(`xp 9703000 @s`);
            player.addTag(quest.rewardTag);
            player.sendMessage("§aYou have received your reward!");
          });
        } else {t
          player.runCommandAsync(`playsound ${quest.failureSound} @a`);
          player.sendMessage("§cYou have already completed this quest!");
        }
      } else {
        system.run(() => {
          player.runCommandAsync(`playsound ${quest.failureSound} @a`);
          player.sendMessage("§cQuest not completed yet!");
        });
      }
    } else {
      player.runCommandAsync(`playsound kaiju.menu_click @s`);
      showQuestBook(player);
    }
  });
}

world.afterEvents.itemUse.subscribe((event) => {
  const { source, itemStack } = event;
  if (itemStack?.typeId === "gojidraw:kaiju_quest_book") {
    showQuestBook(source);
  }
});