import { Block, Dimension, Player } from "@minecraft/server"
import { checkIfDaytime } from "../../library/worldFuntions"


/**
 * Evento Tick del componente repeaterCableSignalComponent
 * @param {{block: Block, dimension: Dimension}} event 
 */
export const clockPanelTickEvent = async ({ block, dimension }) => {
    try {
        if (block === undefined) return;
        let permutation = block.permutation;
        let time = checkIfDaytime() ? 1 : 2;

        if (block.typeId === 'minecraft:air') return


        const raycastHit = dimension.getBlockFromRay({ x: block.location.x, y: block.location.y + 1, z: block.location.z }, { x: 0, y: 1, z: 0 }, { excludeTypes: ["minecraft:air", "minecraft:glass", "minecraft:stained_glass", "nps_ec:cable", "nps_ec:reinforced_cable", "nps_ec:repeater"] });

        if (raycastHit !== undefined) time = 0;
        permutation = permutation.withState('nps_ec:time', time)

        block.setPermutation(permutation);
    } catch { }
}

/**
 * Evento OnIteract del componente clockPanelComponent
 * @param {{player: Player, block: Block, dimension: Dimension}} event 
 */
export const clockPanelOnInteractEvent = ({ player, block, dimension }) => {
    if (block === undefined) return;
    let permutation = block.permutation;

    permutation = permutation.withState('nps_ec:face', !permutation.getState('nps_ec:face'));

    block.setPermutation(permutation);
}