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

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

system.beforeEvents.startup.subscribe((event) => {
  event.itemComponentRegistry.registerCustomComponent('yokai:boots_luck', {
    onUse: use => {
      if (use.source.hasTag("no_skill")) return
      const player = use.source;
      const mana = player.getDynamicProperty("mana");
      if (!player.hasTag("move")) return;
      if (mana >= 2) {
        player.playSound("dash_lightning")
        player.spawnParticle("yokai:dash_luck", player.location)
        player.setDynamicProperty("mana", mana - 2)
        const velocity = player.getVelocity();
        const view = player.getViewDirection();
        
        const viewLength = Math.sqrt(view.x * view.x + view.z * view.z);
        const normalizedFront = {
          x: view.x / viewLength,
          y: 0,
          z: view.z / viewLength
        };
        
        const right = { 
          x: -normalizedFront.z, 
          y: 0, 
          z: normalizedFront.x 
        }; 
        
        const frontSpeed = velocity.x * normalizedFront.x + velocity.z * normalizedFront.z; 
        const rightSpeed = velocity.x * right.x + velocity.z * right.z; 
    
        const force = 7;

        if (Math.abs(frontSpeed) > Math.abs(rightSpeed)) {
          if (frontSpeed > 0) {
            player.applyKnockback({ 
              x: normalizedFront.x * 8, 
              z: normalizedFront.z * 8
            }, 0.45);
            player.triggerEvent("luck_front");
          } else {
            player.applyKnockback({ 
              x: -normalizedFront.x * force, 
              z: -normalizedFront.z * force 
            }, 0.45);
            player.triggerEvent("luck_back");
          }
        } else {
          if (rightSpeed > 0) {
            player.applyKnockback({ 
              x: right.x * force, 
              z: right.z * force 
            }, 0.45);
            player.triggerEvent("luck_right");
          } else {
            player.applyKnockback({ 
              x: -right.x * force, 
              z: -right.z * force 
            }, 0.45);
            player.triggerEvent("luck_left");
          }
        }
      }
    }
  });
});
 

  //Gloves luck
    system.beforeEvents.startup.subscribe((event) => {
      
    event.itemComponentRegistry.registerCustomComponent('yokai:gloves_luck', {
      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_gloves_luck")
          player.triggerEvent("mob_gloves_luck")
          system.runTimeout(() => {
            player.triggerEvent("atk_none")
          }, 18)
        }
      }
    })
  });
  
  
  
  world.afterEvents.dataDrivenEntityTrigger.subscribe((data) => {
  if (data.entity.typeId == "yokai:gloves_luck_mob" && data.eventId == "explosão") {
    system.runTimeout(() => {
      data.entity.dimension.playSound("gloves_luck_impact", data.entity.location)
    }, 4)
    system.runTimeout(() => {
      data.entity.dimension.spawnParticle("yokai:gloves_thunder", data.entity.location)
      data.entity.dimension.spawnParticle("yokai:gloves_impact", data.entity.location)
      data.entity.dimension.spawnParticle("yokai:gloves_impact2", data.entity.location)
      data.entity.dimension.spawnParticle("yokai:gloves_impact3", data.entity.location)
      const mobs = data.entity.dimension.getEntities({
        location: data.entity.location,
        maxDistance: 5,
      })
      const player = data.entity.getComponent("minecraft:projectile")
      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,
          })
          let loc = mob.location
          if (loc) {
            loc.y += 1.2
            player.owner.spawnParticle("yokai:gloves_damage", loc)
          }
        }
      }
    }, 5)
    system.runTimeout(() => {
      data.entity.triggerEvent("kill")
    }, 10)
  }
})
