import { blockEntityManager } from "../blockEntitiesManager";
import { SuppEntities } from "../reg/suppEntities";
import { system, world } from "@minecraft/server";
import { McItems } from "../reg/mcItems";
import { isDimensionNatural, playSwingArm } from "../util/miscUtils";
blockEntityManager.registerEntityBlock(SuppEntities.Clock, "razz_sup:clock_block" /* SuppBlocks.Clock */, {
    onCreated(entity) {
        entity.setProperty("razz_sup:can_read_time", isDimensionNatural(entity.dimension));
    }
});
world.beforeEvents.itemUse.subscribe((event) => {
    if (event.itemStack.typeId == McItems.Clock) {
        const player = event.source;
        if (!player)
            return;
        // Display location to player
        system.run(() => {
            displayCurrentHour(player);
            playSwingArm(player);
        });
    }
});
export const ClockComponent = {
    onPlayerInteract: (event) => {
        //swing
        const player = event.player;
        if (!player)
            return;
        // Display location to player
        displayCurrentHour(player);
    }
};
function displayCurrentHour(player) {
    const dim = player.dimension;
    let time = ((world.getTimeOfDay() + 6000) % 24000);
    const minutes = Math.floor(((time % 1000) / 1000) * 60);
    const hours = Math.floor(time / 1000);
    const postfix = "";
    let ob = "";
    let br = "";
    if (!isDimensionNatural(dim)) {
        time = Math.floor(Math.random() * 24000);
        ob += '§k'; // obfuscated
        br += '§r'; // reset
    }
    let minutesFormatted = minutes.toString().padStart(2, '0');
    const timeStr = ob + hours + br + ":" + ob + minutesFormatted + br + postfix;
    player.onScreenDisplay.setActionBar(`Time: ${timeStr}`);
}
