import { world } from "@minecraft/server";
import highlightBlock from "./MultiblockHighlight.js";

const SOUND_WRONG = "note.bit";
const SOUND_WRONG_OPTIONS = { pitch: 0.3, volume: 5.0 };

const SOUND_RIGHT = "note.bell";
const SOUND_RIGHT_OPTIONS = { pitch: 1.0, volume: 5.0 };

const multiblocks = ["pb_dino:pyrolyzer", "pb_dino:pyrolyzer_tank"];

// Helper function to find the adjacent block based on direction and side
function getAdjacentBlock(block, direction, side) {
  const location = block.location;
  let adjacentLocation;

  switch (side) {
    case "left":
      switch (direction) {
        case "north":
          adjacentLocation = {
            x: location.x - 1,
            y: location.y,
            z: location.z,
          };
          break;
        case "south":
          adjacentLocation = {
            x: location.x + 1,
            y: location.y,
            z: location.z,
          };
          break;
        case "east":
          adjacentLocation = {
            x: location.x,
            y: location.y,
            z: location.z - 1,
          };
          break;
        case "west":
          adjacentLocation = {
            x: location.x,
            y: location.y,
            z: location.z + 1,
          };
          break;
      }
      break;
    case "right":
      switch (direction) {
        case "north":
          adjacentLocation = {
            x: location.x + 1,
            y: location.y,
            z: location.z,
          };
          break;
        case "south":
          adjacentLocation = {
            x: location.x - 1,
            y: location.y,
            z: location.z,
          };
          break;
        case "east":
          adjacentLocation = {
            x: location.x,
            y: location.y,
            z: location.z + 1,
          };
          break;
        case "west":
          adjacentLocation = {
            x: location.x,
            y: location.y,
            z: location.z - 1,
          };
          break;
      }
      break;
  }

  return block.dimension.getBlock(adjacentLocation);
}

// Subscribe to the playerPlaceBlock event
world.afterEvents.playerPlaceBlock.subscribe((event) => {
  const block = event.block;
  const player = event.player;
  if (!multiblocks.includes(block.typeId)) return;

  // Get the facing direction of the placed block
  const facingDirection = block.permutation.getState(
    "minecraft:cardinal_direction"
  );

  // Get adjacent blocks
  const leftBlock = getAdjacentBlock(block, facingDirection, "left");
  const rightBlock = getAdjacentBlock(block, facingDirection, "right");

  // Check if there are no adjacent multiblocks
  if (
    !multiblocks.includes(leftBlock?.typeId) &&
    !multiblocks.includes(rightBlock?.typeId)
  ) {
    highlightBlock(block, "white");
    return; // Exit if no adjacent multiblocks
  }

  let feedBack = false;

  /////////////////////////////////////// PYROLYZER PLACEMENT ////////////////////////
  if (block.typeId == "pb_dino:pyrolyzer") {

    // Check the right block for tank connections
    if (rightBlock.typeId == "pb_dino:pyrolyzer_tank") {
      if (rightBlock.permutation.getState("pb_dino:mb_position") === "single") {
        if (
          rightBlock.permutation.getState("minecraft:cardinal_direction") ===
          facingDirection
        ) {
          highlightBlock(rightBlock, "green");
          highlightBlock(block, "green");
          player.playSound(SOUND_RIGHT, SOUND_RIGHT_OPTIONS);
          return;
        } else {
          highlightBlock(rightBlock, "red");
          highlightBlock(block, "red");
          player.onScreenDisplay.setActionBar(
            "The adjacent pyrolyzer tank is facing the wrong way."
          );
          player.playSound(SOUND_WRONG, SOUND_WRONG_OPTIONS);
          return;
        }
      } else {
        highlightBlock(rightBlock, "red");
        highlightBlock(block, "red");
        player.onScreenDisplay.setActionBar(
          "The adjacent pyrolyzer tank is already part of a multiblock."
        );
        player.playSound(SOUND_WRONG, SOUND_WRONG_OPTIONS);
        return;
      }
    }
    if (leftBlock.typeId == "pb_dino:pyrolyzer_tank") {
      if (leftBlock.permutation.getState("pb_dino:mb_position") === "single") {
        if (
          leftBlock.permutation.getState("minecraft:cardinal_direction") ===
          facingDirection
        ) {
          highlightBlock(leftBlock, "green");
          highlightBlock(block, "green");
          player.playSound(SOUND_RIGHT, SOUND_RIGHT_OPTIONS);
          return;
        } else {
          highlightBlock(leftBlock, "red");
          highlightBlock(block, "red");
          player.onScreenDisplay.setActionBar(
            "The adjacent pyrolyzer tank is facing the wrong way."
          );
          player.playSound(SOUND_WRONG, SOUND_WRONG_OPTIONS);
          return;
        }
      } else {
        highlightBlock(leftBlock, "red");
        highlightBlock(block, "red");
        player.onScreenDisplay.setActionBar(
          "The adjacent pyrolyzer tank is already part of a multiblock."
        );
        player.playSound(SOUND_WRONG, SOUND_WRONG_OPTIONS);
        return;
      }
    }
        // Check for adjacent pyrolyzer blocks
        if (leftBlock.typeId == "pb_dino:pyrolyzer") {
          highlightBlock(leftBlock, "red");
          highlightBlock(block, "red");
          player.onScreenDisplay.setActionBar(
            "This pyrolyzer block needs to be attached to the side of a pyrolyzer tank, not another pyrolyzer."
          );
          player.playSound(SOUND_WRONG, SOUND_WRONG_OPTIONS);
          return;
          // No return in case both sides are pyrolyzer
        }
        if (rightBlock.typeId == "pb_dino:pyrolyzer") {
          highlightBlock(rightBlock, "red");
          highlightBlock(block, "red");
          player.onScreenDisplay.setActionBar(
            "This core block needs to be attached to the side of a pyrolyzer tank, not another pyrolyzer."
          );
          player.playSound(SOUND_WRONG, SOUND_WRONG_OPTIONS);
          return;
        }
  }

  /////////////////////////////////////// TANK PLACEMENT ////////////////////////
  if (block.typeId == "pb_dino:pyrolyzer_tank") {
    // Check for adjacent pyrolyzer blocks
    if (leftBlock.typeId == "pb_dino:pyrolyzer") {
      if (leftBlock.permutation.getState("pb_dino:mb_position") === "single") {
        if (
          leftBlock.permutation.getState("minecraft:cardinal_direction") ===
          facingDirection
        ) {
          highlightBlock(leftBlock, "green");
          highlightBlock(block, "green");
          player.playSound(SOUND_RIGHT, SOUND_RIGHT_OPTIONS);
          return;
        } else {
          highlightBlock(leftBlock, "red");
          highlightBlock(block, "red");
          player.onScreenDisplay.setActionBar(
            "The adjacent pyrolyzer is facing the wrong way."
          );
          player.playSound(SOUND_WRONG, SOUND_WRONG_OPTIONS);
          return;
        }
      } else {
        highlightBlock(leftBlock, "red");
        highlightBlock(block, "red");
        player.onScreenDisplay.setActionBar(
          "The adjacent pyrolyzer is already part of another multiblock."
        );
        player.playSound(SOUND_WRONG, SOUND_WRONG_OPTIONS);
        return;
      }
    }

    if (rightBlock.typeId == "pb_dino:pyrolyzer") {
      if (rightBlock.permutation.getState("pb_dino:mb_position") === "single") {
        if (
          rightBlock.permutation.getState("minecraft:cardinal_direction") ===
          facingDirection
        ) {
          highlightBlock(rightBlock, "green");
          highlightBlock(block, "green");
          player.playSound(SOUND_RIGHT, SOUND_RIGHT_OPTIONS);
          return;
        } else {
          highlightBlock(rightBlock, "red");
          highlightBlock(block, "red");
          player.onScreenDisplay.setActionBar(
            "The adjacent pyrolyzer is facing the wrong way."
          );
          player.playSound(SOUND_WRONG, SOUND_WRONG_OPTIONS);
          return;
        }
      } else {
        highlightBlock(rightBlock, "red");
        highlightBlock(block, "red");
        player.onScreenDisplay.setActionBar(
          "The adjacent pyrolyzer is already part of another multiblock."
        );
        player.playSound(SOUND_WRONG, SOUND_WRONG_OPTIONS);
        return;
      }
    }

    // Check for adjacent module blocks
    if (leftBlock.typeId == "pb_dino:pyrolyzer_tank") {
      highlightBlock(leftBlock, "red");
      highlightBlock(block, "red");
      player.onScreenDisplay.setActionBar(
        "This pyrolyzer tank needs to be placed adjacent to a pyrolyzer."
      );
      player.playSound(SOUND_WRONG, SOUND_WRONG_OPTIONS);
      return;
    }

    if (rightBlock.typeId == "pb_dino:pyrolyzer_tank") {
      highlightBlock(rightBlock, "red");
      highlightBlock(block, "red");
      player.onScreenDisplay.setActionBar(
        "This pyrolyzer tank needs to be placed adjacent to a pyrolyzer."
      );
      player.playSound(SOUND_WRONG, SOUND_WRONG_OPTIONS);
      return;
    }
  }
});
