import { world, EquipmentSlot } from "@minecraft/server";
import { checkBlockAboveFor2BlockHighBlock } from "../utilities/helpers";

const fakeSkullComponent = {
  onTick({ block }) {
    // If there is no block below, or the block below isn’t a real banned skull, remove this fake block
    const below = block.below();
    if (!below || !below.typeId.includes("hb_hr:banned_wither_skull")) {
      block.setType("minecraft:air");
    }
  },
};

const SkullComponent = {
  beforeOnPlayerPlace(e) {
    const { block, player } = e;
    // Banned Wither Skull Particles
    const center = block.center();
    switch (e.permutationToPlace.type.id) {
      case "hb_hr:banned_wither_skull_b1_block":
        try {
          block.dimension.spawnParticle("hb_hr:skull_b1", { x: center.x, y: center.y - 0.5, z: center.z });
        } catch (e) {}
        break;
      case "hb_hr:banned_wither_skull_b2_block":
        try {
          block.dimension.spawnParticle("hb_hr:skull_b2", { x: center.x, y: center.y - 0.5, z: center.z });
        } catch (e) {}
        break;
      case "hb_hr:banned_wither_skull_b3_block":
        try {
          block.dimension.spawnParticle("hb_hr:skull_b3", { x: center.x, y: center.y - 0.5, z: center.z });
        } catch (e) {}
        break;
      default:
        break;
    }

    if (checkBlockAboveFor2BlockHighBlock(block, player, block.location)) {
      e.cancel = true;
      return;
    }
    block.above().setType("hb_hr:fake_banned_wither_skull_block");

    
  },
  onTick({ block }) {
    // Banned Wither Skull Particles
    const center = block.center();
    switch (block.typeId) {
      case "hb_hr:banned_wither_skull_b1_block":
        try {
          block.dimension.spawnParticle("hb_hr:skull_b1", { x: center.x, y: center.y - 0.5, z: center.z });
        } catch (e) {}
        break;
      case "hb_hr:banned_wither_skull_b2_block":
        try {
          block.dimension.spawnParticle("hb_hr:skull_b2", { x: center.x, y: center.y - 0.5, z: center.z });
        } catch (e) {}
        break;
      case "hb_hr:banned_wither_skull_b3_block":
        try {
          block.dimension.spawnParticle("hb_hr:skull_b3", { x: center.x, y: center.y - 0.5, z: center.z });
        } catch (e) {}
        break;
      default:
        break;
    }
    // If there is no block below, or the block below isn’t a real banned skull, remove this fake block
    const above = block.above();
    if (!above || !above.typeId.includes("hb_hr:fake_banned_wither_skull_block")) {
      above.setType("hb_hr:fake_banned_wither_skull_block");
    }
  },
};

const customComponents = {
  "hb_hr:fake_banned_wither_skull_block": fakeSkullComponent,
  "hb_hr:banned_wither_skull_block": SkullComponent,
};

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

  // When a real banned skull block is broken, also clear any fake skull directly above it
  world.afterEvents.playerBreakBlock.subscribe((event) => {
    const broken = event.brokenBlockPermutation;
    if (broken.type.id.includes("hb_hr:banned_wither_skull")) {
      const above = event.block.above();
      if (above) above.setType("minecraft:air");
    }
  });

  // Handle itemUse to place our fake blocked skull block
  // world.afterEvents.itemUseOn.subscribe((event) => {
  //   const player = event.source;
  //   // Only players can place blocks
  //   if (player.typeId !== "minecraft:player") return;

  //   // Check the item in the player’s main hand
  //   const hand = player.getComponent("minecraft:equippable").getEquipment(EquipmentSlot.Mainhand);
  //   if (!hand || !hand.typeId.includes("hb_hr:banned_wither_skull")) {
  //     return;
  //   }
  //   const gameMode = player.getGameMode();

  //   // Raycast to find the target block and face the player clicked on
  //   const raycastBlock = player.getBlockFromViewDirection({
  //     includeLiquidBlocks: false,
  //     includePassableBlocks: false,
  //     maxDistance: 8,
  //   });
  //   if (raycastBlock == undefined) {
  //     return;
  //   } else {
  //     const location = raycastBlock.block.location;
  //     const face = raycastBlock.face;
  //     switch (face) {
  //       case "Up":
  //         if (checkBlockAboveFor2BlockHighBlock(location, player, { x: location.x + 0.5, y: location.y + 1, z: location.z + 0.5 })) {
  //           return;
  //         }
  //         raycastBlock.block.above().setType("hb_hr:fake_banned_wither_skull_block");
  //         break;
  //       case "Down":
  //         if (checkBlockAboveFor2BlockHighBlock(location, player, { x: location.x + 0.5, y: location.y - 1, z: location.z + 0.5 })) {
  //           return;
  //         }
  //         raycastBlock.block.above().setType("hb_hr:fake_banned_wither_skull_block");
  //         break;
  //       case "North":
  //         if (checkBlockAboveFor2BlockHighBlock(location, player, { x: location.x + 0.5, y: location.y, z: location.z - 0.5 })) {
  //           return;
  //         }
  //         raycastBlock.block.above().setType("hb_hr:fake_banned_wither_skull_block");
  //         break;
  //       case "South":
  //         if (checkBlockAboveFor2BlockHighBlock(location, player, { x: location.x + 0.5, y: location.y, z: location.z + 1.5 })) {
  //           return;
  //         }
  //         raycastBlock.block.above().setType("hb_hr:fake_banned_wither_skull_block");
  //         break;
  //       case "West":
  //         if (checkBlockAboveFor2BlockHighBlock(location, player, { x: location.x - 0.5, y: location.y, z: location.z + 0.5 })) {
  //           return;
  //         }
  //         raycastBlock.block.above().setType("hb_hr:fake_banned_wither_skull_block");
  //         break;
  //       case "East":
  //         if (checkBlockAboveFor2BlockHighBlock(location, player, { x: location.x + 1.5, y: location.y, z: location.z + 0.5 })) {
  //           return;
  //         }
  //         raycastBlock.block.above().setType("hb_hr:fake_banned_wither_skull_block");
  //         break;
  //       default:
  //         // If it’s some other face (e.g. “WestAndUp”), ignore
  //         return;
  //     }
  //   }

  //   // If in survival or adventure, consume one banned skull item from the player’s hand
  //   if (gameMode === "survival" || gameMode === "adventure") {
  //     player.runCommand(`/clear @s ${hand.typeId} 0 1`);
  //   }
  // });
}
