import * as mc from "@minecraft/server";
import * as lib from "./lib";

export function connectToPipes(block, permutation) {
    const north = block.north();
    const east = block.east();
    const south = block.south();
    const west = block.west();

    let northConnects = (north.hasTag("pb_scraft_pipe"));
    let eastConnects = (east.hasTag("pb_scraft_pipe"));
    let southConnects = (south.hasTag("pb_scraft_pipe"));
    let westConnects = (west.hasTag("pb_scraft_pipe"));

    mc.system.runTimeout(() => {
        if (northConnects) north.setPermutation(north.permutation.withState('pb_scraft:pipe_south', 1));
        if (eastConnects) east.setPermutation(east.permutation.withState('pb_scraft:pipe_west', 1));
        if (southConnects) south.setPermutation(south.permutation.withState('pb_scraft:pipe_north', 1));
        if (westConnects) west.setPermutation(west.permutation.withState('pb_scraft:pipe_east', 1));
    }, 0)
    
    return mc.BlockPermutation.resolve(permutation.type.id, {
        'pb_scraft:pipe_north': northConnects ? 1 : 0,
        'pb_scraft:pipe_south': southConnects ? 1 : 0,
        'pb_scraft:pipe_east': eastConnects ? 1 : 0,
        'pb_scraft:pipe_west': westConnects ? 1 : 0
    });
}

export function disconnectFromPipes(block, permutation) {
    const northConnects = permutation.getState("pb_scraft:pipe_north");
    const eastConnects = permutation.getState("pb_scraft:pipe_east");
    const southConnects = permutation.getState("pb_scraft:pipe_south");
    const westConnects = permutation.getState("pb_scraft:pipe_west");

    const north = block.north();
    const east = block.east();
    const south = block.south();
    const west = block.west();

    if (northConnects) north.setPermutation(north.permutation.withState('pb_scraft:pipe_south', 0));
    if (eastConnects) east.setPermutation(east.permutation.withState('pb_scraft:pipe_west', 0));
    if (southConnects) south.setPermutation(south.permutation.withState('pb_scraft:pipe_north', 0));
    if (westConnects) west.setPermutation(west.permutation.withState('pb_scraft:pipe_east', 0));
}

export function findTanks(block, blockmap, network_range) {
    //if range is minimum no point in checking further
    if (network_range <= 1) return;

    blockmap.set(lib.locString(block.location), block);


    if (block.hasTag("pb_scraft_mana_tank")) {

    };

    if (block.hasTag("pb_scraft_pipe")) {
        const permutation = block.permutation;
        const north = block.north();
        const east = block.east();
        const south = block.south();
        const west = block.west();
        const top = block.above();
        const bottom = block.below();

        const northConnects = permutation.getState("pb_scraft:pipe_north");
        const eastConnects = permutation.getState("pb_scraft:pipe_east");
        const southConnects = permutation.getState("pb_scraft:pipe_south");
        const westConnects = permutation.getState("pb_scraft:pipe_west");
        const topConnects = permutation.getState("pb_scraft:pipe_top");
        const bottomConnects = permutation.getState("pb_scraft:pipe_bottom");

        try {
            if (northConnects && !blockmap.has(lib.locString(north.location))) {
                findTanks(north, blockmap, network_range - 1);
            }
            if (eastConnects && !blockmap.has(lib.locString(east.location))) {
                findTanks(east, blockmap, network_range - 1);
            }
            if (southConnects && !blockmap.has(lib.locString(south.location))) {
                findTanks(south, blockmap, network_range - 1);
            }
            if (westConnects && !blockmap.has(lib.locString(west.location))) {
                findTanks(west, blockmap, network_range - 1);
            }
            if (topConnects && !blockmap.has(lib.locString(top.location))) {
                findTanks(top, blockmap, network_range - 1);
            }
            if (bottomConnects && !blockmap.has(lib.locString(bottom.location))) {
                findTanks(bottom, blockmap, network_range - 1);
            }
        } catch (error) {
            return;
        }

    }
}