import { world, Location, EntityInventoryComponent, ItemStack, ItemTypes } from "@minecraft/server";
//function loads structure with backpack entity
function loadStructure1() {
  let players = world.getPlayers();
for (let player of players) {
  let a = world.events.tick.subscribe((data) => {

      console.warn("Structure Loaded");
      player.runCommandAsync("structure load " + player.id + "t ~ ~ ~");
      console.warn(player.id);
      world.events.tick.unsubscribe(a);
    
  });
}}
//function saves structure with entity
function saveStructure1() {
  let players = world.getPlayers();
for (let player of players) {
    let a = world.events.tick.subscribe((data) => {
        player.runCommandAsync(
            "structure save " + player.id + "t ~ ~ ~ ~ ~3 ~ true disk false"
          );
          player.runCommandAsync(
            "event entity @e[type=pog:backpack, r=3] despawn_backpack"
          );
          console.warn("Structure Saved");
        world.events.tick.unsubscribe(a);
    });
  }}

world.events.tick.subscribe((data) => {
  let players = world.getPlayers();
for (let player of players) {
    const [entitie] = player.dimension.getEntities({
      tags: [`players:${player.name}`],
    });
    let invi = player.getComponent("inventory").container;
    const {x,y,z} = player.location
    let items = invi.getItem(player.selectedSlot);
    //this spawns the entity with a tag with the player name when the player does not have tag 'backpack1'
    if (items?.typeId == "pog:backpack" && !player.hasTag("backpack2")) {
      player.addTag("backpack2")
      let Dimensions = world.getDimension(player.dimension.id);
      let entitee = Dimensions.spawnEntity("pog:backpack", new Location(player.location.x, player.location.y, player.location.z));
      entitee.nameTag = "BackPack";
      entitee.addTag("players:" + player.name);
    }
    if (items?.typeId === "pog:backpack") {
        //if player does not have this tag while holding the item, we run the loadStructure function to load our entity
      if (!player.hasTag("tag1")) {
        player.addTag("tag1");
        loadStructure1();
      }
      //this always run when we hold our item
      entitie?.teleport(
        {
          x: Number(player.location.x),
          y: Number(player.location.y + 0.5),
          z: Number(player.location.z),
        },
        world.getDimension(player.dimension.id),
        0,
        0
      );
      //This will whitelist all items from the backpack inventory, like shulkers and backpack Item
      let e = world.events.tick.subscribe(data => {
        const inventory = entitie.getComponent("inventory");
       
        for (let slot = 0; slot < inventory.container.size; slot++) {
          let items = inventory.container.getItem(slot)
          let container = entitie.getComponent('inventory').container
          let contain = player.getComponent("inventory").container
            const itemStack = inventory.container.getItem(slot);
            const amount = itemStack?.amount
            const data = itemStack?.data
            let newItem =  new ItemStack(ItemTypes?.get("pog:netherite_helmet"), 0, 0);
            let OldItem =  new ItemStack(ItemTypes?.get("pog:backpack"), 1, 0);
             let shulker =  new ItemStack(ItemTypes?.get("minecraft:undyed_shulker_box"), 1, 0);
            if (items?.typeId == "pog:backpack") {
              var vars = slot;
              player.runCommandAsync("say Hello")
              container.setItem(vars, newItem);
              contain.setItem(player.selectedSlot, OldItem)
              player.runCommandAsync("say ItemSet")
            }
            world.events.tick.unsubscribe(e)
        }
          })
    } else if (!items?.typeId == "pog:backpack") {
        //if the player has this tag while not holding the item, we run the saveStructure function to save our entity
      if (player.hasTag("tag1")) {
        player.removeTag("tag1");
        saveStructure1()
      }
      //this always run when we don't hold the item
      entitie?.teleport(
        {
          x: Number(player.location.x),
          y: Number(player.location.y + 3),
          z: Number(player.location.z),
        },
        world.getDimension(player.dimension.id),
        0,
        0
      );
    }
  }}
);

