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

  
  //Water ball
  system.beforeEvents.startup.subscribe((event) => {
    event.itemComponentRegistry.registerCustomComponent('yokai:water_ball', {
      onUse: use => {
        if (use.source.hasTag("no_skill")) return
        const player = use.source
        const mana = player.getDynamicProperty("mana")
        const exam = player.getDynamicProperty("exam")
        if (mana >= 2) {
          player.triggerEvent("water_ball")
          player.playSound("shoot_water")
          if (exam == 1) {
          player.setDynamicProperty("mana", mana - 1)
          } else {
          player.setDynamicProperty("mana", mana - 2)
          }
          player.triggerEvent("atk_water_ball")
          system.runTimeout(() => {
            player.triggerEvent("atk_none")
          }, 10)
        }
      }
    })
  });
  
  
  //Dragon Nest
  
  system.beforeEvents.startup.subscribe((event) => {
    event.itemComponentRegistry.registerCustomComponent('yokai:dragon_nest', {
      onUse: use => {
        if (use.source.hasTag("no_skill")) return
        const player = use.source
        const mana = player.getDynamicProperty("mana")
        if (mana >= 5) {
          const entity = player.dimension.spawnEntity("yokai:dragon_nest", player.location)
          player.setDynamicProperty("mana", mana - 5)
          player.triggerEvent("atk_dragon_nest")
          system.runTimeout(() => {
            player.triggerEvent("atk_none")
          }, 20)
        }
      }
    })
  });
  
  world.afterEvents.entityHurt.subscribe((event) => {
    if (event.hurtEntity.typeId == "yokai:dragon_nest") {
      const item = event.damageSource.damagingEntity.getComponent("inventory").container.getItem(event.damageSource.damagingEntity.selectedSlotIndex);
      if (event.damageSource.damagingProjectile) {
        if (event.damageSource.damagingProjectile.typeId == "yokai:dweller_anti") {
          event.hurtEntity.dimension.spawnParticle("yokai:dragon_nest_splash", event.hurtEntity.location)
          event.hurtEntity.triggerEvent("sumir")
        }
      } else if (item) {
        if (item.typeId == "yokai:slayer_asta" || item.typeId === "yokai:dweller_asta" || item.typeId === "yokai:destroyer_asta") {
          const hit = event.hurtEntity.getDynamicProperty("hit") ?? 0
          event.hurtEntity.setDynamicProperty("hit", hit + 2)
          if (hit + 2 >= 5) {
            event.hurtEntity.triggerEvent("kill")
            event.hurtEntity.dimension.spawnParticle("yokai:dragon_nest_splash", event.hurtEntity.location)
            system.runTimeout(() => {
              event.hurtEntity.triggerEvent("sumir")
            }, 4)
          }
        } else {
          const hit = event.hurtEntity.getDynamicProperty("hit") ?? 0
          event.hurtEntity.setDynamicProperty("hit", hit + 1)
          if (hit + 1 >= 5) {
            event.hurtEntity.triggerEvent("kill")
            event.hurtEntity.dimension.spawnParticle("yokai:dragon_nest_splash", event.hurtEntity.location)
            system.runTimeout(() => {
              event.hurtEntity.triggerEvent("sumir")
            }, 4)
          }
        }
      } else {
        const hit = event.hurtEntity.getDynamicProperty("hit") ?? 0
        event.hurtEntity.setDynamicProperty("hit", hit + 1)
        if (hit + 1 >= 5) {
          event.hurtEntity.triggerEvent("kill")
          event.hurtEntity.dimension.spawnParticle("yokai:dragon_nest_splash", event.hurtEntity.location)
          system.runTimeout(() => {
            event.hurtEntity.triggerEvent("sumir")
          }, 4)
        }
      }
    }
  })
  
  //Dragon roar

  system.beforeEvents.startup.subscribe((event) => {
    event.itemComponentRegistry.registerCustomComponent('yokai:dragon_roar', {
      onUse: use => {
        if (use.source.hasTag("no_skill")) return
        const player = use.source
        const mana = player.getDynamicProperty("mana")
        if (mana >= 7) {
          player.triggerEvent("dragon_roar")
          player.playSound("shoot_water")
          player.dimension.playSound("dragon_roar", player.location)
          player.setDynamicProperty("mana", mana - 7)
          player.triggerEvent("atk_water_ball")
          system.runTimeout(() => {
            player.triggerEvent("atk_none")
          }, 10)
        }
      }
    })
  });
system.afterEvents.scriptEventReceive.subscribe((data) => {
    if (data.sourceEntity.typeId == "yokai:dragon_roar" && 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 = 4
      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(40, {
                damagingEntity: player.owner,
                damagingProjectile: data.sourceEntity,
              })
              acertou = true  
              if (mob.typeId == "yokai:dragon_nest") break
            }
          }
        }
        if (acertou) {
          data.sourceEntity.triggerEvent("explosão")  
        }
      }
    }
  })
  

