import { world, system, BlockPermutation, EquipmentSlot, ItemStack } from "@minecraft/server";
import { Vector3 } from "../Vector3";
import { spawnParticleAtPlayer } from "../SpawnParticle";

const lightPlayers = [];
const previousLocation = new Map();

//Function run when using the spell
export function lightSpell(player) {
  lightPlayers.push(player);
  player.sendMessage(`<${player.nameTag}> Light spell!`);
  //Trigger light particles
  const item = player.getComponent("equippable").getEquipmentSlot(EquipmentSlot.Offhand);
  let offhandItem;
  if (item) {
    offhandItem = item.getItem();
  }
  player.runCommand("/execute as @s at @s run replaceitem entity @s slot.weapon.offhand 0 blockception_magic:light_spell");
  const particleLoc = new Vector3(player.location.x, player.location.y + 1, player.location.z);
  spawnParticleAtPlayer(player, "blockception_magic:light_on", particleLoc);
  spawnParticleAtPlayer(player, "blockception_magic:light_on_fog", particleLoc);
  //Actual code
  system.runTimeout(() => {
    if (offhandItem) {
      player.getComponent("equippable").getEquipmentSlot(EquipmentSlot.Offhand).setItem(offhandItem);
    } else {
      player.runCommand("/execute as @s at @s run replaceitem entity @s slot.weapon.offhand 0 air");
    }
  }, 2);
  //Timeout
  system.runTimeout(() => {
    //Particles
    spawnParticleAtPlayer(player, "blockception_magic:light_off", particleLoc);
    spawnParticleAtPlayer(player, "blockception_magic:light_off_fog", particleLoc);
    //Remove from map
    lightPlayers.splice(lightPlayers.indexOf(player), 1);
    //Remove final light block
    let finalBlock = world.getDimension(player.dimension.id).getBlock(previousLocation.get(player.id));
    if (finalBlock.typeId == "minecraft:light_block_14") {
      finalBlock.setPermutation(BlockPermutation.resolve("minecraft:air"));
    }
  }, 200)
}

export function setLightBlocks() {
  lightPlayers.forEach((player) => {
    const location = new Vector3(
      Math.floor(player.location.x),
      Math.floor(player.location.y + 1),
      Math.floor(player.location.z)
    );

    const dimension = player.dimension;
    const currentBlock = dimension.getBlock(location);

    // Set the light block at the current location
    if (currentBlock.typeId !== "minecraft:light_block_14") {
      currentBlock.setPermutation(BlockPermutation.resolve("minecraft:light_block_14"));
    }

    // Cleanup old block
    if (previousLocation.has(player.id)) {
      const previousLoc = previousLocation.get(player.id);

      // Remove light block at the old location
      if (
        previousLoc.x !== location.x ||
        previousLoc.y !== location.y ||
        previousLoc.z !== location.z
      ) {
        const previousBlock = dimension.getBlock(previousLoc);
        if (previousBlock.typeId === "minecraft:light_block_14") {
          previousBlock.setPermutation(BlockPermutation.resolve("minecraft:air"));
        }
      }
    }

    // Update previous location
    previousLocation.set(player.id, location);
  });
}
