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

// Function to calculate the angle between two vectors
function calculateAngleBetweenVectors(v1, v2) {
  const dotProduct = v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
  const magnitudeV1 = Math.sqrt(v1.x * v1.x + v1.y * v1.y + v1.z * v1.z);
  const magnitudeV2 = Math.sqrt(v2.x * v2.x + v2.y * v2.y + v2.z * v2.z);
  return Math.acos(dotProduct / (magnitudeV1 * magnitudeV2)) * (180 / Math.PI);
}

export default function evilGreatAxeAbility(player, GreatAxeTier) {
  // Set up the delay
  let particleEffect;
  let damageAmount;
  let radius;
  let knockbackMultiplier;
  let viewAngle = 90;
  particleEffect = "float_ad:evil_axe_smash";

  switch (GreatAxeTier) {
    case 1:
      damageAmount = 10;
      radius = 10;
      knockbackMultiplier = 1;
      break;
    case 2:
      damageAmount = 20;
      radius = 10;
      knockbackMultiplier = 1.5;
      break;
    case 3:
      damageAmount = 30;
      radius = 10;
      knockbackMultiplier = 2;
      break;
    default:
      damageAmount = 10;
      radius = 10;
      knockbackMultiplier = 1;
  }

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

  system.runTimeout(() => {
    let playerPos = player.location;
    let lookingDirection = player.getViewDirection();

    // Calculate the position slightly behind the player
    let behindPlayerPos = {
      x: playerPos.x - lookingDirection.x * 1.5,
      y: playerPos.y,
      z: playerPos.z - lookingDirection.z * 1.5,
    };

    // Summon the entity behind the player facing them
    player.runCommandAsync(
      `summon float_ad:ability ${behindPlayerPos.x} ${behindPlayerPos.y} ${behindPlayerPos.z} facing ${playerPos.x} ${playerPos.y} ${playerPos.z} float_ad:set_evil_great_axe`
    );

    player.runCommandAsync(`particle ${particleEffect} ${playerPos.x} ${playerPos.y + 1.5} ${playerPos.z}`);
  }, 8); // trigger particle effect 3 ticks before damage

  system.runTimeout(() => {
    let playerPos = player.location;
    let lookingDirection = player.getViewDirection();

    const entities = player.dimension.getEntities({
      location: playerPos,
      maxDistance: radius,
      excludeTypes: ["minecraft:player"],
    });

    let affectedEntities = [];

    entities.forEach((entity) => {
      let entityPos = entity.location;
      let directionToEntity = {
        x: entityPos.x - playerPos.x,
        y: entityPos.y - playerPos.y,
        z: entityPos.z - playerPos.z,
      };

      // Calculate the angle between the player's looking direction and the direction to the entity
      let angle = calculateAngleBetweenVectors(lookingDirection, directionToEntity);

      if (angle <= viewAngle / 2) {
        if (combinedMobs.includes(entity.typeId)) {
          affectedEntities.push(entity);
        }
      }
    });

    affectedEntities.forEach((entity) => {
      const { x, z } = entity.location;
      const dx = x - playerPos.x;
      const dz = z - playerPos.z;
      const distance = Math.sqrt(dx * dx + dz * dz);

      // Calculate the impulse direction away from the player
      let impulseX = dx / distance;
      let impulseZ = dz / distance;

      const impulseStrength = knockbackMultiplier;

      entity.applyImpulse({ x: impulseX * impulseStrength, y: 0.5, z: impulseZ * impulseStrength }); // Lowered y value for less vertical impulse

      // Apply damage
      entity.applyDamage(damageAmount, { cause: "starve" });
    });
  }, 11); // delay for damage
}
