import {
  world,
  ItemStack,
  system
} from "@minecraft/server";
world.beforeEvents.worldInitialize.subscribe(initEvent => {
  initEvent.blockComponentRegistry.registerCustomComponent('we:tumbleweed_growth', {
    onRandomTick(e) {
      const {
        block
      } = e;

      if (block.typeId !== 'we:tumbleweed_stem_block') return;

      const random = Math.floor(Math.random() * 9);
      if (random !== 0) return;

      const permutation = block.permutation;

        const newPermutation = permutation.withState('we:growth', permutation.getState('we:growth')+1);
        block.setPermutation(newPermutation);
      
    },
    onPlayerInteract(e) {

      const block = e.block.center();
      const player = e.player;
      const equipment = player.getComponent('equippable');
      const selectedItem = equipment.getEquipment('Mainhand');
      if (!selectedItem) return;
      if (selectedItem.typeId !== 'minecraft:bone_meal') return;
      if (!e.player.matches({gameMode: 'creative'})) {
            if (selectedItem.amount > 1) {
              selectedItem.amount -= 1;
              equipment.setEquipment('Mainhand', selectedItem);
            } else {
              equipment.setEquipment('Mainhand', undefined);
            }
          }

        const permutation = e.block.permutation;
        const newPermutation = permutation.withState('we:growth', permutation.getState('we:growth')+1);
        e.block.setPermutation(newPermutation);

e.dimension.spawnParticle('minecraft:crop_growth_emitter', block)
      e.dimension.playSound('item.bone_meal.use', block)
    }
  });

initEvent.blockComponentRegistry.registerCustomComponent('we:tumbleweed_harvest', {
    onPlayerInteract(e) {
      const block = e.block.center();
      const player = e.player;

      const permutation = e.block.permutation;
      const newPermutation = permutation.withState('we:growth', 1);
      e.block.setPermutation(newPermutation);

      const tumbleweed = new ItemStack('we:tumbleweed', 1);
        system.runTimeout(() => {
          e.dimension.spawnItem(tumbleweed, block);
        }, 1);
        e.dimension.playSound('block.sweet_berry_bush.pick', block)
    }
  });

initEvent.blockComponentRegistry.registerCustomComponent('we:tumbleweed_loot', {
    onPlayerDestroy(e) {
      if (e.player.matches({
          gameMode: 'creative'
        })) return;
      const block = e.block.center();
      const player = e.player;
      const heldItem = player.getComponent("minecraft:equippable").getEquipment("Mainhand");
      if (!heldItem) {
        return;
      } else if (heldItem.typeId !== 'minecraft:shears') {
        return;
      } else {
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/tumbleweed_stem\"`)
      }
    }
  });
initEvent.blockComponentRegistry.registerCustomComponent('we:tumbleweed_loot_sticks', {
    onPlayerDestroy(e) {
      if (e.player.matches({
          gameMode: 'creative'
        })) return;
      const block = e.block.center();
      const player = e.player;
      const heldItem = player.getComponent("minecraft:equippable").getEquipment("Mainhand");
      if (!heldItem) {
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/fancy_dead_bush_sticks\"`)
      } else if (heldItem.typeId !== 'minecraft:shears') {
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/fancy_dead_bush_sticks\"`)
      } else {
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/tumbleweed_stem\"`)
      }
    }
  });
initEvent.blockComponentRegistry.registerCustomComponent('we:tumbleweed_loot_grown', {
    onPlayerDestroy(e) {
      if (e.player.matches({
          gameMode: 'creative'
        })) return;
      const block = e.block.center();
      const player = e.player;
      const heldItem = player.getComponent("minecraft:equippable").getEquipment("Mainhand");
      if (!heldItem) {
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/fancy_dead_bush_sticks\"`)
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/tumbleweed\"`)
      } else if (heldItem.typeId !== 'minecraft:shears') {
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/fancy_dead_bush_sticks\"`)
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/tumbleweed\"`)
      } else {
        e.dimension.runCommandAsync(`loot spawn ${block.x} ${block.y} ${block.z} loot \"blocks/tumbleweed_stem_grown\"`)
      }
    }
  });
});