import { world, system, Dimension } from "@minecraft/server";


  
  //Magna ball
  system.beforeEvents.startup.subscribe((event) => {
    event.itemComponentRegistry.registerCustomComponent('yokai:ball_magna', {
      onUse: use => {
        if (use.source.hasTag("no_skill")) return
        const player = use.source
        const mana = player.getDynamicProperty("mana")
        if (mana >= 1) {
          player.triggerEvent("ball_magna")
          player.setDynamicProperty("mana", mana - 1)
          player.triggerEvent("atk_ball_magna")
          system.runTimeout(() => {
            player.triggerEvent("atk_none")
          }, 10)
        }
      }
    })
  });
  
  //Bal Scatter Magna
  system.beforeEvents.startup.subscribe((event) => {
    event.itemComponentRegistry.registerCustomComponent('yokai:ball_scatter_magna', {
      onUse: use => {
      if (use.source.hasTag("no_skill")) return
        const player = use.source
        const mana = player.getDynamicProperty("mana")
        if (mana >= 5) {
          player.triggerEvent("ball_scatter_magna")
          player.setDynamicProperty("mana", mana - 5)
          player.triggerEvent("atk_ball_magna")
          system.runTimeout(() => {
            player.triggerEvent("atk_none")
          }, 10)
        }
      }
    })
  });
system.afterEvents.scriptEventReceive.subscribe((data) => {
    if (data.sourceEntity.typeId == "yokai:ball_magna" && data.id == "yokai:damage") {
      let timer = data.sourceEntity.getDynamicProperty("timer") ?? 0
      data.sourceEntity.setDynamicProperty("timer", timer + 1)
      timer = data.sourceEntity.getDynamicProperty("timer") ?? 0
      const player = data.sourceEntity.getComponent("minecraft:projectile")
      const mobs = data.sourceEntity.dimension.getEntities({
        location: data.sourceEntity.location,
        maxDistance: 2,
        excludeTypes: ["item"],
      })
      let acertou = false
      const maxX = 2
      const maxY = 1.5
      if (timer >= 48) {
        data.sourceEntity.triggerEvent("explosão")  
      } else {
        
        for (const mob of mobs) {
          if (mob.id != data.sourceEntity.id && mob.id != player.owner.id && mob.typeId != "minecraft:xp_orb") {
            const position = mob.location
            const dx = position.x - data.sourceEntity.location.x;
            const dy = position.y - data.sourceEntity.location.y;
            const dz = position.z - data.sourceEntity.location.z;
          
            const baseX = Math.sqrt(dx * dx + dz * dz);
            const baseY = Math.abs(dy);
            if (baseX <= maxX && baseY <= maxY) {
              mob.applyDamage(15, {
                damagingEntity: player.owner,
                damagingProjectile: data.sourceEntity,
              })
              acertou = true  
              if (mob.typeId == "yokai:dragon_nest") break
            }
          }
        }
        if (acertou) {
          data.sourceEntity.triggerEvent("explosão")  
        }
      }
    }
  })
  
  //Grand Slam Spin
  system.beforeEvents.startup.subscribe((event) => {
    event.itemComponentRegistry.registerCustomComponent('yokai:grand_slam_spin', {
      onUse: use => {
        if (use.source.hasTag("no_skill")) return
        const player = use.source
        const mana = player.getDynamicProperty("mana")
        if (mana >= 3) {
          player.triggerEvent("grand_slam_spin")
          player.spawnParticle("yokai:spin_fire", player.location)
          player.dimension.playSound("spin", player.location)
          player.setDynamicProperty("mana", mana - 3)
          const mobs = player.dimension.getEntities({
            location: player.location,
            maxDistance: 3.2,
          })
          for (const mob of mobs) {
            if (mob.id !== player.id) {
             mob.applyDamage(10, {
               cause: "entityAttack",
               damagingEntity: player,
             })
              const dx = mob.location.x - player.location.x;
              const dz = mob.location.z - player.location.z;
              const distance = Math.sqrt(dx * dx + dz * dz);
              const knockbackX = distance === 0 ? 2.5 : dx / distance;
              const knockbackZ = distance === 0 ? 2.5 : dz / distance;
              mob.applyKnockback({ x: knockbackX * 2.5, z: knockbackZ * 2.5 }, -0.6)
              let loc = mob.location
              loc.y += 1.2
              player.spawnParticle("yokai:fire_impact", loc)
              mob.setOnFire(5)
            }
          }
          system.runTimeout(() => {
            player.triggerEvent("atk_none")
          }, 13)
        }
      }
    })
  });
