import {
  world,
  system,
  ItemStack
} from "@minecraft/server";
world.beforeEvents.itemUseOn.subscribe((event) => {

  const player = event.source;
  const block = event.block

  const equipment = player.getComponent('equippable');
  const selectedItem = equipment.getEquipment('Mainhand');

  if (!selectedItem || !player || !block) {
    return;
  }

  if (selectedItem.typeId === 'minecraft:glass_bottle' && block.typeId === 'minecraft:sculk_catalyst') {
    system.run(() => {
      if (!player.matches({
          gameMode: 'creative'
        })) {
        if (selectedItem.amount > 1) {
          selectedItem.amount -= 1;
          equipment.setEquipment('Mainhand', selectedItem);
        } else {
          equipment.setEquipment('Mainhand', undefined);
        }
      }
      const soulDew = new ItemStack('we:soul_dew', 1);
        system.runTimeout(() => {
          player.dimension.spawnItem(soulDew, player.location);
        }, 1);

      block.dimension.spawnParticle('we:soul_rise', {
        x: block.center().x,
        y: block.above().y - 0.4,
        z: block.center().z
      })
      block.dimension.playSound('charge.sculk', block.center())
    })
  }

  if (selectedItem.typeId === 'we:soul_dew' && block.typeId === 'we:phozl_dencha_block') {

    system.run(() => {
      if (!player.matches({
          gameMode: 'creative'
        })) {
        if (selectedItem.amount > 1) {
          selectedItem.amount -= 1;
          equipment.setEquipment('Mainhand', selectedItem);
        } else {
          equipment.setEquipment('Mainhand', undefined);
        }
        const bottle = new ItemStack('minecraft:glass_bottle', 1);
        system.runTimeout(() => {
          player.dimension.spawnItem(bottle, player.location);
        }, 1);
      }

      block.dimension.spawnParticle('we:sculk_growth_emitter', {
        x: block.center().x,
        y: block.center().y,
        z: block.center().z
      })
      block.dimension.playSound('break.sculk_vein', block.center())

      if(block.above().isAir) {
        block.above().setType(block.typeId);
      }
    })
  }

  if (selectedItem.typeId === 'we:soul_dew' && block.typeId === 'we:sculk_jaw') {

    system.run(() => {
      if (!player.matches({
          gameMode: 'creative'
        })) {
        if (selectedItem.amount > 1) {
          selectedItem.amount -= 1;
          equipment.setEquipment('Mainhand', selectedItem);
        } else {
          equipment.setEquipment('Mainhand', undefined);
        }
        const bottle = new ItemStack('minecraft:glass_bottle', 1);
        system.runTimeout(() => {
          player.dimension.spawnItem(bottle, player.location);
        }, 1);
      }
      block.dimension.spawnParticle('we:sculk_growth_emitter', {
        x: block.center().x,
        y: block.center().y + 0.5,
        z: block.center().z
      })
      block.dimension.playSound('break.sculk_vein', block.center())

      const direction = Math.floor(Math.random() *4); // 0-3

      if(direction == 0 && block.west().typeId === 'minecraft:sculk') {
        block.west().setType(block.typeId);
        
        const blockFace = block.west().permutation.withState('minecraft:block_face', 'up');
        block.west().setPermutation(blockFace);

      } else if(direction == 1 && block.east().typeId === 'minecraft:sculk') {
        block.east().setType(block.typeId);

        const blockFace = block.east().permutation.withState('minecraft:block_face', 'up');
        block.east().setPermutation(blockFace);

      } else if(direction == 2 && block.north().typeId === 'minecraft:sculk') {
        block.north().setType(block.typeId);

        const blockFace = block.north().permutation.withState('minecraft:block_face', 'up');
        block.north().setPermutation(blockFace);

      } else if(direction == 3 && block.south().typeId === 'minecraft:sculk') {
        block.south().setType(block.typeId);

        const blockFace = block.south().permutation.withState('minecraft:block_face', 'up');
        block.south().setPermutation(blockFace);
      }
    })
  }
})