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

export class workbenchManager {

    static beforeplaceTop(data) {
        const block = data.block;
        const below = block.below();
        if (below.hasTag("sgs_cake_workbench_base")) {
            data.permutationToPlace = mc.BlockPermutation.resolve(data.permutationToPlace.type.id, { 'sgs_cake:can_craft': 1, "minecraft:cardinal_direction": data.permutationToPlace.getState("minecraft:cardinal_direction") });

            mc.system.runTimeout(() => {
                if (data.permutationToPlace.type.id == "sgs_cake:recipes") below.setPermutation(below.permutation.withState('sgs_cake:crafting_station', 1));
                if (data.permutationToPlace.type.id == "sgs_cake:carpentry") below.setPermutation(below.permutation.withState('sgs_cake:crafting_station', 5));

                if (data.permutationToPlace.type.id == "sgs_farm:fun_saw_hand") below.setPermutation(below.permutation.withState('sgs_farm:crafting_station', 2));
                if (data.permutationToPlace.type.id == "sgs_farm:fun_blueprint") below.setPermutation(below.permutation.withState('sgs_farm:crafting_station', 3));
                if (data.permutationToPlace.type.id == "sgs_farm:fun_paint_station") below.setPermutation(below.permutation.withState('sgs_farm:crafting_station', 4));

                highlightBlock(block);
                highlightBlock(below);
            }, 0)
        }

        if (below.hasTag("sgs_farm_workbench_base")) {
            data.permutationToPlace = mc.BlockPermutation.resolve(data.permutationToPlace.type.id, { 'sgs_cake:can_craft': 1, "minecraft:cardinal_direction": data.permutationToPlace.getState("minecraft:cardinal_direction") });

            mc.system.runTimeout(() => {
                highlightBlock(block);
                highlightBlock(below);
            }, 0)
        }
    }

    static destroyTop(data) {
        const block = data.block;
        const below = block.below();
        if (below.hasTag("sgs_cake_workbench_base")) {
            below.setPermutation(below.permutation.withState('sgs_cake:crafting_station', 0));
        };
        if (below.hasTag("sgs_farm_workbench_base")) {
            below.setPermutation(below.permutation.withState('sgs_farm:crafting_station', 0));
        }
    }



    static onplace(data) {
        const block = data.block;
        const above = block.above();

        if (above.hasTag("sgs_cake_workbench_top")) {
            above.setPermutation(above.permutation.withState('sgs_cake:can_craft', 1));

            if (above.hasTag("sgs_cake_workbench_recipes")) above.setPermutation(above.permutation.withState('sgs_cake:can_craft', 1));
            if (above.hasTag("sgs_cake_workbench_carpentry")) above.setPermutation(above.permutation.withState('sgs_cake:can_craft', 1));

            highlightBlock(block);
            highlightBlock(above);
        }

        if (above.hasTag("sgs_farm_workbench_top")) {
            above.setPermutation(above.permutation.withState('sgs_farm:can_craft', 1));

            if (above.typeId = "sgs_farm:fun_saw_hand") above.setPermutation(above.permutation.withState('sgs_farm:can_craft', 1));
            if (above.typeId = "sgs_farm:fun_blueprint") above.setPermutation(above.permutation.withState('sgs_farm:can_craft', 1));
            if (above.typeId = "sgs_farm:fun_paint_station") above.setPermutation(above.permutation.withState('sgs_farm:can_craft', 1));

            highlightBlock(block);
            highlightBlock(above);
        }
    }

    static destroy(data) {
        const block = data.block;
        const above = block.above();
        if (above.hasTag("sgs_cake_workbench_top")) {
            above.setPermutation(above.permutation.withState('sgs_cake:can_craft', 0));
        }
        if (above.hasTag("sgs_farm_workbench_top")) {
            above.setPermutation(above.permutation.withState('sgs_farm:can_craft', 0));
        }
    }
}