import { Block } from "@minecraft/server";
import { excludeBlocksArray } from "../new_wood/OnTick";

// set block wall tag here
const wallTag = "hb_hr_wall:is_wall";

export class wall_Manager {
  // A Set of block typeIds that the wall should NEVER connect to
  static ignoredBlocks = new Set([
    "minecraft:chest",
    "minecraft:trapped_chest",
    "minecraft:structure_void",
    "minecraft:barrier",
    // …add more as needed
  ]);

  static mergedSet = new Set([...excludeBlocksArray, ...wall_Manager.ignoredBlocks]);

  // Updates all the connection & top_bit states for the given wall block
  static update_Wall_States(wall) {
    // gather neighbors
    let above, north, south, east, west;
    try {
      above = wall.above(1);
    } catch {}
    try {
      north = wall.north(1);
    } catch {}
    try {
      south = wall.south(1);
    } catch {}
    try {
      east = wall.east(1);
    } catch {}
    try {
      west = wall.west(1);
    } catch {}

    const neighbors = [
      { block: above, side: "up" },
      { block: north, side: "north" },
      { block: south, side: "south" },
      { block: east, side: "east" },
      { block: west, side: "west" },
    ];

    // process each neighbor
    for (const { block: neighbor, side } of neighbors) {
      let shouldConnect = false;

      if (neighbor && !neighbor.isAir && !neighbor.isLiquid) {
        const tid = neighbor.typeId;
        if (!wall_Manager.mergedSet.has(tid)) {
          shouldConnect = true;
        }
      }

      if (side !== "up") {
        wall.setPermutation(wall.permutation.withState(`hb_hr_wall:connect_${side}`, shouldConnect));
      } else {
        wall.setPermutation(wall.permutation.withState("hb_hr_wall:top_bit", shouldConnect));
      }
    }

    // handle the “slim” logic for 2-way connections
    const northState = wall.permutation.getState("hb_hr_wall:connect_north");
    const southState = wall.permutation.getState("hb_hr_wall:connect_south");
    const eastState = wall.permutation.getState("hb_hr_wall:connect_east");
    const westState = wall.permutation.getState("hb_hr_wall:connect_west");

    if ((northState && southState && !eastState && !westState) || (!northState && !southState && eastState && westState)) {
      wall.setPermutation(wall.permutation.withState("hb_hr_wall:slim", true));

      const slimBit = northState && southState ? "north_south" : "east_west";

      wall.setPermutation(wall.permutation.withState("hb_hr_wall:slim_bit", slimBit));
    } else {
      wall.setPermutation(wall.permutation.withState("hb_hr_wall:slim", false));
    }
  }

  // Whenever a block changes, call this to update any walls around it
  static updateWallsAround(block) {
    let above, below, north, south, east, west;
    try {
      above = block.above(1);
    } catch {}
    try {
      below = block.below(1);
    } catch {}
    try {
      north = block.north(1);
    } catch {}
    try {
      south = block.south(1);
    } catch {}
    try {
      east = block.east(1);
    } catch {}
    try {
      west = block.west(1);
    } catch {}

    for (const b of [block, above, below, north, south, east, west]) {
      if (b && b.hasTag(wallTag)) {
        this.update_Wall_States(b);
      }
    }
  }
}
