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

  
  //Ball yuno
  system.beforeEvents.startup.subscribe((event) => {
    event.itemComponentRegistry.registerCustomComponent('yokai:ball_yuno', {
      onUse: use => {
        if (use.source.hasTag("no_skill")) return
        const player = use.source
        const mana = player.getDynamicProperty("mana")
        if (mana >= 1) {
          player.triggerEvent("ball_yuno")
          player.playSound("shoot_air")
          player.setDynamicProperty("mana", mana - 1)
          player.triggerEvent("atk_ball_luck")
          system.runTimeout(() => {
            player.triggerEvent("atk_none")
          }, 10)
        }
      }
    })
  });
  

  //Moon yuno
  system.beforeEvents.startup.subscribe((event) => {
    event.itemComponentRegistry.registerCustomComponent('yokai:moon_yuno', {
      onUse: use => {
        if (use.source.hasTag("no_skill")) return
        const player = use.source
        const mana = player.getDynamicProperty("mana")
        if (mana >= 3) {
          player.triggerEvent("moon_yuno")
          player.playSound("shoot_moon")
          player.setDynamicProperty("mana", mana - 3)
          player.triggerEvent("atk_moon_yuno")
          system.runTimeout(() => {
            player.triggerEvent("atk_none")
          }, 10)
        }
      }
    })
  });
  system.afterEvents.scriptEventReceive.subscribe((data) => {
    if (data.sourceEntity.typeId == "yokai:moon_yuno" && 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: 5.5,
        excludeTypes: ["item"],
      })
      let acertou = false
      const maxX = 5.5
      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(10, {
                damagingEntity: player.owner,
                damagingProjectile: data.sourceEntity,
              })
              acertou = true  
              if (mob.typeId == "yokai:dragon_nest") break
            }
          }
        }
        if (acertou) {
          data.sourceEntity.triggerEvent("explosão")  
        }
      }
    }
  })

  //Tornado yuno
  system.beforeEvents.startup.subscribe((event) => {
    event.itemComponentRegistry.registerCustomComponent('yokai:tornado_yuno', {
      onUse: use => {
        if (use.source.hasTag("no_skill")) return
        const player = use.source
        const mana = player.getDynamicProperty("mana")
        if (mana >= 7) {
          player.setDynamicProperty("mana", mana - 7)
          player.triggerEvent("atk_ball_luck")
          player.triggerEvent("tornado_yuno")
          system.runTimeout(() => {
            player.triggerEvent("atk_none")
          }, 10)
        }
      }
    })
  });
  


world.afterEvents.dataDrivenEntityTrigger.subscribe((data) => {
  if (data.entity.typeId == "yokai:tornado_yuno_mob" && data.eventId == "explosão") {
    data.entity.dimension.playSound("tornado_yuno", data.entity.location)
    const mobs = data.entity.dimension.getEntities({
      location: data.entity.location,
      maxDistance: 5,
    })
    const player = data.entity.getComponent("minecraft:projectile")
    data.entity.dimension.spawnParticle("yokai:tornado_yuno", data.entity.location)
    for (const mob of mobs) {
      if (mob.id != data.entity.id && mob.id != player.owner.id && !mob.hasTag("no_damage")) {
        mob.applyDamage(30, {
           cause: "entityAttack",
           damagingEntity: player.owner,
        })
        mob.applyKnockback({ 
          x: 0,
          z: 0
        }, 1.7);
      }
    }
    data.entity.triggerEvent("kill")
  }
})