import { system } from "@minecraft/server";
import { ItemInventoryContents } from "../utilities.js";

export function barrierBuilder(player, item) {
  system.run(() => {
    const origin = player.location;
    const dimension = player.dimension;

    player.playSound("conduit.activate", {
      pitch: 2.5,
      volume: 3,
      location: player.location,
    });

    const centerX = Math.floor(origin.x);
    const centerY = Math.floor(origin.y);
    const centerZ = Math.floor(origin.z);

    let radius = 4;
    let speed = 2;
    let blockType = "pb_gat:bridge_block";
    let spike = false;

    let hasLife = false;
    let hasEmber = false;
    let hasReach = false;

    if (item) {
      const contents =
        new ItemInventoryContents(item).getContainer()?.content ?? [];

      hasReach = contents.some((stack) => stack?.typeId === "pb_gat:orb_reach");
      const hasSpeed = contents.some(
        (stack) => stack?.typeId === "pb_gat:orb_speed"
      );
      spike = contents.some((stack) => stack?.typeId === "pb_gat:orb_spike");
      hasLife = contents.some((stack) => stack?.typeId === "pb_gat:orb_life");
      hasEmber = contents.some((stack) => stack?.typeId === "pb_gat:orb_ember");

      if (hasReach) radius = 6;
      if (hasSpeed) speed = 1;

      if (hasLife && hasEmber) {
        blockType = "pb_gat:ember_bridge_block_life";
      } else if (hasEmber) {
        blockType = "pb_gat:ember_bridge_block";
      } else if (hasLife) {
        blockType = "pb_gat:bridge_block_life";
      }
    }

    // === SPIKE DUMMY LOGIC ===
    if (spike) {
      const dummy = dimension.spawnEntity("pb_gat:spike_dummy", origin);
      if (dummy) {
        // Tame the dummy to the player
        const tameComponent = dummy.getComponent("minecraft:tameable");
        if (tameComponent) {
          tameComponent.tame(player);
        }
        // SPAWN behavior
        if (hasLife) {
          dummy.triggerEvent("pb_gat:spawn_life");
        } else {
          dummy.triggerEvent("pb_gat:spawn_default");
        }

        // ATTACK behavior
        if (hasReach) {
          dummy.triggerEvent("pb_gat:attack_reach");
        } else {
          dummy.triggerEvent("pb_gat:attack_default");
        }
      }
    }

    // === DOME BUILDING LOGIC ===
    for (let y = radius; y >= -radius; y--) {
      const delay = (radius - y) * speed * 5;

      system.runTimeout(() => {
        for (let x = -radius; x <= radius; x++) {
          for (let z = -radius; z <= radius; z++) {
            const distance = Math.sqrt(x * x + y * y + z * z);

            if (distance >= radius - 0.5 && distance <= radius + 0.5) {
              const bx = centerX + x;
              const by = centerY + y;
              const bz = centerZ + z;

              const block = dimension.getBlock({ x: bx, y: by, z: bz });
              if (
                block &&
                (block.isAir ||
                  block.typeId === "pb_gat:bridge_block" ||
                  block.typeId === "pb_gat:bridge_block_life" ||
                  block.typeId === "pb_gat:ember_bridge_block" ||
                  block.typeId === "pb_gat:ember_bridge_block_life")
              ) {
                player.playSound("place.tuff", {
                  pitch: 1.3,
                  volume: 0.2,
                  location: block.location,
                });
                block.setType(blockType);
              }
            }
          }
        }
      }, delay);
    }
  });
}
