import { Direction, ItemStack } from "@minecraft/server";
import { McBlocks } from "../reg/mcBlocks";
import { getItemInHand, giveItemToPlayer, shrinkIfInSurvival, translate, transmuteBlock } from "../util/miscUtils";
import { suppEvents } from "../suppEventHandler";
import { SuppTags } from "../reg/suppTags";
import { directionFromString } from "../util/dirUtils";
import { isReplaceable } from "../util/blockProperties";
const blockToItems = new Map([
    ["razz_sup:flower_box_crimsonvine" /* SuppBlocks.FlowerBoxWeepingVine */, McBlocks.WeepingVines],
    ["razz_sup:flower_box_warpedvine" /* SuppBlocks.FlowerBoxTwistedVine */, McBlocks.TwistingVines],
    ["razz_sup:flower_box_vine" /* SuppBlocks.FlowerBoxVine */, McBlocks.Vines],
    ["razz_sup:flower_box_fern" /* SuppBlocks.FlowerBoxFern */, McBlocks.LargeFern],
    ["razz_sup:flower_box_glowberry" /* SuppBlocks.FlowerBoxGlowberry */, McBlocks.GlowBerries],
    ["razz_sup:flower_box_lilac" /* SuppBlocks.FlowerBoxLilac */, McBlocks.Lilac],
    ["razz_sup:flower_box_rosebush" /* SuppBlocks.FlowerBoxRosebush */, McBlocks.Rosebush],
    ["razz_sup:flower_box_peony" /* SuppBlocks.FlowerBoxPeony */, McBlocks.Peony],
    ["razz_sup:flower_box_pitcher" /* SuppBlocks.FlowerBoxPitcher */, McBlocks.PitcherPlant],
    ["razz_sup:flower_box_sunflower" /* SuppBlocks.FlowerBoxSunflower */, McBlocks.Sunflower],
    ["razz_sup:flower_box_sweetberry" /* SuppBlocks.FlowerBoxSweetBerry */, McBlocks.SweetBerries]
]);
const itemsToBlock = new Map([
    [McBlocks.WeepingVines, "razz_sup:flower_box_crimsonvine" /* SuppBlocks.FlowerBoxWeepingVine */],
    [McBlocks.TwistingVines, "razz_sup:flower_box_warpedvine" /* SuppBlocks.FlowerBoxTwistedVine */],
    [McBlocks.Vines, "razz_sup:flower_box_vine" /* SuppBlocks.FlowerBoxVine */],
    [McBlocks.LargeFern, "razz_sup:flower_box_fern" /* SuppBlocks.FlowerBoxFern */],
    [McBlocks.GlowBerries, "razz_sup:flower_box_glowberry" /* SuppBlocks.FlowerBoxGlowberry */],
    [McBlocks.Lilac, "razz_sup:flower_box_lilac" /* SuppBlocks.FlowerBoxLilac */],
    [McBlocks.Rosebush, "razz_sup:flower_box_rosebush" /* SuppBlocks.FlowerBoxRosebush */],
    [McBlocks.Peony, "razz_sup:flower_box_peony" /* SuppBlocks.FlowerBoxPeony */],
    [McBlocks.PitcherPlant, "razz_sup:flower_box_pitcher" /* SuppBlocks.FlowerBoxPitcher */],
    [McBlocks.Sunflower, "razz_sup:flower_box_sunflower" /* SuppBlocks.FlowerBoxSunflower */],
    [McBlocks.SweetBerries, "razz_sup:flower_box_sweetberry" /* SuppBlocks.FlowerBoxSweetBerry */]
]);
export const FlowerBoxEmptyComponent = {
    onPlayerInteract(event) {
        const player = event.player;
        const block = event.block;
        const item = getItemInHand(player);
        const flowerBox = itemsToBlock.get(item?.typeId);
        if (flowerBox) {
            //remove item
            const newBlock = transmuteBlock(block.permutation, flowerBox);
            block.setPermutation(newBlock);
            block.dimension.playSound(getSound(item.typeId), block.center(), {
                volume: 0.7,
                pitch: 1.1,
            });
            //TODO: play sound
            shrinkIfInSurvival(player, item);
        }
        else {
            player.onScreenDisplay.setActionBar(translate("message", "flower_box"));
        }
    }
};
export const FLowerBoxFullComponent = {
    onPlayerInteract(event) {
        const player = event.player;
        if (!player)
            return;
        const item = getItemInHand(event.player);
        const block = event.block;
        if (!item) {
            const pickup = blockToItems.get(event.block.typeId);
            if (pickup) {
                const item = new ItemStack(pickup, 1);
                //TODO:sound
                giveItemToPlayer(player, item);
                const newBlock = transmuteBlock(block.permutation, "razz_sup:flower_box_empty" /* SuppBlocks.FlowerBoxEmpty */);
                block.setPermutation(newBlock);
            }
        }
    }
};
function getSound(item) {
    if (item == McBlocks.WeepingVines || item == McBlocks.TwistingVines) {
        return "dig.roots";
    }
    else if (item == McBlocks.Vines) {
        return "dig.vines";
    }
    else if (item == McBlocks.GlowBerries) {
        return "dig.cave_vines";
    }
    return "dig.grass";
}
function adjustRopeBits(block, above) {
    const attachFace = directionFromString(block.permutation.getState("minecraft:block_face"));
    console.log("attachFace", attachFace);
    if (attachFace == Direction.Down && !isReplaceable(above)) { //non solid doesnt work well...
        console.log("block above invalid " + above.typeId);
        block.setPermutation(block.permutation.withState("minecraft:block_face", "up"));
    }
}
suppEvents.subscribeOnNeighborChanged((event => {
    const block = event.block;
    if (block.hasTag(SuppTags.FlowerBox) && event.fromDir == Direction.Up) {
        adjustRopeBits(block, event.neighborBlock);
    }
}));
