import { BlockPermutation, ItemStack, Player, world } from "@minecraft/server";
import { McItems } from "../reg/mcItems";
import { getItemInHand, giveItemToPlayer, isSpawnEgg, shrinkIfInSurvival, swapItem } from "../util/miscUtils";
import { SuppStates } from "../reg/suppStates";
import { SuppItems } from "../reg/suppItems";
import { attemptPlacingAt } from "../util/placementUtils";
const BUCKET_FILL_AMOUNT = 3;
const MAX_LAYERS = 12;
function spawnJarWithTag(perm, block) {
    const fillingType = getJarFillingType(perm);
    if (fillingType == FillingType.Empty || fillingType == FillingType.Cookie)
        return;
    const volumeCount = getJarVolume(perm);
    if (volumeCount <= 0)
        return;
    const itemStack = new ItemStack(SuppItems.FilledJar, 1);
    itemStack.setDynamicProperty(SuppStates.FillingType, fillingType);
    itemStack.setDynamicProperty(SuppStates.VolumeCount, volumeCount);
    itemStack.setLore([`[${volumeCount} ${fillingType}]`]);
    block.dimension.spawnItem(itemStack, block.center());
}
world.afterEvents.blockExplode.subscribe((event => {
    const block = event.explodedBlockPermutation.type.id;
    if (block != "razz_sup:jar" /* SuppBlocks.Jar */)
        return;
    spawnJarWithTag(event.explodedBlockPermutation, event.block);
}));
world.afterEvents.playerBreakBlock.subscribe((event) => {
    const id = event.brokenBlockPermutation.type.id;
    if (id != "razz_sup:jar" /* SuppBlocks.Jar */)
        return;
    spawnJarWithTag(event.brokenBlockPermutation, event.block);
});
export const FilledJarComponent = {
    onUseOn(event) {
        const block = event.block;
        const face = event.blockFace;
        const item = event.itemStack;
        let blockPerm = BlockPermutation.resolve("razz_sup:jar" /* SuppBlocks.Jar */);
        const fillingType = item.getDynamicProperty(SuppStates.FillingType);
        const volumeCount = item.getDynamicProperty(SuppStates.VolumeCount);
        if (fillingType && volumeCount) {
            blockPerm = blockPerm
                .withState(SuppStates.FillingType, fillingType)
                .withState(SuppStates.VolumeCount, volumeCount);
        }
        if (attemptPlacingAt(block, face, blockPerm)) {
            block.dimension.playSound("dig.stone", block.center(), { volume: 0.8, pitch: 1 });
            const player = event.source;
            if (player instanceof Player)
                shrinkIfInSurvival(player, item, 1);
        }
    }
};
export const JarComponent = {
    //cookie not come out todo
    onPlayerInteract(event) {
        const block = event.block;
        if (block.typeId != "razz_sup:jar" /* SuppBlocks.Jar */)
            return;
        const player = event.player;
        if (!player)
            return;
        const item = getItemInHand(player);
        const id = item ? item.typeId : undefined;
        if (!block)
            return;
        else if (id == McItems.Cookie) {
            tryTransferJarContent(block, FillingType.Cookie, 1, item, player, undefined, "block.itemframe.add_item");
        }
        else if (id == McItems.WaterBucket) {
            tryTransferJarContent(block, FillingType.Water, 3, item, player, McItems.EmptyBucket, "bucket.empty_water");
        }
        else if (id == McItems.MilkBucket) {
            tryTransferJarContent(block, FillingType.Milk, 3, item, player, McItems.EmptyBucket, "bucket.empty_water");
        }
        else if (id == SuppItems.ChocolateBucket) {
            tryTransferJarContent(block, FillingType.Chocolate, 3, item, player, McItems.EmptyBucket, "bucket.empty_water");
        }
        else if (id == McItems.LavaBucket) {
            tryTransferJarContent(block, FillingType.Lava, 3, item, player, McItems.EmptyBucket, "bucket.empty_lava");
        }
        //too bad
        else if (id == McItems.Potion) {
            tryTransferJarContent(block, FillingType.Water, 1, item, player, McItems.Bottle, "bottle.empty");
        }
        else if (id == McItems.HoneyBottle) {
            tryTransferJarContent(block, FillingType.Honey, 1, item, player, McItems.Bottle, "bottle.empty");
        }
        else if (item && isSpawnEgg(item)) {
            player.onScreenDisplay.setActionBar("...");
        }
        //TODO: add? idk why this doesnt show up, its on the wiki
        /*
        if (id == McItems.Potion) {
            const potionType = item.getComponent("minecraft:potion");
            if (potionType && potionType.potionEffectType == "water") {
                event.cancel = addLayers(block, item, player, 1, FillingType.Water, McItems.Bottle);
            }
        }*/
        //decrement
        if (id == McItems.EmptyBucket) {
            tryTransferJarContent(block, FillingType.Water, -3, item, player, McItems.WaterBucket, "bucket.fill_water");
            tryTransferJarContent(block, FillingType.Lava, -3, item, player, McItems.LavaBucket, "bucket.fill_lava");
            tryTransferJarContent(block, FillingType.Chocolate, -3, item, player, SuppItems.ChocolateBucket, "bucket.fill_water");
            tryTransferJarContent(block, FillingType.Milk, -3, item, player, McItems.MilkBucket, "bucket.fill_water");
        }
        else if (id == McItems.Bottle) {
            tryTransferJarContent(block, FillingType.Water, -1, item, player, McItems.Potion, "bottle.fill");
            tryTransferJarContent(block, FillingType.Honey, -1, item, player, McItems.HoneyBottle, "bottle.fill");
        }
        else if (!id) {
            tryTransferJarContent(block, FillingType.Cookie, -1, undefined, player, McItems.Cookie, "random.pop");
        }
    }
};
export function getJarVolume(permutation) {
    return permutation.getState(SuppStates.VolumeCount);
}
export function tryTransferJarContent(block, fillingType, incrementValue, item, player, replacementItemType, sound) {
    const currentVolumeCount = getJarVolume(block.permutation);
    const currentFillingType = getJarFillingType(block.permutation);
    const newVolumeCount = currentVolumeCount + incrementValue;
    if (newVolumeCount > MAX_LAYERS || newVolumeCount < 0)
        return false;
    if (currentFillingType != FillingType.Empty && currentFillingType !== fillingType)
        return false;
    //TODO: sounds
    block.setPermutation(block.permutation
        .withState(SuppStates.FillingType, newVolumeCount <= 0 ? FillingType.Empty : fillingType)
        .withState(SuppStates.VolumeCount, newVolumeCount));
    if (item && player) {
        swapItem(player, item, replacementItemType);
    }
    else if (!item && player && replacementItemType) {
        giveItemToPlayer(player, new ItemStack(replacementItemType));
    }
    if (sound) {
        const level = block.dimension;
        level.playSound(sound, block.center(), { volume: 0.8, pitch: 1 });
    }
    return true;
}
export function getJarFillingType(permutation) {
    const fillingType = permutation.getState(SuppStates.FillingType);
    if (!fillingType)
        return FillingType.Empty;
    return Object.values(FillingType).find(value => value === fillingType);
}
export var FillingType;
(function (FillingType) {
    FillingType["Cookie"] = "cookie";
    FillingType["Chocolate"] = "chocolate";
    FillingType["Milk"] = "milk";
    FillingType["Water"] = "water";
    FillingType["Lava"] = "lava";
    FillingType["Honey"] = "honey";
    FillingType["Empty"] = "empty";
})(FillingType || (FillingType = {}));
function isLiquid(ft) {
    return ft === FillingType.Water || ft === FillingType.Lava || ft === FillingType.Honey;
}
