import { Block, BlockPermutation, Dimension } from "@minecraft/server";
import { blockNeighbors, checkAnyNeighborHasTag, setBlockProperty, setBlockToAir } from "../../library/blockManager";

/**
 * Evento Tick del componente digitalDoorLowerComponent
 * @param {{block: Block, dimension: Dimension}} event 
 */
export const digitalDoorLowerTickEvent = ({ block, dimension }) => {
    try {
        if (!block) return;
        const {blockNeighborUp} = blockNeighbors(block);

        if(!blockNeighborUp) return;

        const isPowered = block.permutation.getState('nps_ec:power');

        let permutation = block.permutation;

        if (!isPowered && (checkAnyNeighborHasTag(block, 'power:receiver:on') || checkAnyNeighborHasTag(blockNeighborUp, 'power:receiver:on'))) {
            if(block.typeId == 'minecraft:air') return
            permutation = permutation.withState('nps_ec:open', true);
            permutation = permutation.withState('nps_ec:power', 1);

            if (!permutation.getState('nps_ec:open_sound')) {
                block.dimension.runCommand(`playsound digital.door @a ${block.x} ${block.y+1} ${block.z}`)
                permutation = permutation.withState('nps_ec:open_sound', true)
                permutation = permutation.withState('nps_ec:close_sound', false)
            }
        } else if (isPowered && !checkAnyNeighborHasTag(block, 'power:receiver:on') && !checkAnyNeighborHasTag(blockNeighborUp, 'power:receiver:on')) {
            if(block.typeId == 'minecraft:air') return
            permutation = permutation.withState('nps_ec:open', false);
            permutation = permutation.withState('nps_ec:power', 0);
            if (!permutation.getState('nps_ec:close_sound')) {
                block.dimension.runCommand(`playsound digital.door @a ${block.x} ${block.y+1} ${block.z}`)
                permutation = permutation.withState('nps_ec:close_sound', true)
                permutation = permutation.withState('nps_ec:open_sound', false)
            }
        }

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

/**
 * Evento Place del componente digitalDoorLowerComponent
 * @param {{previousBlock: BlockPermutation, dimension: Dimension, block: Block}} event 
 */
export const digitalDoorLowerPlaceEvent = ({previousBlock, dimension, block}) => {
    const {blockNeighborUp} = blockNeighbors(block);

    if(blockNeighborUp.typeId !== 'minecraft:air' && blockNeighborUp.typeId !== 'minecraft:water' && blockNeighborUp.typeId !== 'minecraft:lava' && blockNeighborUp.typeId !== 'nps_ec:digital_door_upper') {
        setBlockToAir(block, dimension);
        return
    }
    let permutation = block.permutation;


    
    dimension.runCommand(`setblock ${block.x} ${block.y+1} ${block.z} nps_ec:digital_door_upper ["nps_ec:face"="${permutation.getState('minecraft:cardinal_direction')}"]`)

    permutation = permutation.withState('nps_ec:place', true);
    block.setPermutation(permutation)
}

/**
 * Evento Tick del componente digitalUpperLowerComponent
 * @param {{block: Block, dimension: Dimension}} event 
 */
export const digitalDoorUpperTickEvent = ({ block, dimension }) => {
    try {
        if (!block) return;
        const {blockNeighborDown} = blockNeighbors(block);
        if(!blockNeighborDown) return;

        const isDoorBelowClosed = blockNeighborDown.hasTag('door:close');
        const isDoorBelowOpen = blockNeighborDown.hasTag('door:open');

        if(isDoorBelowClosed) {
            setBlockProperty(block, 'nps_ec:open', false);
        } else if(isDoorBelowOpen) {
            setBlockProperty(block, 'nps_ec:open', true);
        }
    } catch { }
}