import { world, system, ItemStack, BlockPermutation, EquipmentSlot, EntityComponentTypes } from "@minecraft/server";
import { NAMESPACE } from "../constants";
import * as mc from "@minecraft/server";

export function goodHubSummon(player) {
  const structureName = "float_ad/good_hub";
  const tempStructureName = "temp_good_structure_save";
  const position = { x: 50, y: 250, z: 50 };
  const size = { x: 50, y: 50, z: 44 };

  // Save the player's current location and dimension
  const playerLocation = player.location;
  const playerDimension = player.dimension.id;
  const locationTag = `saved_location:${playerLocation.x},${playerLocation.y},${playerLocation.z},${playerDimension}`;
  player.addTag(locationTag);

  // Play entrance sound
  player.runCommand(`playsound float_ad.good_hub_entrance "${player.name}"`);
  player.runCommand(`effect "${player.name}" saturation 4 4 true`);
  // Teleport the player to the specified location
  const teleportCommand = `tp "${player.name}" 85 270 84 facing 72.99 272.27 76.86`;
  const loadCommand = `structure load "${structureName}" ${position.x} ${position.y} ${position.z}`;

  player.runCommand(`camera "${player.name}" fade time 0.5 1.5 1 color 255 253 158`);
  player.runCommand(`effect "${player.name}" slow_falling 2 4 true`);

  // Check if any player is already in the hub
  const playersInHub = world.getPlayers({ tags: ["good_hub"] });
  if (playersInHub.length === 0) {
    // Save the current structure as a temporary structure
    system.runTimeout(() => {
      player.runCommand(
        `structure save "${NAMESPACE}.${tempStructureName}" ${position.x} ${position.y} ${position.z} ${
          position.x + size.x
        } ${position.y + size.y} ${position.z + size.z} false disk`
      );
    }, 14);
    // Load the new structure
    system.runTimeout(() => {
      world.getDimension("overworld").runCommand(loadCommand);
      player.runCommand(`camera "${player.name}" clear`);
    }, 20);
  }

  system.runTimeout(() => {
    world.getDimension("overworld").runCommand(teleportCommand);
  }, 10);
  player.addTag("good_hub");
}

export function goodHubRemove(player) {
  const tempStructureName = "temp_good_structure_save";
  const position = { x: 50, y: 250, z: 50 };

  // Retrieve the saved location tag and teleport the player back to that location
  const locationTag = player.getTags().find((tag) => tag.startsWith("saved_location:"));
  if (locationTag) {
    const [_, coordinates, dimension] = locationTag.split(":");
    const [x, y, z] = coordinates.split(",").map(Number);
    const teleportBackCommand = `tp "${player.name}" ${x} ${y} ${z}`;
    // Play exit sound
    player.runCommand(`playsound float_ad.hub_exit_good "${player.name}"`);
    system.runTimeout(() => {
      world.getDimension(dimension).runCommand(teleportBackCommand);
    }, 12);
    system.runTimeout(() => {
      player.runCommand(`event entity @e[type=float_ad:intro_npc] float:despawn`);
    }, 15);
    player.runCommand(`camera "${player.name}" fade time 0.2 1 1 color 255 253 158`);
    player.removeTag(locationTag);
    player.removeTag("good_hub");
  }

  // Check if any other players are still in the hub
  const playersInHub = world.getPlayers({ tags: ["good_hub"] });
  if (playersInHub.length === 0) {
    // Load the temporary structure to remove the hub
    const loadCommand = `structure load "${NAMESPACE}.${tempStructureName}" ${position.x} ${position.y} ${position.z}`;
    world.getDimension("overworld").runCommand(loadCommand);
    world
      .getDimension("overworld")
      .runCommand(`event entity @e[type=float_ad:altar_wisp,x=82,y=255,z=75,r=4] float_ad:kill_entity`);
    world
      .getDimension("overworld")
      .runCommand(`event entity @e[type=float_ad:settings_hub,x=82,y=7,z=75,r=80] float_ad:kill_entity`);
    world
      .getDimension("overworld")
      .runCommand(`event entity @e[type=float_ad:translate_hub,x=82,y=255,z=75,r=80] float_ad:kill_entity`);
    world
      .getDimension("overworld")
      .runCommand(`event entity @e[type=float_ad:book_hub,x=82,y=255,z=75,r=80] float_ad:killevent`);
    world
      .getDimension("overworld")
      .runCommand(`event entity @e[type=float_ad:hub_tp_good,x=82,y=255,z=75,r=80] float_ad:despawn`);
    world
      .getDimension("overworld")
      .runCommand(`event entity @e[type=float_ad:hooded_figure_good,x=76,y=265,z=85,r=60] float:despawn`);
    system.runTimeout(() => {
      world.getDimension("overworld").runCommand(`kill @e[type=item,x=82,y=255,z=75,r=80]`);
    }, 5);
  }
}
