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

export class stoveManager {

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

            mc.system.runTimeout(() => {
                if (data.permutationToPlace.type.id == "sgs_farm:fun_stove_pot") below.setPermutation(below.permutation.withState('sgs_farm:crafting_station', 1));
                if (data.permutationToPlace.type.id == "sgs_farm:fun_stove_pan") below.setPermutation(below.permutation.withState('sgs_farm:crafting_station', 2));
                if (data.permutationToPlace.type.id == "sgs_farm:fun_stove_oven") below.setPermutation(below.permutation.withState('sgs_farm:crafting_station', 3));

                highlightBlock(block);
                highlightBlock(below);

                block.dimension.playSound("block.campfire.crackle", block.location);
            }, 0)
        }
    }

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

stoveInteract();

function stoveInteract() {

    mc.world.beforeEvents.playerInteractWithBlock.subscribe((data) => {
        const block = data.block;
        const player = data.player;

        mc.system.runTimeout(() => {
            if (block.typeId == "sgs_farm:fun_chop_board") {
                block.dimension.playSound("sgs_farm.block.knife_chop", block.location);
            }

            if (block.typeId == "sgs_farm:fun_millstone") {
                block.dimension.playSound("block.grindstone.use", block.location);
            }

            if (block.typeId == "sgs_farm:fun_stove_oven") {
                block.dimension.playSound("block.campfire.crackle", block.location);
            }

            if (block.typeId == "sgs_farm:fun_stove_pan") {
                block.dimension.playSound("sgs_farm.block.pan_sizzle", block.location);
            }

            if (block.typeId == "sgs_farm:fun_stove_pot") {
                block.dimension.playSound("sgs_farm.block.pot_boil", block.location);
            }

            if (block.typeId == "sgs_farm:fun_saw_hand") {
                block.dimension.playSound("sgs_farm.block.workbench_saw", block.location);
            }

            if (block.typeId == "sgs_farm:fun_blueprint") {
                block.dimension.playSound("item.book.page_turn", block.location);
            }

            if (block.typeId == "sgs_farm:fun_paint_station") {
                block.dimension.playSound("bucket.fill_lava", block.location);
            }
        }, 1)
        return;
    });
}