import { system } from "@minecraft/server";
import { combinedMobs } from "../variables.js";

export default function evilStaffAbility(player, staffTier) {
  // Notify the player and set up the delay
  const blockHit = player.getBlockFromViewDirection({
    maxDistance: 50, // Max distance the player can target
    includeLiquidBlocks: true, // Include liquid blocks in the raycast
    includePassableBlocks: true, // Include passable blocks like air or torches
  });

  if (!blockHit) {
    return;
  }

  const blockLocation = blockHit.block.location;

  let particleEffect;
  let damageAmount;

  switch (staffTier) {
    case 1:
      particleEffect = "float_ad:dark_ring_tier_1";
      damageAmount = 10;
      break;
    case 2:
      particleEffect = "float_ad:dark_ring_tier_2";
      damageAmount = 20;
      break;
    case 3:
      particleEffect = "float_ad:dark_ring_tier_3";
      damageAmount = 30;
      break;
    default:
      particleEffect = "float_ad:dark_ring_1";
      damageAmount = 10;
  }

  // Play sound
  player.playSound("float_ad.staff_ability_evil");

  player.runCommandAsync(`particle ${particleEffect} ${blockLocation.x} ${blockLocation.y + 1.5} ${blockLocation.z}`);

  system.runTimeout(() => {
    // Get the block the player is looking at
    if (blockHit) {
      // Apply damage to entities within an 8-block radius around the target block
      const entities = player.dimension.getEntities({
        location: blockLocation,
        maxDistance: 8,
      });

      for (const entity of entities) {
        if (combinedMobs.includes(entity.typeId)) {
          entity.applyDamage(damageAmount, { cause: "starve" });
        }
      }

      // Trigger particle effect at the target block
      player.runCommandAsync(
        `particle float_ad:fire_strike_flames ${blockLocation.x} ${blockLocation.y + 1} ${blockLocation.z}`
      );
    } else {
    }
  }, 10); // 0.5-second delay
}
