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

export class stackfenceManager {

    static beforeplace(data) {
        const block = data.block;

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


        const northConnects = (north.hasTag("sgs_farm_fence"));
        const eastConnects = (east.hasTag("sgs_farm_fence"));
        const southConnects = (south.hasTag("sgs_farm_fence"));
        const westConnects = (west.hasTag("sgs_farm_fence"));

        mc.system.runTimeout(() => {
            try {
                if (northConnects && !north.hasTag("sgs_farm_gate")) north.setPermutation(north.permutation.withState('sgs_farm:fence_south', 1));
                if (eastConnects && !east.hasTag("sgs_farm_gate")) east.setPermutation(east.permutation.withState('sgs_farm:fence_west', 1));
                if (southConnects && !south.hasTag("sgs_farm_gate")) south.setPermutation(south.permutation.withState('sgs_farm:fence_north', 1));
                if (westConnects && !west.hasTag("sgs_farm_gate")) west.setPermutation(west.permutation.withState('sgs_farm:fence_east', 1));
            } catch (error) { }
        }, 0)

        data.permutationToPlace = mc.BlockPermutation.resolve(data.permutationToPlace.type.id, {
            'sgs_farm:fence_upper': 0,
            'sgs_farm:fence_north': northConnects ? 1 : 0,
            'sgs_farm:fence_south': southConnects ? 1 : 0,
            'sgs_farm:fence_east': eastConnects ? 1 : 0,
            'sgs_farm:fence_west': westConnects ? 1 : 0
        });

        const above = block.above();

        mc.system.runTimeout(() => {
            if (above.isAir) {
                const north = above.north();
                const east = above.east();
                const south = above.south();
                const west = above.west();


                const northConnects = (north.hasTag("sgs_farm_fence"));
                const eastConnects = (east.hasTag("sgs_farm_fence"));
                const southConnects = (south.hasTag("sgs_farm_fence"));
                const westConnects = (west.hasTag("sgs_farm_fence"));


                try {
                    if (northConnects && !north.hasTag("sgs_farm_gate")) north.setPermutation(north.permutation.withState('sgs_farm:fence_south', 1));
                    if (eastConnects && !east.hasTag("sgs_farm_gate")) east.setPermutation(east.permutation.withState('sgs_farm:fence_west', 1));
                    if (southConnects && !south.hasTag("sgs_farm_gate")) south.setPermutation(south.permutation.withState('sgs_farm:fence_north', 1));
                    if (westConnects && !west.hasTag("sgs_farm_gate")) west.setPermutation(west.permutation.withState('sgs_farm:fence_east', 1));
                } catch (error) { }


                above.setPermutation(mc.BlockPermutation.resolve(data.permutationToPlace.type.id, {
                    'sgs_farm:fence_upper': 1,
                    'sgs_farm:fence_north': northConnects ? 1 : 0,
                    'sgs_farm:fence_south': southConnects ? 1 : 0,
                    'sgs_farm:fence_east': eastConnects ? 1 : 0,
                    'sgs_farm:fence_west': westConnects ? 1 : 0
                }));
            }
        }, 0)
    }

    static destroy(data) {

        const northConnects = data.destroyedBlockPermutation.getState("sgs_farm:fence_north");
        const eastConnects = data.destroyedBlockPermutation.getState("sgs_farm:fence_east");
        const southConnects = data.destroyedBlockPermutation.getState("sgs_farm:fence_south");
        const westConnects = data.destroyedBlockPermutation.getState("sgs_farm:fence_west");

        try {
            const block = data.block;
            const north = block.north();
            const east = block.east();
            const south = block.south();
            const west = block.west();

            if (northConnects && north.hasTag("sgs_farm_fence") && !north.hasTag("sgs_farm_gate")) north.setPermutation(north.permutation.withState('sgs_farm:fence_south', 0));
            if (eastConnects && east.hasTag("sgs_farm_fence") && !east.hasTag("sgs_farm_gate")) east.setPermutation(east.permutation.withState('sgs_farm:fence_west', 0));
            if (southConnects && south.hasTag("sgs_farm_fence") && !south.hasTag("sgs_farm_gate")) south.setPermutation(south.permutation.withState('sgs_farm:fence_north', 0));
            if (westConnects && west.hasTag("sgs_farm_fence") && !west.hasTag("sgs_farm_gate")) west.setPermutation(west.permutation.withState('sgs_farm:fence_east', 0));

        } catch (error) { };

        try {
            const block = data.block.above();
            const north = block.north();
            const east = block.east();
            const south = block.south();
            const west = block.west();

            if (northConnects && north.hasTag("sgs_farm_fence") && !north.hasTag("sgs_farm_gate")) north.setPermutation(north.permutation.withState('sgs_farm:fence_south', 0));
            if (eastConnects && east.hasTag("sgs_farm_fence") && !east.hasTag("sgs_farm_gate")) east.setPermutation(east.permutation.withState('sgs_farm:fence_west', 0));
            if (southConnects && south.hasTag("sgs_farm_fence") && !south.hasTag("sgs_farm_gate")) south.setPermutation(south.permutation.withState('sgs_farm:fence_north', 0));
            if (westConnects && west.hasTag("sgs_farm_fence") && !west.hasTag("sgs_farm_gate")) west.setPermutation(west.permutation.withState('sgs_farm:fence_east', 0));

        } catch (error) { };
    }

    static beforeplaceGate(data) {
        const player = data.player;
        const block = data.block;
        let cardinal = data.permutationToPlace.getState('minecraft:cardinal_direction');
        let flipped = 0;

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

        if (!player.isSneaking) {
            if (data.permutationToPlace.type.id == "sgs_farm:fun_pen_gate_iron" || data.permutationToPlace.type.id == "sgs_farm:fun_pen_gate_wood" ) {  //more work for later
                if (cardinal == 'north' && east.hasTag('sgs_farm_stackfence') && east.permutation.getState('minecraft:cardinal_direction')) {
                    cardinal = 'south';
                    flipped = 1;
                }
                if (cardinal == 'south' && west.hasTag('sgs_farm_stackfence') && west.permutation.getState('minecraft:cardinal_direction')) {
                    cardinal = 'north';
                    flipped = 1;
                }
                if (cardinal == 'east' && south.hasTag('sgs_farm_stackfence') && south.permutation.getState('minecraft:cardinal_direction')) {
                    cardinal = 'west';
                    flipped = 1;
                }
                if (cardinal == 'west' && north.hasTag('sgs_farm_stackfence') && north.permutation.getState('minecraft:cardinal_direction')) {
                    cardinal = 'east';
                    flipped = 1;
                }
            }
        }
        else {
            flipped = 1;
        }

        const northConnects = (north.hasTag("sgs_farm_fence"));
        const eastConnects = (east.hasTag("sgs_farm_fence"));
        const southConnects = (south.hasTag("sgs_farm_fence"));
        const westConnects = (west.hasTag("sgs_farm_fence"));

        mc.system.runTimeout(() => {
            if (northConnects && !north.hasTag("sgs_farm_gate")) north.setPermutation(north.permutation.withState('sgs_farm:fence_south', 1));
            if (eastConnects && !east.hasTag("sgs_farm_gate")) east.setPermutation(east.permutation.withState('sgs_farm:fence_west', 1));
            if (southConnects && !south.hasTag("sgs_farm_gate")) south.setPermutation(south.permutation.withState('sgs_farm:fence_north', 1));
            if (westConnects && !west.hasTag("sgs_farm_gate")) west.setPermutation(west.permutation.withState('sgs_farm:fence_east', 1));
        }, 0)

        data.permutationToPlace = mc.BlockPermutation.resolve(data.permutationToPlace.type.id, {
            'minecraft:cardinal_direction': cardinal,
            'sgs_farm:fence_upper': 0,
            'sgs_farm:flipped': flipped
        });

        const above = block.above();

        mc.system.runTimeout(() => {
            if (above.isAir) {
                const north = above.north();
                const east = above.east();
                const south = above.south();
                const west = above.west();

                const northConnects = (north.hasTag("sgs_farm_fence"));
                const eastConnects = (east.hasTag("sgs_farm_fence"));
                const southConnects = (south.hasTag("sgs_farm_fence"));
                const westConnects = (west.hasTag("sgs_farm_fence"));


                if (northConnects && !north.hasTag("sgs_farm_gate")) north.setPermutation(north.permutation.withState('sgs_farm:fence_south', 1));
                if (eastConnects && !east.hasTag("sgs_farm_gate")) east.setPermutation(east.permutation.withState('sgs_farm:fence_west', 1));
                if (southConnects && !south.hasTag("sgs_farm_gate")) south.setPermutation(south.permutation.withState('sgs_farm:fence_north', 1));
                if (westConnects && !west.hasTag("sgs_farm_gate")) west.setPermutation(west.permutation.withState('sgs_farm:fence_east', 1));

                above.setPermutation(mc.BlockPermutation.resolve(data.permutationToPlace.type.id, {
                    'minecraft:cardinal_direction': cardinal,
                    'sgs_farm:fence_upper': 1,
                    'sgs_farm:flipped': flipped
                }));

            }
        }, 0)
    }

    static interactGate(block, player) {
        const currentState = block.permutation.getState('sgs_farm:open');
        const newOpenState = !currentState;
        const sound = newOpenState ? 'block.barrel.close' : 'block.barrel.close';
        const is_upper = block.permutation.getState('sgs_farm:fence_upper');

        const newPermutation = mc.BlockPermutation.resolve(block.typeId, {
            ...block.permutation.getAllStates(),
            'sgs_farm:open': newOpenState
        });

        block.setPermutation(newPermutation);
        block.dimension.playSound(sound, block.location);

        let connected_block = block.above();
        if (is_upper == 1) connected_block = block.below();

        if (connected_block.hasTag("sgs_farm_stackgate")) {
            connected_block.setPermutation(mc.BlockPermutation.resolve(connected_block.typeId, {
                ...connected_block.permutation.getAllStates(),
                'sgs_farm:open': newOpenState
            }));
        }
    }

    static destroyGate(data) {
        try {
            const block = data.block;
            const north = block.north();
            const east = block.east();
            const south = block.south();
            const west = block.west();

            if (north.hasTag("sgs_farm_fence") && !north.hasTag("sgs_farm_gate")) north.setPermutation(north.permutation.withState('sgs_farm:fence_south', 0));
            if (east.hasTag("sgs_farm_fence") && !east.hasTag("sgs_farm_gate")) east.setPermutation(east.permutation.withState('sgs_farm:fence_west', 0));
            if (south.hasTag("sgs_farm_fence") && !south.hasTag("sgs_farm_gate")) south.setPermutation(south.permutation.withState('sgs_farm:fence_north', 0));
            if (west.hasTag("sgs_farm_fence") && !west.hasTag("sgs_farm_gate")) west.setPermutation(west.permutation.withState('sgs_farm:fence_east', 0));
        } catch (error) { };

        try {
            const block = data.block.above();
            const north = block.north();
            const east = block.east();
            const south = block.south();
            const west = block.west();

            if (north.hasTag("sgs_farm_fence") && !north.hasTag("sgs_farm_gate")) north.setPermutation(north.permutation.withState('sgs_farm:fence_south', 0));
            if (east.hasTag("sgs_farm_fence") && !east.hasTag("sgs_farm_gate")) east.setPermutation(east.permutation.withState('sgs_farm:fence_west', 0));
            if (south.hasTag("sgs_farm_fence") && !south.hasTag("sgs_farm_gate")) south.setPermutation(south.permutation.withState('sgs_farm:fence_north', 0));
            if (west.hasTag("sgs_farm_fence") && !west.hasTag("sgs_farm_gate")) west.setPermutation(west.permutation.withState('sgs_farm:fence_east', 0));
        } catch (error) { };
    }
}