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

world.beforeEvents.worldInitialize.subscribe(initEvent => {
  const myceliumSprouts = [
    "nicothekid:mycelium_sprouts_small_block",
    "nicothekid:mycelium_sprouts_tall_block"
  ]
  const boneMeals = [
    "minecraft:bone_meal",
    "nicothekid:glowcelium_bone_meal"
  ]
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:mycelium_sprouts_growth", {
    onPlayerInteract: eventData => {
      const player = eventData.player;
      const block = eventData.block;
      const blockLocation = block.location;
      const playerEquippable = player.getComponent("equippable");
      const itemUsed = playerEquippable.getEquipment("Mainhand");
      function decreaseItem () {
        system.run(() => {
          if (!player.matches({ gameMode: GameMode.creative })) {
            const decrementItem = itemUsed.amount == 1 ? undefined : (itemUsed.amount--, itemUsed);
            playerEquippable.setEquipment("Mainhand", decrementItem)
          }
        });
      }
      if (itemUsed === undefined) return;
      if (myceliumSprouts.includes(block.typeId) && boneMeals.includes(itemUsed.typeId)) {
        if (block.typeId === "nicothekid:mycelium_sprouts_small_block" && itemUsed.typeId === "minecraft:bone_meal") {
          decreaseItem ();
          block.dimension.playSound("item.bone_meal.use", blockLocation, { pitch: 1.0, volume: 1.0 });
          block.dimension.spawnParticle("minecraft:crop_growth_emitter", { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:mycelium_sprouts_tall_block"));
          return;
        }
        if (block.typeId === "nicothekid:mycelium_sprouts_small_block" && itemUsed.typeId === "nicothekid:glowcelium_bone_meal") {
          decreaseItem ();
          block.dimension.playSound("item.bone_meal.use", blockLocation, { pitch: 1.0, volume: 1.0 });
          block.dimension.spawnParticle("minecraft:crop_growth_emitter", { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:glowcelium_sprouts_tall_block"));
          return;
        }
        if (block.typeId === "nicothekid:mycelium_sprouts_tall_block" && itemUsed.typeId === "nicothekid:glowcelium_bone_meal") {
          decreaseItem ();
          block.dimension.playSound("item.bone_meal.use", blockLocation, { pitch: 1.0, volume: 1.0 });
          block.dimension.spawnParticle("minecraft:crop_growth_emitter", { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 });
          block.setPermutation(BlockPermutation.resolve("nicothekid:glowcelium_sprouts_tall_block"));
          return;
        }
      }
      else return;
    }
  });
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:mycelium_sprouts_particles", {
    onRandomTick: eventData => {
      const block = eventData.block;
      if (myceliumSprouts.includes(block.typeId)) {
        if (block.permutation.matches("nicothekid:mycelium_sprouts_tall_block", { "nicothekid:sprouts_particles": false })) {
          block.setPermutation(BlockPermutation.resolve("nicothekid:mycelium_sprouts_tall_block", { "nicothekid:sprouts_particles": true }))
          return;
        }
      }
      else return;
    }
  });
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:mycelium_sprouts_particles_tick", {
    onTick: eventData => {
      const block = eventData.block;
      const blockLocation = block.location;
      if (myceliumSprouts.includes(block.typeId)) {
        if (block.permutation.matches("nicothekid:mycelium_sprouts_tall_block", { "nicothekid:sprouts_particles": true })) {
          block.dimension.spawnParticle("nicothekid:mycelium_sprouts_particle", { x: blockLocation.x + 0.5, y: blockLocation.y + 0.5, z: blockLocation.z + 0.5 })
          return;
        }
      }
      else return;
    }
  });
})
