import { world } from "@minecraft/server";
import highlightBlock from "./multiblockOutlineBlock.js";
import { getAdjacentBlock, multiblocks, cores, modules } from "./multiblockUtils.js";

// Constants for messages
const MESSAGES = {
  invalidCoreDirection: "§b! [§7adjacent §bCore§7 is facing the wrong way§b] !",
  coreAlreadyPartOfMultiblock: "§b! [§7adjacent §bCore§7 is part of another multiblock§b] !",
  invalidModulePlacement: "§b! [Module§7 needs to be adjacent to §bcore§7 blocks§b] !",
  invalidModuleDirection: "§b! [§7adjacent §bmodule§7 is facing the wrong way§b] !",
  blockNeedsAttachmentToModule: "§b! [core§7 needs to be attached to a §bmodule§7, not another §bcore] !",
  blockAlreadyAttachedToModule: "§b! [§7adjacent §bmodule§r is already part of a multiblock§b] !"
};

// Helper function to check adjacent blocks
function checkAdjacentBlock(block, adjacentBlock, facingDirection, player) {
  if (!adjacentBlock) return false;

  if (cores.includes(adjacentBlock.typeId)) {
    if (adjacentBlock.permutation.getState("pb_plane:mb_connection") === "single") {
      if (adjacentBlock.permutation.getState("minecraft:cardinal_direction") === facingDirection) {
        highlightBlock(adjacentBlock, "green");
        highlightBlock(block, "green");
        return true;
      } else {
        highlightBlock(adjacentBlock, "red");
        highlightBlock(block, "red");
        player.onScreenDisplay.setActionBar(MESSAGES.invalidCoreDirection);
        return false;
      }
    } else {
      highlightBlock(adjacentBlock, "red");
      highlightBlock(block, "red");
      player.onScreenDisplay.setActionBar(MESSAGES.coreAlreadyPartOfMultiblock);
      return false;
    }
  }

  if (modules.includes(adjacentBlock.typeId)) {
    highlightBlock(adjacentBlock, "red");
    highlightBlock(block, "red");
    player.onScreenDisplay.setActionBar(MESSAGES.invalidModulePlacement);
    return false;
  }

  return true;
}

// Function to handle placement logic for cores and modules
function handlePlacement(block, adjacentBlock, facingDirection, player) {
  const valid = checkAdjacentBlock(block, adjacentBlock, facingDirection, player);
  if (!valid) return;  // Exit if invalid placement
}

// 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
  }

  /////////////////////////////////////// CORE PLACEMENT ////////////////////////
  if (cores.includes(block.typeId)) {
    // Check for adjacent core blocks
    if (cores.includes(leftBlock?.typeId)) {
      highlightBlock(leftBlock, "red");
      highlightBlock(block, "red");
      player.onScreenDisplay.setActionBar(MESSAGES.blockNeedsAttachmentToModule);
      // No return in case both sides are cores
    }

    if (cores.includes(rightBlock?.typeId)) {
      highlightBlock(rightBlock, "red");
      highlightBlock(block, "red");
      player.onScreenDisplay.setActionBar(MESSAGES.blockNeedsAttachmentToModule);
      return;
    }

    // Check the right block for module connections
    if (modules.includes(rightBlock?.typeId)) {
      if (rightBlock.permutation.getState("pb_plane:mb_connection") === "single") {
        if (rightBlock.permutation.getState("minecraft:cardinal_direction") === facingDirection) {
          highlightBlock(rightBlock, "green");
          highlightBlock(block, "green");
        } else {
          highlightBlock(rightBlock, "red");
          highlightBlock(block, "red");
          player.onScreenDisplay.setActionBar(MESSAGES.invalidModuleDirection);
        }
      } else {
        highlightBlock(rightBlock, "red");
        highlightBlock(block, "red");
        player.onScreenDisplay.setActionBar(MESSAGES.blockAlreadyAttachedToModule);
      }
    }

    // Check the left block for module connections
    if (modules.includes(leftBlock?.typeId)) {
      if (leftBlock.permutation.getState("pb_plane:mb_connection") === "single") {
        if (leftBlock.permutation.getState("minecraft:cardinal_direction") === facingDirection) {
          highlightBlock(leftBlock, "green");
          highlightBlock(block, "green");
        } else {
          highlightBlock(leftBlock, "red");
          highlightBlock(block, "red");
          player.onScreenDisplay.setActionBar(MESSAGES.invalidModuleDirection);
        }
      } else {
        highlightBlock(leftBlock, "red");
        highlightBlock(block, "red");
        player.onScreenDisplay.setActionBar(MESSAGES.blockAlreadyAttachedToModule);
      }
    }
  }

  /////////////////////////////////////// MODULE PLACEMENT ////////////////////////
  if (modules.includes(block.typeId)) {
    // Check for adjacent core blocks
    handlePlacement(block, leftBlock, facingDirection, player);
    handlePlacement(block, rightBlock, facingDirection, player);

    // Check for adjacent module blocks
    if (modules.includes(leftBlock?.typeId)) {
      highlightBlock(leftBlock, "red");
      highlightBlock(block, "red");
      player.onScreenDisplay.setActionBar(MESSAGES.invalidModulePlacement);
      return;
    }

    if (modules.includes(rightBlock?.typeId)) {
      highlightBlock(rightBlock, "red");
      highlightBlock(block, "red");
      player.onScreenDisplay.setActionBar(MESSAGES.invalidModulePlacement);
      return;
    }
  }
});
