import { getRandomFloat } from "./utils/helpers"
import { diamondSwordSpecialOnuse } from "./weaponEffectHandler"
const bowProperties = {
  "4ks_wp:bedrock_bow_special": {
    projectileId: "4ks_wp:bedrock_tornado_arrow",
    speedMultiplier: 1.0
  },
  "4ks_wp:copper_bow_special": {
    projectileId: "4ks_wp:electric_ball",
    speedMultiplier: 0
  },
  "4ks_wp:demon_bow_special": {
    projectileId: "4ks_wp:demon_arrow",
    speedMultiplier: 1.0
  },
  "4ks_wp:divine_bow_special": {
    projectileId: "4ks_wp:divine_arrow",
    speedMultiplier: 1.0
  },
  "4ks_wp:dragon_bow_special": {
    projectileId: "4ks_wp:dragon_ender",
    speedMultiplier: 0.6
  },
  "4ks_wp:emerald_bow_special": {
    projectileId: "4ks_wp:emerald_arrow",
    speedMultiplier: 1.0
  },
  "4ks_wp:lapis_bow_special": {
    projectileId: "4ks_wp:lapis_projectile_bow",
    speedMultiplier: 1.0
  },
  "4ks_wp:platinum_bow_special": {
    projectileId: "4ks_wp:platinum_projectile_bow",
    speedMultiplier: 1.0
  },
  "4ks_wp:ruby_bow_special": {
    projectileId: "4ks_wp:ruby_arrow",
    speedMultiplier: 1.0
  },
  "4ks_wp:steel_bow_special": {
    projectileId: "4ks_wp:steel_arrow",
    speedMultiplier: 1.0
  },
  "4ks_wp:sculk_bow_special": {
    projectileId: "4ks_wp:sculk_arrow",
    speedMultiplier: 1.0
  },
  "4ks_wp:diamond_bow_special": {
    projectileId: "4ks_wp:diamond_arrow",
    speedMultiplier: 8.0
  },
  "4ks_wp:obsidian_bow_special": {
    projectileId: "4ks_wp:obsidian_arrow",
    speedMultiplier: 1.0
  }
}

export async function specialBowHandler(bowType, e, player, haveInfinity) {
  if (!bowProperties[bowType]) return
  if (bowType == "4ks_wp:diamond_bow_special") {
    diamondSwordSpecialOnuse(player)
    return
  }
  const { projectileId, speedMultiplier } = bowProperties[bowType]
  const maxDuration = 19999980
  const useTick = Math.min(20, maxDuration - e.useDuration)

  if (useTick <= 5) return
  const minTick = 1
  const maxTick = 20

  const clampedUseTick = Math.min(Math.max(useTick, minTick), maxTick)
  const scale = (clampedUseTick - minTick) / (maxTick - minTick)
  let power = (0.25 + (5 - 0.25) * scale) * speedMultiplier

  const velocity = {
    x: player.getViewDirection().x * power,
    y: player.getViewDirection().y * power,
    z: player.getViewDirection().z * power,
  }
  const headLocation = player.getHeadLocation()
  const viewDirection = player.getViewDirection()
  const projectile = player.dimension.spawnEntity(projectileId, {
    x: headLocation.x + viewDirection.x * 1,
    y: headLocation.y + viewDirection.y * 1,
    z: headLocation.z + viewDirection.z * 1
  })

  const projectileComp = projectile.getComponent('minecraft:projectile')
  projectileComp.owner = player

  if (bowType === "4ks_wp:ruby_bow_special") {
    for (let i = 0; i < 7; i++) {
      const scatterVelocity = {
        x: velocity.x + (Math.random() - 0.5) * 2, // Adjust scatter intensity
        y: velocity.y + (Math.random() - 0.5) * 2,
        z: velocity.z + (Math.random() - 0.5) * 2,
      };

      // Normalize the velocity to maintain consistent speed
      const magnitude = Math.sqrt(
        scatterVelocity.x ** 2 +
        scatterVelocity.y ** 2 +
        scatterVelocity.z ** 2
      );

      scatterVelocity.x = (scatterVelocity.x / magnitude) * 5;
      scatterVelocity.y = (scatterVelocity.y / magnitude) * 5;
      scatterVelocity.z = (scatterVelocity.z / magnitude) * 5;

      const scatterProjectile = player.dimension.spawnEntity("4ks_wp:ruby_arrow", {
        x: headLocation.x + viewDirection.x * 1.5,
        y: headLocation.y + viewDirection.y * 1.5,
        z: headLocation.z + viewDirection.z * 1.5,
      });

      const scatterProjectileComp = scatterProjectile.getComponent('minecraft:projectile');
      scatterProjectileComp.owner = player;

      scatterProjectileComp?.shoot(scatterVelocity);
    }
  }

  projectileComp?.shoot(velocity)

  if (bowType === "4ks_wp:sculk_bow_special") {
    player.runCommand("/playsound mob.warden.sonic_boom @a[r=12] ~~~")
  } else if (bowType === "4ks_wp:lapis_bow_special") {
    player.runCommand("/playsound 4ks_wp.lapis_onuse @a[r=12] ~~~")
  } else if (bowType === "4ks_wp:ruby_bow_special") {
    player.runCommand("/playsound 4ks_wp.ruby_onuse @a[r=8] ~~~ 0.5")
  } else {
    player.runCommand("/playsound random.bow @a[r=12] ~~~")
  }
  if (!haveInfinity) {
    player.runCommand("/clear @s minecraft:arrow 0 1")
  }
}
