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

/**
 * Evento del componente powerStateComponent
 * @param {{block: Block, dimension: Dimension}} event 
 */
export const powerStateEvent = ({ block, dimension }) => {
    try {
        const {
            blockNeighborDown, blockNeighborUp,
            blockNeighborEast, blockNeighborWest,
            blockNeighborNorth, blockNeighborSouth
        } = blockNeighbors(block);

        if (!blockNeighborUp || !blockNeighborDown ||
            !blockNeighborEast || !blockNeighborWest ||
            !blockNeighborNorth || !blockNeighborSouth) return;

        let permutation = block.permutation;

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

        if (!permutation.getState('nps_ec:power')) {
            if (checkAnyNeighborHasTag(block, 'power:on')) permutation = permutation.withState('nps_ec:power', 1)
        } else {
            if (!checkAnyNeighborHasTag(block, 'power:on')) permutation = permutation.withState('nps_ec:power', 0)
        }

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


/**
 * Evento del componente powerStateUpDownComponent
 * @param {{block: Block, dimension: Dimension}} event 
 */
export const powerStateUpDownEvent = ({ block, dimension }) => {
    try {
        const {
            blockNeighborDown, blockNeighborUp
        } = blockNeighbors(block);

        if (!blockNeighborUp || !blockNeighborDown) return;

        let permutation = block.permutation;

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

        if (!permutation.getState('nps_ec:power')) {
            if (checkTopOrBottomNeighborHasTag(block, 'power:on')) permutation = permutation.withState('nps_ec:power', 1)
        } else {
            if (!checkTopOrBottomNeighborHasTag(block, 'power:on')) permutation = permutation.withState('nps_ec:power', 0)
        }

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


/**
 * Evento del componente powerStateComponent
 * @param {{block: Block, dimension: Dimension}} event 
 */
export const powerPureStateEvent = ({ block, dimension }) => {
    try {
        if (block === undefined) return;
        const {
            blockNeighborDown, blockNeighborUp,
            blockNeighborEast, blockNeighborWest,
            blockNeighborNorth, blockNeighborSouth
        } = blockNeighbors(block);

        if (!blockNeighborUp || !blockNeighborDown ||
            !blockNeighborEast || !blockNeighborWest ||
            !blockNeighborNorth || !blockNeighborSouth) return;

        let permutation = block.permutation;

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

        if (!permutation.getState('nps_ec:power')) {
            if (checkAnyNeighborHasTag(block, 'power:pure')) permutation = permutation.withState('nps_ec:power', 1)
        } else {
            if (!checkAnyNeighborHasTag(block, 'power:pure')) permutation = permutation.withState('nps_ec:power', 0)
        }

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

/**
 * Evento del componente powerStateConnectorComponent
 * @param {{block: Block, dimension: Dimension}} event 
 */
export const powerStateConnectorEvent = ({ block, dimension }) => {
    try {
        if (block === undefined) return;
    const {
        blockNeighborDown, blockNeighborUp,
        blockNeighborEast, blockNeighborWest,
        blockNeighborNorth, blockNeighborSouth
    } = blockNeighbors(block);

        if (!blockNeighborUp || !blockNeighborDown ||
            !blockNeighborEast || !blockNeighborWest ||
            !blockNeighborNorth || !blockNeighborSouth) return;

        let permutation = block.permutation;

        if (block.typeId === 'minecraft:air') return
        
        if (blockNeighborUp.typeId.includes("pressure_plate") && blockNeighborUp.permutation.getState('redstone_signal')) {
            const redstoneSignal = blockNeighborUp.permutation.getState('redstone_signal');
            if (redstoneSignal) permutation = permutation.withState('nps_ec:power', 1)
            else permutation = permutation.withState('nps_ec:power', 0)
        } else {
            if (checkAnyNeighborHasTag(block, 'state:on')) permutation = permutation.withState('nps_ec:power', 1)
            if (!checkAnyNeighborHasTag(block, 'state:on')) permutation = permutation.withState('nps_ec:power', 0)
        }


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


/**
 * Evento del componente powerStateReceiverComponent
 * @param {{block: Block, dimension: Dimension}} event 
 */
export const powerStateReceiverEvent = ({ block, dimension }) => {
    if (block === undefined) return;
    const {
        blockNeighborDown, blockNeighborUp,
        blockNeighborEast, blockNeighborWest,
        blockNeighborNorth, blockNeighborSouth
    } = blockNeighbors(block);

    if (!blockNeighborUp || !blockNeighborDown ||
        !blockNeighborEast || !blockNeighborWest ||
        !blockNeighborNorth || !blockNeighborSouth) return;

    let permutation = block.permutation;

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

    if (!permutation.getState('nps_ec:power')) {
        if (checkAnyNeighborHasTag(block, 'power:on:pure')) permutation = permutation.withState('nps_ec:power', 1)
    } else {
        if (!checkAnyNeighborHasTag(block, 'power:on:pure')) permutation = permutation.withState('nps_ec:power', 0)
    }

    block.setPermutation(permutation);
}

/**
 * Evento del componente powerStateTrapdoorComponent
 * @param {{block: Block, dimension: Dimension}} event 
 */
export const powerStateTrapdoorEvent = ({ block, dimension }) => {
    if (block === undefined) return;
    const {
        blockNeighborDown, blockNeighborUp,
        blockNeighborEast, blockNeighborWest,
        blockNeighborNorth, blockNeighborSouth
    } = blockNeighbors(block);

    if (!blockNeighborUp || !blockNeighborDown ||
        !blockNeighborEast || !blockNeighborWest ||
        !blockNeighborNorth || !blockNeighborSouth) return;

    let permutation = block.permutation;


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

    if (!permutation.getState('nps_ec:power')) {
        if (checkAnyNeighborHasTag(block, 'power:receiver:on')) {
            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} ${block.z}`)
                permutation = permutation.withState('nps_ec:open_sound', true)
                permutation = permutation.withState('nps_ec:close_sound', false)
            }
        }
    } else {
        if (!checkAnyNeighborHasTag(block, 'power:receiver:on')) {
            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} ${block.z}`)
                permutation = permutation.withState('nps_ec:close_sound', true)
                permutation = permutation.withState('nps_ec:open_sound', false)
            }
        }
    }

    block.setPermutation(permutation);
}