import { blockEntityManager } from "../blockEntitiesManager";
import { SuppEntities } from "../reg/suppEntities";
import { system, world } from "@minecraft/server";
import { getOrCreateWorldData, hasFamily, isDimensionNatural, playSwingArm } from "../util/miscUtils";
import { randomInt } from "../util/mthUtils";
import { McItems } from "../reg/mcItems";
import { SuppTags } from "../reg/suppTags";
export let globeTexture = getOrCreateWorldData("razz_sup_global_globe", randomInt(0, 9));
blockEntityManager.registerEntityBlock(SuppEntities.Globe, "razz_sup:globe" /* SuppBlocks.Globe */);
blockEntityManager.registerEntityBlock(SuppEntities.SepiaGlobe, "razz_sup:globe_sepia" /* SuppBlocks.SepiaGlobe */);
blockEntityManager.registerBEProperty("razz_sup:face_rotation");
//random texture
world.afterEvents.entitySpawn.subscribe((event) => {
    if (hasFamily(event.entity, SuppTags.Globes)) {
        const globe = event.entity;
        //we could have even used entity properties here or alter the variants component directly
        globe.triggerEvent("razz_sup:set_globe" + globeTexture);
    }
});
world.beforeEvents.itemUse.subscribe((event) => {
    if (event.itemStack.typeId == McItems.Compass) {
        const player = event.source;
        if (!player)
            return;
        // Display location to player
        system.run(() => {
            displayCurrentPos(player, player.location);
            playSwingArm(player);
        });
    }
});
export const GlobeComponent = {
    onPlayerInteract: (event) => {
        //swing
        const player = event.player;
        const globe = event.block;
        if (!player)
            return;
        const be = blockEntityManager.getOrCreateBlockEntity(globe);
        if (be) {
            const face = be.getProperty("razz_sup:face_rotation");
            //no need, controller plays the animation
            be.playAnimation("animation.razz_sup.globe_spin");
            const newFace = (face + 1) % 4;
            be.setProperty("razz_sup:face_rotation", newFace);
        }
        displayCurrentPos(player, globe.location);
    }
};
function displayCurrentPos(player, pos) {
    const obf = !isDimensionNatural(player.dimension) ? "§kO" : "";
    // Display location to player
    player.onScreenDisplay.setActionBar(`X: ${obf}${Math.floor(pos.x)}, Y: ${Math.floor(pos.y)}, Z: ${Math.floor(pos.z)}`);
}
