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

world.beforeEvents.worldInitialize.subscribe(initEvent => {
  const mossBerryPlants = [
    "nicothekid:moss_berry_stage_1_block",
    "nicothekid:moss_berry_stage_2_block"
  ]
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:moss_berry_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 (mossBerryPlants.includes(block.typeId) && itemUsed.typeId === "minecraft:bone_meal") {
        if (block.typeId === "nicothekid:moss_berry_stage_1_block") {
          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:moss_berry_stage_2_block"));
          return;
        }
        if (block.typeId === "nicothekid:moss_berry_stage_2_block") {
          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:moss_berry_stage_3_block"));
          return;
        }
      }
      else return;
    }
  });
  initEvent.blockTypeRegistry.registerCustomComponent("nicothekid:moss_berry_random_growth", {
    onRandomTick: eventData => {
      const block = eventData.block;
      const blockLocation = block.location;
      const blockBelow = block.below(1);
      if (mossBerryPlants.includes(block.typeId)) {
        let randomChance = Math.random();
        if (block.typeId === "nicothekid:moss_berry_stage_1_block") {
          if (blockBelow.typeId === "minecraft:farmland") {
            if (randomChance <= 0.75) {
              block.setPermutation(BlockPermutation.resolve("nicothekid:moss_berry_stage_2_block"));
              return;
            }
            else return;
          }
          else {
            if (randomChance <= 0.1) {
              block.setPermutation(BlockPermutation.resolve("nicothekid:moss_berry_stage_2_block"));
              return;
            }
            else return;
          }
        }
        if (block.typeId === "nicothekid:moss_berry_stage_2_block") {
          if (blockBelow.typeId === "minecraft:farmland") {
            if (randomChance <= 0.75) {
              block.setPermutation(BlockPermutation.resolve("nicothekid:moss_berry_stage_3_block"));
              return;
            }
            else return;
          }
          else {
            if (randomChance <= 0.1) {
              block.setPermutation(BlockPermutation.resolve("nicothekid:moss_berry_stage_3_block"));
              return;
            }
            else return;
          }
        }
      }
      else return;
    }
  });
});