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

/**
 * Evento del componente plasmaBridgeComponent
 * @param {{block: Block, dimension: Dimension}} event 
 */
export const plasmaBridgeEvent = ({ block, dimension }) => {
    try {
        const powerState = block.permutation.getState('nps_ec:power');
        const face = block.permutation.getState('minecraft:cardinal_direction');

        // Actualizar el estado de energía basado en las condiciones
        if (!powerState) {
            if (checkAnyNeighborHasTag(block, 'power:on')) setBlockProperty(block, 'nps_ec:power', 1);
        } else if (powerState) {
            if (!checkAnyNeighborHasTag(block, 'power:on')) setBlockProperty(block, 'nps_ec:power', 0);
        }

        if (powerState) {
            const checkBlocks = plasmaBridgeCheckBlocks(dimension, block, face);

            if (checkBlocks.result) {
                if (checkBlocks.face == 'north') {
                    dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${block.x} ${block.y} ${checkBlocks.blockNeighbor.z} nps_ec:plasma_bridge_north_south replace minecraft:air`);
                    dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${block.x} ${block.y} ${checkBlocks.blockNeighbor.z} nps_ec:plasma_bridge_north_south replace minecraft:water`);
                }
                else if (checkBlocks.face == 'south') {
                    dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${block.x} ${block.y} ${checkBlocks.blockNeighbor.z} nps_ec:plasma_bridge_north_south replace minecraft:air`);
                    dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${block.x} ${block.y} ${checkBlocks.blockNeighbor.z} nps_ec:plasma_bridge_north_south replace minecraft:water`);
                }
                else if (checkBlocks.face == 'west') {
                    dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${checkBlocks.blockNeighbor.x} ${block.y} ${block.z} nps_ec:plasma_bridge_west_east replace minecraft:air`);
                    dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${checkBlocks.blockNeighbor.x} ${block.y} ${block.z} nps_ec:plasma_bridge_west_east replace minecraft:water`);
                }
                else if (checkBlocks.face == 'east') {
                    dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${checkBlocks.blockNeighbor.x} ${block.y} ${block.z} nps_ec:plasma_bridge_west_east replace minecraft:air`);
                    dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${checkBlocks.blockNeighbor.x} ${block.y} ${block.z} nps_ec:plasma_bridge_west_east replace minecraft:water`);
                }
            }


            if (!checkBlocks.result) {
                if (face == 'north') dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${block.x} ${block.y} ${checkBlocks.blockNeighbor.z} minecraft:air replace nps_ec:plasma_bridge_north_south`);
                else if (face == 'south') dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${block.x} ${block.y} ${checkBlocks.blockNeighbor.z} minecraft:air replace nps_ec:plasma_bridge_north_south`);
                else if (face == 'west') dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${checkBlocks.blockNeighbor.x} ${block.y} ${block.z} minecraft:air replace nps_ec:plasma_bridge_west_east`);
                else if (face == 'east') dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${checkBlocks.blockNeighbor.x} ${block.y} ${block.z} minecraft:air replace nps_ec:plasma_bridge_west_east`);
            }
        }

        if (!powerState) {
            const checkBlocks = plasmaBridgeCheckBlocks(dimension, block, face);
            if (checkBlocks.result) {
                if (checkBlocks.face == 'north' && !checkBlocks.blockNeighbor.permutation.getState('nps_ec:power')) {
                    dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${block.x} ${block.y} ${checkBlocks.blockNeighbor.z} minecraft:air replace nps_ec:plasma_bridge_north_south`)

                    if (block.south(1).typeId.includes("water")) {
                        dimension.runCommand(`setblock ${block.x} ${block.y} ${block.z + 1} minecraft:water replace`)
                    }
                } else if (checkBlocks.face == 'south' && !checkBlocks.blockNeighbor.permutation.getState('nps_ec:power')) {
                    dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${block.x} ${block.y} ${checkBlocks.blockNeighbor.z} minecraft:air replace nps_ec:plasma_bridge_north_south`)
                    if (block.north(1).typeId.includes("water")) {
                        dimension.runCommand(`setblock ${block.x} ${block.y} ${block.z - 1} minecraft:water replace`)
                    }
                } else if (checkBlocks.face == 'west' && !checkBlocks.blockNeighbor.permutation.getState('nps_ec:power')) {
                    dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${checkBlocks.blockNeighbor.x} ${block.y} ${block.z} minecraft:air replace nps_ec:plasma_bridge_west_east`)
                    if (block.east(1).typeId.includes("water")) {
                        dimension.runCommand(`setblock ${block.x + 1} ${block.y} ${block.z} minecraft:water replace`)
                    }
                } else if (checkBlocks.face == 'east' && !checkBlocks.blockNeighbor.permutation.getState('nps_ec:power')) {
                    dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${checkBlocks.blockNeighbor.x} ${block.y} ${block.z} minecraft:air replace nps_ec:plasma_bridge_west_east`)
                    if (block.west(1).typeId.includes("water")) {
                        dimension.runCommand(`setblock ${block.x - 1} ${block.y} ${block.z} minecraft:water replace`)
                    }
                }
            }
            if (!checkBlocks.result) {
                if (face == 'north') {
                    dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${block.x} ${block.y} ${checkBlocks.blockNeighbor.z} minecraft:air replace nps_ec:plasma_bridge_north_south`)
                } else if (face == 'south') {
                    dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${block.x} ${block.y} ${checkBlocks.blockNeighbor.z} minecraft:air replace nps_ec:plasma_bridge_north_south`)
                } else if (face == 'west') {
                    dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${checkBlocks.blockNeighbor.x} ${block.y} ${block.z} minecraft:air replace nps_ec:plasma_bridge_west_east`)
                } else if (face == 'east') {
                    dimension.runCommand(`fill ${block.x} ${block.y} ${block.z} ${checkBlocks.blockNeighbor.x} ${block.y} ${block.z} minecraft:air replace nps_ec:plasma_bridge_west_east`)
                }
            }
        }
    } catch { }
}

/**
 * Escanea los bloques en un rango de 64 bloque para
 * encontrar un bloque de plasma bridge reciver
 * @param {Dimension} dimension 
 * @param {Block} block 
 * @param {string} face 
 * @returns {{result: boolean, blockNeighbor: block, face: string}}
 */
const plasmaBridgeCheckBlocks = (dimension, block, face) => {
    const whiteList = ['minecraft:air', "minecraft:water", 'nps_ec:plasma_bridge_north_south', 'nps_ec:plasma_bridge_west_east']
    for (let i = 1; i < 64; i++) {
        if (face == 'north') {
            const blockNeighbor = dimension.getBlock({ x: block.x, y: block.y, z: block.z + i });
            if (whiteList.includes(blockNeighbor.typeId)) { }
            else if (blockNeighbor.typeId == 'nps_ec:plasma_bridge_receiver') {
                if (blockNeighbor.permutation.getState('minecraft:cardinal_direction') == 'south') return { result: true, blockNeighbor, face };
                return { result: false, blockNeighbor, face };
            }
            else { return { result: false, blockNeighbor } }
        } else if (face == 'south') {
            const blockNeighbor = dimension.getBlock({ x: block.x, y: block.y, z: block.z - i });
            if (whiteList.includes(blockNeighbor.typeId)) { }
            else if (blockNeighbor.typeId == 'nps_ec:plasma_bridge_receiver') {
                if (blockNeighbor.permutation.getState('minecraft:cardinal_direction') == 'north') return { result: true, blockNeighbor, face };
                return { result: false, blockNeighbor, face };
            }
            else { return { result: false, blockNeighbor } }
        } else if (face == 'west') {
            const blockNeighbor = dimension.getBlock({ x: block.x + i, y: block.y, z: block.z });
            if (whiteList.includes(blockNeighbor.typeId)) { }
            else if (blockNeighbor.typeId == 'nps_ec:plasma_bridge_receiver') {
                if (blockNeighbor.permutation.getState('minecraft:cardinal_direction') == 'east') return { result: true, blockNeighbor, face };
                return { result: false, blockNeighbor, face };
            }
            else { return { result: false, blockNeighbor } }
        } else if (face == 'east') {
            const blockNeighbor = dimension.getBlock({ x: block.x - i, y: block.y, z: block.z });
            if (whiteList.includes(blockNeighbor.typeId)) { }
            else if (blockNeighbor.typeId == 'nps_ec:plasma_bridge_receiver') {
                if (blockNeighbor.permutation.getState('minecraft:cardinal_direction') == 'west') return { result: true, blockNeighbor, face };
                return { result: false, blockNeighbor, face };
            }
            else { return { result: false, blockNeighbor } }
        }
    }


    if (face == 'north') {
        const blockNeighbor = dimension.getBlock({ x: block.x, y: block.y, z: block.z + 65 });
        return { result: false, blockNeighbor };
    } else if (face == 'south') {
        const blockNeighbor = dimension.getBlock({ x: block.x, y: block.y, z: block.z - 65 });
        return { result: false, blockNeighbor }
    } else if (face == 'west') {
        const blockNeighbor = dimension.getBlock({ x: block.x + 65, y: block.y, z: block.z });
        return { result: false, blockNeighbor }
    } else if (face == 'east') {
        const blockNeighbor = dimension.getBlock({ x: block.x - 65, y: block.y, z: block.z });
        return { result: false, blockNeighbor }
    }
}