import { world, MolangVariableMap } from "@minecraft/server";

export function highlightBlock(block, color, shape = "box") {
  let location = block?.location;
  if (!block || !location) return;

  // Define RGB values for each color
  const colorRGB = {
    red: { r: 0.9, g: 0, b: 0 },
    orange: { r: 1, g: 0.51, b: 0 },
    green: { r: 0, g: 0.9, b: 0 },
    blue: { r: 0, g: 0, b: 0.9 },
    white: { r: 0.9, g: 0.9, b: 0.9 },
    yellow: { r: 0.9, g: 0.9, b: 0 },
    gold: { r: 1, g: 0.8, b: 0 },
    purple: { r: 0.51, g: 0, b: 0.9 },
    cyan: { r: 0, g: 0.9, b: 0.9 },
    eldritch: { r: 0.8, g: 0.51, b: 1 },
    black: { r: 0.1, g: 0.1, b: 0.1 },
    coal_ore: { r: 0.14, g: 0.14, b: 0.14 },
    copper_ore: { r: 0.95, g: 0.51, b: 0.4 },
    iron_ore: { r: 0.68, g: 0.515, b: 0.46 },
    gold_ore: { r: 0.92, g: 0.61, b: 0.05 },
    diamond_ore: { r: 0.11, g: 0.81, b: 0.83 },
    emerald_ore: { r: 0.09, g: 0.77, b: 0.26 },
    lapis_ore: { r: 0.09, g: 0.33, b: 0.74 },
    redstone_ore: { r: 0.79, g: 0.02, b: 0.02 },
    lit_redstone_ore: { r: 0.79, g: 0.02, b: 0.02 },
    quartz_ore: { r: 0.83, g: 0.79, b: 0.72 },
    nether_gold_ore: { r: 0.92, g: 0.61, b: 0.05 },
    ancient_debris: { r: 0.49, g: 0.37, b: 0.34 },
  };

  // Use the provided color or default to white if not found
  const { r, g, b } = colorRGB[color] || colorRGB.white;

  // Construct particle identifier based on shape
  const particleId = `pb_gat:block_highlight_${shape}`;

  let mvm = new MolangVariableMap();
  mvm.setFloat("variable.red", r);
  mvm.setFloat("variable.green", g);
  mvm.setFloat("variable.blue", b);

  // Y sides
  mvm.setFloat("variable.dirx", 0);
  mvm.setFloat("variable.diry", 1);
  mvm.setFloat("variable.dirz", 0);
  block.dimension.spawnParticle(
    particleId,
    { x: location.x + 0.51, y: location.y - 0.01, z: location.z + 0.51 },
    mvm
  );
  block.dimension.spawnParticle(
    particleId,
    { x: location.x + 0.51, y: location.y + 1.01, z: location.z + 0.51 },
    mvm
  );

  // X sides
  mvm.setFloat("variable.dirx", 1);
  mvm.setFloat("variable.diry", 0);
  mvm.setFloat("variable.dirz", 0);
  block.dimension.spawnParticle(
    particleId,
    { x: location.x + 1.01, y: location.y + 0.51, z: location.z + 0.51 },
    mvm
  );
  block.dimension.spawnParticle(
    particleId,
    { x: location.x - 0.01, y: location.y + 0.51, z: location.z + 0.51 },
    mvm
  );

  // Z sides
  mvm.setFloat("variable.dirx", 0);
  mvm.setFloat("variable.diry", 0);
  mvm.setFloat("variable.dirz", 1);
  block.dimension.spawnParticle(
    particleId,
    { x: location.x + 0.51, y: location.y + 0.51, z: location.z + 1.01 },
    mvm
  );
  block.dimension.spawnParticle(
    particleId,
    { x: location.x + 0.51, y: location.y + 0.51, z: location.z - 0.01 },
    mvm
  );
}

export function highlightTeleportBlock(block) {
  let location = block?.location;
  let mvm = new MolangVariableMap();

  // Set color variables
  mvm.setFloat(`variable.dirx`, 0);
  mvm.setFloat(`variable.diry`, 1);
  mvm.setFloat(`variable.dirz`, 0);
  block?.dimension.spawnParticle(
    "pb_gat:block_teleport_highlight",
    { x: location.x + 0.51, y: location.y - 0.01, z: location.z + 0.51 },
    mvm
  );
  block?.dimension.spawnParticle(
    "pb_gat:block_teleport_highlight",
    { x: location.x + 0.51, y: location.y + 1.01, z: location.z + 0.51 },
    mvm
  );

  mvm.setFloat(`variable.dirx`, 1);
  mvm.setFloat(`variable.diry`, 0);
  mvm.setFloat(`variable.dirz`, 0);
  block?.dimension.spawnParticle(
    "pb_gat:block_teleport_highlight",
    { x: location.x + 1.01, y: location.y + 0.51, z: location.z + 0.51 },
    mvm
  );
  block?.dimension.spawnParticle(
    "pb_gat:block_teleport_highlight",
    { x: location.x - 0.01, y: location.y + 0.51, z: location.z + 0.51 },
    mvm
  );

  mvm.setFloat(`variable.dirx`, 0);
  mvm.setFloat(`variable.diry`, 0);
  mvm.setFloat(`variable.dirz`, 1);
  block?.dimension.spawnParticle(
    "pb_gat:block_teleport_highlight",
    { x: location.x + 0.51, y: location.y + 0.51, z: location.z + 1.01 },
    mvm
  );
  block?.dimension.spawnParticle(
    "pb_gat:block_teleport_highlight",
    { x: location.x + 0.51, y: location.y + 0.51, z: location.z - 0.01 },
    mvm
  );
}
