// Import necessary modules from Minecraft server API
import { world, ItemStack, BlockPermutation } from "@minecraft/server";

const boneBlocks = ["hb_hr:bone_pile_b2_0", "hb_hr:bone_pile_b2_1", "hb_hr:bone_pile_b2_2", "hb_hr:bone_pile_small_0", "hb_hr:bone_pile_small_1", "hb_hr:bone_pile_small_2", "hb_hr:bone_pile_small_3", "hb_hr:bone_pile_small_4"];

export const bonesLikeBlockComponent = {
  beforeOnPlayerPlace(e) {
    const { permutationToPlace, block } = e;
    if (!boneBlocks.includes(permutationToPlace.type.id)) return;
    let randomVariant;

    switch (permutationToPlace.type.id) {
      case "hb_hr:bone_pile_b2_0":
      case "hb_hr:bone_pile_b2_1":
      case "hb_hr:bone_pile_small_0":
      case "hb_hr:bone_pile_small_2":
        randomVariant = Math.floor(Math.random() * 3);
        break;
      case "hb_hr:bone_pile_b2_2":
      case "hb_hr:bone_pile_small_1":
        randomVariant = Math.floor(Math.random() * 2);
        break;
      case "hb_hr:bone_pile_small_3":
        randomVariant = 0;
        break;
      case "hb_hr:bone_pile_small_4":
        randomVariant = Math.floor(Math.random() * 6);
        break;

      default:
        randomVariant = 0;
        break;
    }

    const baseStates = {
      "hb_hr:placed_bit": true,
      "hb_hr:variant": randomVariant,
      "hb_hr:rotation": Math.floor(Math.random() * 3),
    };

    const newPerm = BlockPermutation.resolve(permutationToPlace.type.id, baseStates);

    e.permutationToPlace = newPerm;
  },
  onPlayerDestroy(e) {
    // Destructure event data for easier access
    const { dimension, block, player } = e;

    // Extract destroyed block permutation from event data
    const { destroyedBlockPermutation: perm } = e;

    // Get the hb_hr:variant state of the destroyed block
    const variantLevel = perm.getState("hb_hr:variant");
    // Determine the number of pink petals items to spawn based on the hb_hr:variant block state value
    let dropCount = 0;
    if (variantLevel === 0) {
      dropCount = 1;
    } else if (variantLevel === 1) {
      dropCount = 2;
    } else if (variantLevel === 2) {
      dropCount = 3;
    } else if (variantLevel === 3) {
      dropCount = 4;
    } else if (variantLevel === 4) {
      dropCount = 5;
    } else if (variantLevel === 5) {
      dropCount = 6;
    }

    // Get the equipment component for the player
    const equipment = player.getComponent("equippable");
    // Get the selected item from the player's mainhand
    const usedItem = equipment.getEquipment("Mainhand");
    if (!usedItem) {
      // Spawn the pink petals items at the destroyed block location if any should be spawned
      if (dropCount > 0) {
        const bonesItem = new ItemStack("minecraft:bone", dropCount);
        dimension.spawnItem(bonesItem, block.location);
      }
      return;
    }
    if (usedItem.getComponent("enchantable") != undefined) {
      const enchants = usedItem.getComponent("enchantable");
      if (enchants.getEnchantment("silk_touch") != undefined) {
        const bonesItem = new ItemStack(block.typeId, 1);
        dimension.spawnItem(bonesItem, block.location);
      }
    } else {
      if (dropCount > 0) { 
        const bonesItem = new ItemStack("minecraft:bone", dropCount);
        dimension.spawnItem(bonesItem, block.location);
      }
    }
  },
};

const customComponents = {
  "hb_hr:bone_variants": bonesLikeBlockComponent,
};

export function bonesBlock() {
  world.beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
    for (const [componentName, componentImplementation] of Object.entries(customComponents)) {
      blockComponentRegistry.registerCustomComponent(componentName, componentImplementation);
    }
  });
}
