import { BlockPermutation, ItemStack } from "@minecraft/server";
import {} from "./eternal/server";
import { gamerules, beforeEvents, world } from "./eternal/constants";

/** @type {import("@minecraft/server").BlockCustomComponent} */

const DoorComponent = {
  onPlayerInteract(event) {
    const { block } = event;
    const permutation = block.permutation;
    const blockOpenState = permutation.getState("ecbl_bc:open_bit");
    const blockUpperState = permutation.getState("ecbl_bc:upper_block_bit");

    let newOpenState = !blockOpenState;
    let location = block.location;
    let attachedBlock;

    if (blockUpperState == true) attachedBlock = block.below();
    else attachedBlock = block.above();

    attachedBlock.setPermutation(attachedBlock.permutation.setState("ecbl_bc:open_bit", newOpenState));
    block.setPermutation(block.permutation.setState("ecbl_bc:open_bit", newOpenState));

    //play sounds each time the door is interacted with
    if (newOpenState) world.playSound("open.wooden_door", location);
    else world.playSound("close.wooden_door", location);
  },

  //WHEN PLACING THE BLOCK
  beforeOnPlayerPlace(event) {
    const { block, player, permutationToPlace } = event;

    const equipment = player.getComponent("equippable");
    const selectedItem = equipment.getEquipment("Mainhand");
    const generateDoor = (doorHingeBit = false) => {
      const cardinal = permutationToPlace.getState("minecraft:cardinal_direction");

      event.permutationToPlace = event.permutationToPlace.withState("ecbl_bc:door_hinge_bit", doorHingeBit);
      event.permutationToPlace = event.permutationToPlace.withState("minecraft:cardinal_direction", cardinal);

      block.above().setPermutation(
        BlockPermutation.resolve(selectedItem.typeId, {
          "minecraft:cardinal_direction": cardinal,
          "ecbl_bc:upper_block_bit": true,
          "ecbl_bc:open_bit": false,
          "ecbl_bc:door_hinge_bit": doorHingeBit,
        })
      );
    };

    //double door set-up
    let leftBlock;

    switch (permutationToPlace.getState("minecraft:cardinal_direction")) {
      case "south":
        leftBlock = block.east();
        break;
      case "north":
        leftBlock = block.west();
        break;
      case "east":
        leftBlock = block.north();
        break;
      case "west":
        leftBlock = block.south();
        break;
      default:
        break;
    }
    generateDoor();

    if (
      leftBlock.permutation.getState("minecraft:cardinal_direction") ===
      event.permutationToPlace.getState("minecraft:cardinal_direction")
    ) {
      if (
        leftBlock.permutation.getState("ecbl_bc:upper_block_bit") ===
        event.permutationToPlace.getState("ecbl_bc:upper_block_bit")
      ) {
        generateDoor(true);
      }
    }
  },

  onTick(event) {
    const { block } = event;
    const blockUpperState = block.permutation.getState("ecbl_bc:upper_block_bit");
    let blockId = block.typeId !== "minecraft:air" ? block.typeId : undefined;

    //check if each door destroyed
    if (block.below().typeId == "minecraft:air" && blockUpperState) block.setType("minecraft:air");
    if (block.above().typeId == "minecraft:air" && !blockUpperState) block.setType("minecraft:air");

    //check if block below the door is destroyed
    if (block.below().typeId == "minecraft:air" && !blockUpperState) {
      block.setType("minecraft:air");
      if (gamerules.doTileDrops) block.dimension.spawnItem(new ItemStack(blockId), block.location);
    }
  },
};

beforeEvents.worldInitialize.subscribe(({ blockComponentRegistry }) => {
  blockComponentRegistry.registerCustomComponent("ecbl_bc:door", DoorComponent);
});
