import { world, system, ItemStack, EquipmentSlot } from "@minecraft/server";
let reau = false
let reau1 = false
let rau = false
const soundState = new WeakMap()
system.runInterval(() => {
  for (const player of world.getPlayers()) {
    const entities = player.dimension.getEntities({ type: "kick:wendigo_die" });
    for (const e of entities) {
      const dx = e.location.x - player.location.x;
      const dy = e.location.y - player.location.y;
      const dz = e.location.z - player.location.z;
      if (dx * dx + dy * dy + dz * dz <= 25) {
        player.removeTag("no_cooked_meat");
        player.removeTag("message")
        player.addTag("despawn_wen")
      }
    }
  }
}, 20);

world.afterEvents.itemUse.subscribe((skick) => {
  const itey = skick.itemStack
  const player = skick.source
  if (itey.typeId === "kick:radio" && !player.isSneaking) {
    reau = true
    player.addTag("actu")
    world.sendMessage("Hello, I am SaiKICK, the creator of this addon, well u may ask why am I here, so let me clear that up, I am here as a guide to help u")
    world.sendMessage("If u have any queries type !help")
    player.runCommand('playsound intro @s')
  }
  if (itey.typeId === "kick:radio" && player.isSneaking) {
    const nearby = player.dimension.getEntities({
      type: "kick:wendigo_stalker",
      location: player.location,
      maxDistance: 30
    });
    const nearby2 = player.dimension.getEntities({
      type: "kick:wendigo_die",
      location: player.location,
      maxDistance: 5
    })
    const hasNearby1 = nearby2.length > 0
    const hasNearby = nearby.length > 0;
    if (hasNearby) {
      player.onScreenDisplay.setActionBar("Playing Radio...")
      player.runCommand('playsound radio.breath @s')
      soundState.set(player, "breath");
    }

    else {
      player.onScreenDisplay.setActionBar("Playing Radio...")
      player.runCommand(`stopsound @s`);
      player.runCommand('playsound radio.tt @s')
      soundState.set(player, "tt");
    }
    if (hasNearby1) {
      player.onScreenDisplay.setActionBar("Playing Radio...")
      player.runCommand('playsound radio.tt @s')
      player.runCommand('playsound heartbeat @s')
      soundState.set(player, "heart")
    }
  }
})

world.afterEvents.itemUse.subscribe((rkick) => {
  const ew = rkick.itemStack
  const player = rkick.source
  if (ew.typeId === "kick:radio_death") {
    player.onScreenDisplay.setActionBar("Playing §4§kRadio...")
    player.runCommand('playsound radio.heart @s')
  }
})

world.afterEvents.chatSend.subscribe((ukick) => {
  const msg1 = ukick.message
  const player = ukick.sender
  if (reau && msg1 === "!help") {
    rau = true
    world.sendMessage("Oh,need help?")
    world.sendMessage("Tell me what u want from the following:-")
    world.sendMessage("Type the number that corresponds to your question")
    player.runCommand('playsound needhelp @s')
    world.sendMessage("1•About Addon")
    world.sendMessage("2•I see a Creature..")
    world.sendMessage("3•How to keep myself warm?")
    if (player.hasTag("cursed")) {
      reau1 = true
      world.sendMessage("4•What is Wendigo?")
      world.sendMessage("5•How to defeat Wendigo?")
      world.sendMessage("6•I can't eat cooked meat and veggies..")
      world.sendMessage("7•What to eat..HELP!")
      world.sendMessage("8•Wendigo isn't fighting me!?")
      world.sendMessage("9•I see a player with wendigo head..")
      world.sendMessage("10•Where can I get the body parts")
    }
  }
  if (reau && rau && msg1 === "1") {
    world.sendMessage("Oh, about the addon, u ask?")
    world.sendMessage("Well I would rather prefer not saying anything about that... Just think of me as an invisible companion going alongside u in ur journey with some plenty of knowledge")
    player.runCommand('playsound abouttheaddon @s')
  }
  if (reau && rau && msg1 === "2") {
    world.sendMessage("Oh, do u?")
    world.sendMessage("Stay calm it might be dangerous")
    player.runCommand('playsound wendigospot @s')
  }
  if (reau && rau && msg1 === "3") {
    world.sendMessage("Mmm try wearing pieces of leather armor, sitting near a campfire or fire, to keep yourself warm")
    player.runCommand('playsound staywarmadvice @s')
  }
  if (reau && reau1 && msg1 === "4") {
    world.sendMessage("It's a creature that craves for meat, especially human meat. Legends say that it also has the capability to curse fellows, since they lack the organs to reproduce.Anyone who is cursed by one will experience slow and an unmerciful death")
    player.runCommand('playsound whatiswendigo @s')
  }
  if (reau && reau1 && msg1 === "5") {
    world.sendMessage("Hmm, I have heard that there's a specific sword capable of defeating Wendigo, I wonder what it can be, but I guess it starts with the letter 's'")
    player.runCommand('playsound howtodefeat @s')
  }
  if (reau && reau1 && msg1 === "6") {
    world.sendMessage("Are you serious? Did u get cursed by wendigo?")
    world.sendMessage("Oh Hell Nah")
    player.runCommand('playsound ugotcursed @s')
  }
  if (reau && reau1 && msg1 === "7") {
    world.sendMessage("Eating raw is probably the only way to fill your hunger...")
    world.sendMessage("Oh my God it is so gross")
    player.runCommand('playsound gross @s')
  }
  if (reau && reau1 && msg1 === "8") {
    world.sendMessage("Maybe try to spawn him manually using his body parts")
    world.sendMessage("Try looking for some")
    player.runCommand('playsound ritual @s')
  }
  if (reau && reau1 && msg1 === "9") {
    world.sendMessage("Oh, stay calm, don't move! don't go too close, they might try attracting u to feed their tummies, don't fall for it!")
    player.runCommand('playsound wendigolookingplayer @s')
  }
  if (reau && reau1 && msg1 === "10") {
    world.sendMessage("I don't really know where u can get the antlers, but I think I have heard that the skull is somewhere in some shrine or dungeon type area..")
    player.runCommand('playsound location @s')

  }
})

world.afterEvents.playerSpawn.subscribe((nkick) => {
  const yt = nkick.initialSpawn
  const player = nkick.player
  const item = new ItemStack("kick:radio")
  const item1 = new ItemStack("kick:record_15")
  if (!yt) return;
  if (player.hasTag("got_radio")) return;
  system.runTimeout(() => {
    player.getComponent("minecraft:inventory").container.addItem(item)
    player.getComponent("minecraft:inventory").container.addItem(item1)
    player.addTag("got_radio")
  }, 20)
})