import * as mc from "@minecraft/server";
import { highlightBlock } from "../sgs_cake_lib";

export class connectCardinalManager {

    static beforeplace(data, parameters) {
        const block = data.block;
        const direction = data.permutationToPlace.getState("minecraft:cardinal_direction");

        const connect_tag = parameters.params.connect_tag;
        let right;
        let left;

        switch (direction) {
            case "north":
                right = block.east();
                left = block.west();
                break;
            case "east":
                right = block.south();
                left = block.north();
                break;
            case "west":
                right = block.north();
                left = block.south();
                break;
            case "south":
                right = block.west();
                left = block.east();
                break;
            default:
                break;
        }



        const leftConnects = (left.hasTag(connect_tag) && left.permutation.getState("minecraft:cardinal_direction") == direction);
        const rightConnects = (right.hasTag(connect_tag) && right.permutation.getState("minecraft:cardinal_direction") == direction);

        mc.system.runTimeout(() => {
            if (leftConnects) left.setPermutation(left.permutation.withState('sgs_cake:right', 1));
            if (rightConnects) right.setPermutation(right.permutation.withState('sgs_cake:left', 1));
        }, 0)
        data.permutationToPlace = mc.BlockPermutation.resolve(data.permutationToPlace.type.id,
            {
                'minecraft:cardinal_direction': direction,
                "sgs_cake:right": rightConnects ? 1 : 0,
                "sgs_cake:left": leftConnects ? 1 : 0
            });
    }

    //clean up connections made in case this block breaks
    static destroy(data, parameters) {
        const block = data.block;
        const direction = data.brokenBlockPermutation.getState("minecraft:cardinal_direction");

        const connect_tag = parameters.params.connect_tag;
        let right;
        let left;

        switch (direction) {
            case "north":
                right = block.east();
                left = block.west();
                break;
            case "east":
                right = block.south();
                left = block.north();
                break;
            case "west":
                right = block.north();
                left = block.south();
                break;
            case "south":
                right = block.west();
                left = block.east();
                break;
            default:
                break;
        }

        const leftConnects = (left.hasTag(connect_tag) && left.permutation.getState("minecraft:cardinal_direction") == direction);
        const rightConnects = (right.hasTag(connect_tag) && right.permutation.getState("minecraft:cardinal_direction") == direction);

        if (leftConnects) left.setPermutation(left.permutation.withState('sgs_cake:right', 0));
        if (rightConnects) right.setPermutation(right.permutation.withState('sgs_cake:left', 0));
    }
}
