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

export class cakeBlockManager {
    static beforeplace(data) {
        const block = data.block;
        const blockId = data.permutationToPlace.type.id;

        const north = block.north();
        const east = block.east();
        const south = block.south();
        const west = block.west();
        const up = block.above();
        const down = block.below();

        let northConnects = north.hasTag("sgs_cake_block");
        let eastConnects = east.hasTag("sgs_cake_block");
        let southConnects = south.hasTag("sgs_cake_block");
        let westConnects = west.hasTag("sgs_cake_block");
        let upConnects = up.hasTag("sgs_cake_block");
        let downConnects = down.hasTag("sgs_cake_block");
        let downGlaze = (down.hasTag("sgs_cake_block_glaze") && down.type.id == blockId);

        mc.system.runTimeout(() => {
            if (northConnects) north.setPermutation(north.permutation.withState('sgs_cake:south', 1));
            if (eastConnects) east.setPermutation(east.permutation.withState('sgs_cake:west', 1));
            if (southConnects) south.setPermutation(south.permutation.withState('sgs_cake:north', 1));
            if (westConnects) west.setPermutation(west.permutation.withState('sgs_cake:east', 1));
            if (downConnects) down.setPermutation(down.permutation.withState('sgs_cake:up', 1));
        }, 0)

        data.permutationToPlace = mc.BlockPermutation.resolve(data.permutationToPlace.type.id, {
            'sgs_cake:north': northConnects ? 1 : 0,
            'sgs_cake:east': eastConnects ? 1 : 0,
            'sgs_cake:south': southConnects ? 1 : 0,
            'sgs_cake:west': westConnects ? 1 : 0,
            'sgs_cake:up': upConnects ? 1 : 0,
            'sgs_cake:glaze': downGlaze ? 1 : 0
        });
    }

    static destroy(data) {
    }

    static interact(data) {
        const block = data.block;
        const player = data.player;
        const location = block.bottomCenter();

        const mainhand = player.getComponent(mc.EntityEquippableComponent.componentId).getEquipmentSlot(mc.EquipmentSlot.Mainhand);
        const heldItem = mainhand.getItem();

        if (heldItem?.typeId != "sgs_cake:fork") {
            return;
        }

        if (player.getGameMode() == mc.GameMode.Creative || hungerManager.addHunger(player, 10, 5)) {
            block.setType("minecraft:air");
            block.dimension.spawnParticle("sgs_cake:consume_block", location);
            block.dimension.playSound("random.burp", location);
        }
    }

    static beforeplaceSections(data) {

    }

    static interactSections(data) {
        const block = data.block;
        const player = data.player;
        const location = block.bottomCenter();

        const mainhand = player.getComponent(mc.EntityEquippableComponent.componentId).getEquipmentSlot(mc.EquipmentSlot.Mainhand);
        const heldItem = mainhand.getItem();
        if (heldItem != undefined) {
            if (heldItem.typeId == "minecraft:flint_and_steel" && block.hasTag("sgs_cake_lightable")) return;
        }


        if (player.getGameMode() == mc.GameMode.Creative || hungerManager.addHunger(player, 2, 2)) {
            block.dimension.spawnParticle("sgs_cake:consume_block", location);
            block.dimension.playSound("random.burp", location);

            let sections = block.permutation.getState("sgs_cake:sections");
            sections++;

            if (sections >= 4)   //cake has been eaten
            {
                block.setType("minecraft:air");
                return;
            }

            block.setPermutation(block.permutation.withState('sgs_cake:sections', sections));
        }
    }
}
