import { world, BlockPermutation } from "@minecraft/server";
import { EffectTypes } from "@minecraft/server";
import { castSpellProjectile } from "./Spells";
import { namespace } from "../Namespace";
import { Vector3 } from "../Vector3";

//Function run when using the spell
export function iceSpell(player) {
  player.sendMessage(`<${player.nameTag}> Ice spell!`);
  castSpellProjectile(player, "blockception_magic:ice_spell", 2);
}

function blockIsWater(block) {
  if (block.typeId == "minecraft:water" || block.typeId == "minecraft:flowing_water") {
    return true;
  } else {
    return false;
  }
}

function setIceAndSnow(location, dimension, radius) {
  //Snow handling
  const centreX = Math.floor(location.x) + 0.5;
  const centreZ = Math.floor(location.z) + 0.5;
  const centreY = location.y;

  for (let angle = 0; angle < 360; angle += 20) {
    for (let distance = 0; distance <= radius; distance++) {
      const radAngle = angle * (Math.PI / 180);
      let snowX = centreX + distance * Math.cos(radAngle);
      let snowZ = centreZ + distance * Math.sin(radAngle);
      let snowLocation = new Vector3(snowX, centreY, snowZ);
      const snowBlock = world.getDimension(dimension.id).getBlock(snowLocation);
      const blockBelow = world.getDimension(dimension.id).getBlock(new Vector3(snowLocation.x, snowLocation.y - 1, snowLocation.z));
      const blockAbove = world.getDimension(dimension.id).getBlock(new Vector3(snowLocation.x, snowLocation.y + 1, snowLocation.z));
      //Replace air with snow
      if (blockIsWater(snowBlock)) {
        snowBlock.setPermutation(BlockPermutation.resolve("minecraft:packed_ice"));
        if (blockAbove.typeId == "minecraft:air") {
          blockAbove.setPermutation(BlockPermutation.resolve("minecraft:snow_layer", {"height": Math.round(Math.random() * 2)}));
        }
      } else if (snowBlock.typeId == "minecraft:air") {
        snowBlock.setPermutation(BlockPermutation.resolve("minecraft:snow_layer", {"height": Math.round(Math.random() * 2)}));
      }
      if (blockIsWater(blockBelow)) {
        blockBelow.setPermutation(BlockPermutation.resolve("minecraft:packed_ice"));
      }
    }
  }
}

export function iceSpellEvent(location, dimension) {
  //Set blocks
  setIceAndSnow(location, dimension, 2.5)
}

export function iceHurtSpellEvent(location, dimension) {
  //Get entities within 2.5 blocks
  const entities = world.getDimension(dimension.id).getEntities({
    location: location,
    maxDistance: 2.5,
  });
  //Remove any undefined entities or other spells nearby
  for ( let x = entities.length - 1; x >= 0; x-- ) {
    if (entities[x] === undefined) {
      entities.splice(x, 1);
    }
    if (entities[x].typeId.startsWith(namespace) && entities[x].typeId.endsWith("_spell")) {
      entities.splice(x, 1);
    }
  };
  //Apply effects to the entities
  entities.forEach(entity => {
    entity.addEffect(EffectTypes.get('slowness'), 100, {amplifier: 2, showParticles: false});
    entity.addEffect(EffectTypes.get('weakness'), 50, {amplifier: 1, showParticles: false});
  });

  //Set blocks
  setIceAndSnow(location, dimension, 1.5);
}