import { Block, Dimension, system } from "@minecraft/server";
import { blockNeighbors, setBlockProperty } from "../../../library/blockManager";

/**
 * Evento onRandomTick del componente farmerComponent
 * Addon: Electrical Creativity BP
 * @param {{block: Block, dimension: Dimension}} event
 */
export const farmerOnTickEvent = ({ block, dimension }) => {
    try {
        if (!block.permutation.getState("nps_ec:power")) return;
        const { blockNeighborEast, blockNeighborWest } = blockNeighbors(block);
        const blockPosition = { x: block.x, y: block.y, z: block.z }
        const size = 7; // Tamaño del cuadrado (por ejemplo, 7x7)

        const blocks = [];
        let growths = 0;
        let growthFulls = 0;

        // Crear el plano basado en blockPosition
        for (let z = -Math.floor(size / 2); z <= Math.floor(size / 2); z++) {
            const row = [];
            for (let x = -Math.floor(size / 2); x <= Math.floor(size / 2); x++) {
                row.push({
                    x: blockPosition.x + x,
                    y: blockPosition.y,
                    z: blockPosition.z + z
                });
            }
            blocks.push(row);
        }

        for (let i = 0; i < blocks.length; i++) {
            for (let j = 0; j < blocks[i].length; j++) {
                const growth = dimension.getBlock(blocks[i][j]);
                if (!(growth.x == block.x && growth.z == block.z)) {
                    if (growth.permutation.getState("growth") != undefined) {
                        growths++;
                        if (growth.permutation.getState("growth") == 7) {
                            growthFulls++;
                        }
                    }
                }
            }
        }
        if (growths && block.isWaterlogged) {
            if (growths == growthFulls) {
                setBlockProperty(block, 'nps_ec:open', true);

                if (blockNeighborEast.typeId == "minecraft:air" || blockNeighborEast.permutation.getState("growth") != undefined) {
                    dimension.runCommandAsync(`setblock ${block.x + 1} ${block.y} ${block.z} flowing_water destroy`);
                }
                if (blockNeighborWest.typeId == "minecraft:air" || blockNeighborWest.permutation.getState("growth") != undefined) {
                    dimension.runCommandAsync(`setblock ${block.x - 1} ${block.y} ${block.z} flowing_water destroy`);
                }
            } else {
                setBlockProperty(block, 'nps_ec:open', false);
                dimension.runCommandAsync(`fill ${block.x + 1} ${block.y} ${block.z} ${block.x + 1} ${block.y} ${block.z} air replace water`);
                dimension.runCommandAsync(`fill ${block.x - 1} ${block.y} ${block.z} ${block.x - 1} ${block.y} ${block.z} air replace water`);
            }
        } else {
            setBlockProperty(block, 'nps_ec:open', false);
            dimension.runCommandAsync(`fill ${block.x + 1} ${block.y} ${block.z} ${block.x + 1} ${block.y} ${block.z} air replace water`);
            dimension.runCommandAsync(`fill ${block.x - 1} ${block.y} ${block.z} ${block.x - 1} ${block.y} ${block.z} air replace water`);
        }
    } catch { }
}