import * as MC from "@minecraft/server";
import * as UI from "@minecraft/server-ui";
import { onItem } from "./utilities";

MC.world.afterEvents.playerSpawn.subscribe((event) => {
  const { player } = event;

  if (player.getDynamicProperty("playerState") !== "alreadyJoined") {
    const guidebook = new MC.ItemStack("chronicoverride_llc:guidebook");
    player.dimension.spawnItem(guidebook, player.location);
    event.player.setDynamicProperty("playerState", "alreadyJoined");
  }
});

onItem(({ itemStack, source }) => {
  if (itemStack.typeId === "chronicoverride_llc:guidebook") mainMenu(source);
});

function mainMenu(player) {
  const menu = new UI.ActionFormData()
    .title("Field Guide: Shadows and Spirits")
    .body("")
    .button("Monsters")
    .button("Armors")
    .button("Items")
    .button("Weapons");

  menu.show(player).then((response) => {
    if (response.selection === 0) return monsterMenu(player);
    if (response.selection === 1) return armorMenu(player);
    if (response.selection === 2) return itemMenu(player);
    if (response.selection === 3) return weaponMenu(player);
  });
}

function monsterMenu(player) {
  const menu = new UI.ActionFormData()
    .title("Field Guide: Shadows and Spirits")
    .body("")
    .button("Bonemonger")
    .button("Faceless")
    .button("Haunt")
    .button("Lurker")
    .button("Night Stalker")
    .button("Screecher")
    .button("Shadewalker")
    .button("Watcher")
    .button("Whisper")
    .button("Widow");

  menu.show(player).then((response) => {
    if (response.selection >= 0)
      return monsterDescriptionMenu(player, response.selection);
  });
}

function monsterDescriptionMenu(player, response) {
  const menu = new UI.ActionFormData().title(
    "Field Guide: Shadows and Spirits"
  );
  if (response === 0) {
    menu.body(
      `§l§cBonemonger§r\n
§6Description:§r A huge skeleton made of bones and dark magic.\n
§6Behavior:§r It heals during battle unless stopped by the player holding the banishing rites in their off hand.\n
§aTips:§r Vulnerable to the Spectral Spear. Use the Banishing Rites in your off hand to stop its health regeneration.`
    );
  }
  if (response === 1) {
    menu.body(
      `§l§cFaceless§r\n
§6Description:§r: A tall, ghostly faceless creature that attacks head on.\n
§6Behavior:§r Attacks the player head on. If hit by the player it will teleport away before striking again. Light sources repel the monster.\n
§aTips:§r Hold the Spectral Mirror in your off hand to make the creature fully visible.`
    );
  }
  if (response === 2) {
    menu.body(
      `§l§cHaunt§r\n
§6Description:§r: A floating semi transparent ghost that attacks head on.\n
§6Behavior:§r When it spots a player, it speeds up, chases the player, and attacks.\n
§aTips:§r Use the Spectral Mirror in your off hand to make the Haunt fully visible.`
    );
  }
  if (response === 3) {
    menu.body(
      `§l§cLurker§r\n
§6Description:§r: A semi transparent shadow monster that lurks in the dark.\n
§6Behavior:§r Waits in the shadows and then runs and attacks the player from behind. When you look at it, it won't move. When hit, it runs away and then will come back and strike again.\n
§aTips:§r Use the Spectral Mirror in your off hand to make the Lurker fully visible. Be sure to watch your back!`
    );
  }
  if (response === 4) {
    menu.body(
      `§l§cNight Stalker§r\n
§6Description:§r: A lightning fast predator that hunts in the dark.\n
§6Behavior:§r Avoids all light and attacks the player quickly. Inflicts a strong slash and causes a flash on the screen.\n
§aTips:§r Stay in well lit areas. The Lunar Blade repels this monster.`
    );
  }
  if (response === 5) {
    menu.body(
      `§l§cScreecher§r\n
§6Description:§r: A flying skeleton monster that swoops down to attack the player.\n
§6Behavior:§r  Dives toward the player then releases a loud screech attack that causes nausea, screen distortion, and confusion.\n
§aTips:§r Use Flash Powder to knock it from the sky momentarily and use melee weapons. Use the Spectral Spear to attack it while in the air.`
    );
  }
  if (response === 6) {
    menu.body(
      `§l§cShadewalker§r\n
§6Description:§r: A shadow creature that roams in the darkness.\n
§6Behavior:§r Lurks in caves and avoids light sources. When it sees the player it will run and hit them with dark strikes that cause the player to experience momentary blackout. \n
§aTips:§r Fight fast and use light to repel the monster. Use the Spectral Spear to fight it from a distance.`
    );
  }
  if (response === 7) {
    menu.body(
      `§l§cWatcher§r\n
§6Description:§r: A floating eye that watches from above and spawns monsters below.\n
§6Behavior:§r Stare into its eyes and it will cause slowness. It spawns waves of monsters until defeated.\n
§aTips:§r Use the Flash Powder to knock it from the sky momentarily to use melee weapons. The echo sword is very effective against this monster and its hoards.`
    );
  }
  if (response === 8) {
    menu.body(
      `§l§cWhisper§r\n
§6Description:§r: A ghost that whispers and distorts the world when it attacks you.\n
§6Behavior:§r When it attacks the player it causes slowness and nausea momentarily.\n
§aTips:§r Use light to repel the Whisper.`
    );
  }
  if (response === 9) {
    menu.body(
      `§l§cWidow§r\n
§6Description:§r: A spider that traps and poisons players.\n
§6Behavior:§r Shoots sticky webs to trap prey and then attacks with a venomous bite that poisons the player.\n
§aTips:§r Avoid webs and hit fast before it closes in. Use the Widows Silk Armor to automatically clear webs around you.`
    );
  }
  menu.button("Back");

  menu.show(player).then((response) => {
    if (response.selection === 0) return monsterMenu(player);
  });
}

function armorMenu(player) {
  const menu = new UI.ActionFormData()
    .title("Field Guide: Shadows and Spirits")
    .body("")
    .button("Nightstalker Armor")
    .button("Widow silk Armor")
    .button("Wraithskin Armor");

  menu.show(player).then((response) => {
    if (response.selection >= 0)
      return armorDescriptionMenu(player, response.selection);
  });
}

function armorDescriptionMenu(player, response) {
  const menu = new UI.ActionFormData().title(
    "Field Guide: Shadows and Spirits"
  );
  if (response === 0) {
    menu.body(
      `§l§cNightstalker Armor§r\n
§6Description:§r When the complete set it worn the player is given slow falling, speed, night vision, and reduced detection by spectral monsters.\n
§6Recipes:§r\n
§l§cNightstalker Hood§r
Comes with Protection IV and Unbreaking III Enchantments built in.
C | C | C
A | # | B
# | # | #
A = Shadow Fragment
B = Nightstalkers Fang
C = Netherite Ingot\n
§l§cNightstalker Chestplate§r
Comes with Protection IV and Unbreaking III Enchantments built in.
A | # | A
C | B | C
C | C | C
A = Shadow Fragment
B = Bonemongers Skull
C = Netherite Ingot\n
§l§cNightstalker Leggings§r
Comes with Protection IV and Unbreaking III Enchantments built in.
C | A | C
C | # | C
B | # | B
A = Shadow Fragment
B = Nightstalkers Fang
C = Netherite Ingot\n
§l§cNightstalker Boots§r
Comes with Protection IV and Unbreaking III Enchantments built in.
# | # | #
A | # | B
C | C | C
A = Shadow Fragment
B = Bonemongers Skull
C = Netherite Ingot\n`
    );
  }
  if (response === 1) {
    menu.body(
      `§l§cWidow's silk Armor§r\n
§6Description:§r Gives the player a speed boost and automatically clears webs. Perfect to use against the Widow.\n
§6Recipes:§r\n
A = Widow's Silk
B = Whispering Essence
C = Netherite Ingot

§l§cWidow's silk Hood§r
Comes with Protection IV and Unbreaking III Enchantments when crafted.
C | C | C
A | B | A
# | # | #\n
§l§cWidow's silk Vest§r
Comes with Protection IV and Unbreaking III Enchantments when crafted.
A | A | A
C | B | C
C | C | C\n
§l§cWidow's silk Leggings§r
Comes with Protection IV and Unbreaking III Enchantments when crafted. 
C | B | C
C | # | C
A | # | A\n
§l§cWidow's silk Boots§r
Comes with Protection IV, Unbreaking III, and Depth Strider III Enchantments when crafted.
B | A | B
C | C | C
# | # | #\n`
    );
  }
  if (response === 2) {
    menu.body(
      `§l§cWraithskin Armor§r\n
§6Description:§r Gives the player speed boost, feather falling, and night vision when complete set is worn.\n
§6Recipes:§r\n
§l§cWraithskin Helmet§r
Comes with Protection IV and Unbreaking III Enchantments built in.
C | C | C
# | B | #
# | A | #
A = Spectral Shard
B = Faceless Mask
C = Netherite Ingot\n
§l§cWraithskin Chestplate§r
Comes with Protection IV, Thorns III, and Unbreaking III Enchantments built in.
C | C | C
C | B | C
# | A | #
A = Spectral Shard
B = Watchers Eye
C = Netherite Ingot\n
§l§cWraithskin Leggings§r
Comes with Protection IV and Unbreaking III Enchantments built in.
C | A | C
C | # | C
B | # | B
A = Spectral Shard
B = Nightstalkers Fang
C = Netherite Ingot\n
§l§cWraithskin Boots§r\n
Comes with Protection IV and Unbreaking III Enchantments built in.
# | B | #
B | A | B
# | # | #
A = Spectral Shard
B = Netherite Ingot\n`
    );
  }
  menu.button("Back");

  menu.show(player).then((response) => {
    if (response.selection === 0) return armorMenu(player);
  });
}

function itemMenu(player) {
  const menu = new UI.ActionFormData()
    .title("Field Guide: Shadows and Spirits")
    .body("")
    .button("Banishing Rites")
    .button("Dreamcatcher")
    .button("Evil Eye Pendant")
    .button("Flash Powder")
    .button("Shade Candle")
    .button("Shadow Salve")
    .button("Spectral Mirror")
    .button("Whispering Doll")
    .button("Widow's Web Potion")
    .button("Spectral Cloak Hood")
    .button("Spectral Cloak Robe")
    .button("Guide Book");

  menu.show(player).then((response) => {
    if (response.selection >= 0)
      return itemDescriptionMenu(player, response.selection);
  });
}

function itemDescriptionMenu(player, response) {
  const menu = new UI.ActionFormData().title(
    "Field Guide: Shadows and Spirits"
  );
  if (response === 0) {
    menu.body(
      `§l§cBanishing Rites§r\n
§6Description:§r Stops the Bonemonger's health regeneration. Increases the players attack damage.\n
§6Recipe:§r\n
A | # | #
B | # | #
C | # | #
A = Faceless Mask
B = Writable Book
C = Blaze Powder`
    );
  }
  if (response === 1) {
    menu.body(
      `§l§cDreamcatcher§r\n
§6Description:§r Creates a safezone area around where it is placed. Spectral monsters will not spawn or enter these areas.\n
§6Recipe:§r\n
# | C | #
B | D | B
# | A | #
A = Spectral Shard
B = String
C = Feather
D = Netherite Ingot`
    );
  }
  if (response === 2) {
    menu.body(
      `§l§cEvil Eye Pendant§r\n
§6Description:§r Gives the player night vision and reveals transparent mobs in their full form when they are near you.\n
§6Recipe:§r\n
B | A | B
# | C | #
# | # | #
A = Watcher's Eye
B = Gold Ingot
C = Ender Pearl`
    );
  }
  if (response === 3) {
    menu.body(
      `§l§cFlash Powder§r\n
§6Description:§r Blinds enemies and knocks airborne mobs to the ground.\n
§6Recipe:§r\n
B | A | B
# | C | #
# | # | #\n
A = Watcher's Eye
B = Glowstone Dust
C = Gunpowder`
    );
  }
  if (response === 4) {
    menu.body(
      `§l§cShade Candle§r\n
§6Description:§r Repels shadowy enemies and lights dark areas.\n
§6Recipe:§r\n
A | # | #
B | # | #
C | # | #
A = Shade Assence
B = Candle
C = Charcoal`
    );
  }
  if (response === 5) {
    menu.body(
      `§l§cShadow Salve§r\n
§6Description:§r The Shadow Salve provides temporary invisibility, allowing the player to bypass enemies or avoid combat altogether. It has a limited duration and should be used strategically.\n
§6Recipe:§r\n
A | B | C
# | D | #
# | # | #
A = Whispering Assence
B = Potion
C = Fermented Spider Eye
D = Gunpowder`
    );
  }
  if (response === 6) {
    menu.body(
      `§l§cSpectral Mirror§r\n
§6Description:§r Reveals invisible mobs when held in the main or off-hand.\n
§6Recipe:§r\n
A | B | A
# | C | #
# | C | #
A = Spectral Shard
B = Glass Pane
C = Iron Ingot`
    );
  }
  if (response === 7) {
    menu.body(
      `§l§cWhispering Doll§r\n
§6Description:§r Grants Swiftness while held and prevents effects from the Whisper.\n
§6Recipe:§r\n
# | C | #
B | D | B
# | A | #
A = Whispering Assence
B = String
C = Leather
D = Netherite Ingot`
    );
  }
  if (response === 8) {
    menu.body(
      `§l§cWidow's Web Potion§r\n
§6Description:§r Throws sticky webs that trap enemies in place.\n
§6Recipe:§r\n
A | C | A
# | B | #
# | # | #
A = Widow's Silk
B = Spider Eye
C = Potion`
    );
  }
  if (response === 9) {
    menu.body(
      `§l§cSpectral Cloak Hood§r\n
§6Description:§r Grants invisibility when worn.\n
§6Recipe:§r\n
A | C | A
B | # | B
# | # | #
A = Spectral Shard
B = Phantom Membrane
C = Netherite Ingot`
    );
  }
  if (response === 10) {
    menu.body(
      `§l§cSpectral Cloak Robe§r\n
§6Description:§r Reduces mob detection when worn.\n
§6Recipe:§r\n
A | # | A
B | C | B
# | # | #
A = Spectral Shard
B = Phantom Membrane
C = Netherite Ingot`
    );
  }
  if (response === 11) {
    menu.body(
      `§l§cGuide Book§r\n
§6Description:§r Help guide you through the Add-on.\n
§6Recipe:§r\n
A | # | #
B | # | #
# | # | #
A = Book
B = Writable Book`
    );
  }
  menu.button("Back");

  menu.show(player).then((response) => {
    if (response.selection === 0) return itemMenu(player);
  });
}

function weaponMenu(player) {
  const menu = new UI.ActionFormData()
    .title("Field Guide: Shadows and Spirits")
    .body("")
    .button("Echo Sword")
    .button("Lunar Blade")
    .button("Spectral Spear");

  menu.show(player).then((response) => {
    if (response.selection >= 0)
      return weaponDescriptionMenu(player, response.selection);
  });
}

function weaponDescriptionMenu(player, response) {
  const menu = new UI.ActionFormData().title(
    "Field Guide: Shadows and Spirits"
  );
  if (response === 0) {
    menu.body(
      `§l§cEcho Sword§r\n
§6Description:§r When you hit one mob, an echo sound effect will hit and damage other nearby mobs simultaneously. Best used against the Watcher and it's waves of mobs.\n
§6Recipe:§r\n
# | D | #
A | D | B
# | C | #
A = Screechers Feather
B = Whispering Assence
C = Diamond Sword
D = Netherite Ingot`
    );
  }
  if (response === 1) {
    menu.body(
      `§l§cLunar Blade§r\n
§6Description:§r A powerful sword with build in enchantments Sharpness V, Smite V, and Unbreaking III. Grants the player extra strength at night.\n
§6Recipe:§r\n
# | B | #
A | D | A
# | C | #
A = Nightstalkers Fang
B = Shadow Fragment
C = Diamond Sword
D = Netherite Ingot`
    );
  }
  if (response === 2) {
    menu.body(
      `§l§cSpectral Spear§r\n
§6Description:§r A powerful Spear with built in enchantments Piercing IV, Knockback II, Smite V, and Unbreaking III. It will come back to you automatically after you throw it.\n
§6Recipe:§r\n
A | B | D
# | C | #
# | C | #
A = Bonemongers Skull
B = Watcher's Eye
C = Blaze Rod
D = Netherite Ingot`
    );
  }
  menu.button("Back");

  menu.show(player).then((response) => {
    if (response.selection === 0) return weaponMenu(player);
  });
}
