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


world.afterEvents.itemUse.subscribe((eventData) => {
    let player = eventData.source
    let itemUse = eventData.itemStack.typeId
    if (itemUse == "edx:dashing_stick" && getScore( player, 'dash') > 0 && getScore( player, 'dash') < 20 ) {
      player.runCommand(`titleraw @s actionbar { "rawtext": [ { "text": "§4Wait 1 second!" }]}`)
    }
    if (itemUse == "edx:dashing_stick" && getScore( player, 'dash') >= 20 && getScore( player, 'dash') < 40 ) {
      player.runCommand(`titleraw @s actionbar  { "rawtext": [ { "text": "§4Wait 2 seconds!" }]}`)
    }
    if (itemUse == "edx:dashing_stick" && getScore( player, 'dash') >= 40 && getScore( player, 'dash') < 60 ) {
      player.runCommand(`titleraw @s actionbar  { "rawtext": [ { "text": "§4Wait 3 seconds!" }]}`)
    }
    if (itemUse == "edx:dashing_stick" && getScore( player, 'dash') >= 60 && getScore( player, 'dash') < 80 ) {
      player.runCommand(`titleraw @s actionbar { "rawtext": [ { "text": "§4Wait 4 seconds!" }]}`)
    }
    if (itemUse == "edx:dashing_stick" && getScore( player, 'dash') >= 80 && getScore( player, 'dash') < 100 ) {
      player.runCommand(`titleraw @s actionbar  { "rawtext": [ { "text": "§4Wait 5 seconds!" }]}`)
    }
    if (itemUse == "edx:dashing_stick" && !player.hasTag("ground") ) {
      player.runCommand(`titleraw @s actionbar  { "rawtext": [ { "text": "§4You must dash on ground!" }]}`)
    }
    
    if (itemUse == "edx:dashing_stick" && !player.hasTag("void_socks") &&  getScore( player, 'dash') == 0 && player.hasTag("ground")) {
        player.applyKnockback(player.getViewDirection().x, player.getViewDirection().z, Math.sqrt(player.getViewDirection().x ** 2 + player.getViewDirection().z ** 2)*10, player.getViewDirection().y)
        player.runCommand(`scoreboard players set @s dash 80`)
        player.runCommand(`playanimation @s animation.player.dashing_stick_use `)
        var container = player.getComponent('inventory').container;
        let inv = player.getComponent( 'inventory' ).container;
        const itemStack = inv.getItem(player.selectedSlot);
        if (itemStack?.getComponent("durability").damage < 50) {
          itemStack.getComponent("durability").damage = itemStack.getComponent("durability").damage +1;
          container.setItem(player.selectedSlot, itemStack);
        }
        if (itemStack?.getComponent("durability").damage == 50) {
          player.runCommand(`replaceitem entity @s slot.weapon.mainhand 0 minecraft:air 1`)
          player.runCommand(`playsound random.break @a[r=20] ~~~ `)
        }
      }
      
      if (itemUse == "edx:dashing_stick" && player.hasTag("void_socks") &&  getScore( player, 'dash') == 0 && player.hasTag("ground")) {
        player.applyKnockback(player.getViewDirection().x, player.getViewDirection().z, Math.sqrt(player.getViewDirection().x ** 2 + player.getViewDirection().z ** 2)*13, player.getViewDirection().y*1.3)
        player.runCommand(`scoreboard players set @s dash 40`)
        player.runCommand(`playanimation @s animation.player.dashing_stick_use `)
        var container = player.getComponent('inventory').container;
        let inv = player.getComponent( 'inventory' ).container;
        const itemStack = inv.getItem(player.selectedSlot);
        if (itemStack?.getComponent("durability").damage < 50) {
          itemStack.getComponent("durability").damage = itemStack.getComponent("durability").damage +1;
          container.setItem(player.selectedSlot, itemStack);
        }
        if (itemStack?.getComponent("durability").damage == 50) {
          player.runCommand(`replaceitem entity @s slot.weapon.mainhand 0 minecraft:air 1`)
          player.runCommand(`playsound random.break @a[r=20] ~~~ `)
        }
      }
  });

  function getScore(player, objective) {
    try {
      const s = world.scoreboard.getObjective(objective);
      if (!s) world.scoreboard.addObjective(objective, objective);
      return s.getScore(player.scoreboardIdentity);
    } catch {
      return 0;
    }
   }