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

export function evilHubSummon(player) {
  const structureName = "float_ad/evil_hub1";
  const tempStructureName = "temp_evil_structure_save";
  const position = { x: 50, y: 0, z: 50 };
  const size = { x: 45, y: 40, z: 45 };

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

  player.runCommand(`camera "${player.name}" fade time 0.5 1 1 color 64 7 0`);
  // Play entrance sound
  player.runCommand(`playsound float_ad.evil_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}" 76.56 11.00 73.64 facing 64.95 13.27 70.54`;

  // Check if any player is already in the hub
  const playersInHub = world.getPlayers().filter((p) => p.hasTag("evil_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`
      );
    }, 10);

    // Load the new structure
    system.runTimeout(() => {
      const loadCommand = `structure load "${structureName}" ${position.x} ${position.y} ${position.z} 0_degrees none true true false`;
      world.getDimension("overworld").runCommand(loadCommand);
      player.runCommand(`camera "${player.name}" clear`);
    }, 12);
  }

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

export function evilHubRemove(player) {
  const tempStructureName = "temp_evil_structure_save";
  const position = { x: 50, y: 0, z: 50 };

  player.removeTag("evil_hub");

  // Check if any player is still in the hub
  const playersInHub = world.getPlayers().filter((p) => p.hasTag("evil_hub"));
  if (playersInHub.length === 0) {
    // Load the temporary structure to remove the hub
    system.runTimeout(() => {
      const loadCommand = `structure load "${NAMESPACE}.${tempStructureName}" ${position.x} ${position.y} ${position.z}`;
      world.getDimension("overworld").runCommand(loadCommand);
    }, 10);
    world
      .getDimension("overworld")
      .runCommand(`event entity @e[type=float_ad:altar_wisp,x=66,y=7,z=85,r=4] float_ad:kill_entity`);
    world
      .getDimension("overworld")
      .runCommand(`event entity @e[type=float_ad:settings_hub,x=66,y=7,z=85,r=80] float_ad:kill_entity`);
    world
      .getDimension("overworld")
      .runCommand(`event entity @e[type=float_ad:translate_hub,x=66,y=7,z=85,r=80] float_ad:kill_entity`);
    world
      .getDimension("overworld")
      .runCommand(`event entity @e[type=float_ad:book_hub,x=66,y=7,z=85,r=80] float_ad:killevent`);
    world
      .getDimension("overworld")
      .runCommand(`event entity @e[type=float_ad:hub_tp_evil,x=66,y=7,z=85,r=80] float_ad:despawn`);
    world
      .getDimension("overworld")
      .runCommand(`event entity @e[type=float_ad:hooded_figure_evil,x=66,y=7,z=85,r=60] float_ad:despawn`);
  }

  // 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}`;
    player.runCommand(`camera "${player.name}" fade time 0.5 1 1 color 64 7 0`);
    // Play exit sound
    player.runCommand(`playsound float_ad.hub_exit_evil "${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.removeTag(locationTag);
  }
}
