import * as mc from '@minecraft/server';
import * as mcui from '@minecraft/server-ui';
import * as racoAPI from '../raco-API';
import { colorSelected } from './wirelessFunctions';
import { hasRedstonePower } from "../hasRedstonePower";

export function transmitterTick(block){
    const faces = [
        'north',
        'south',
        'west',
        'east'
    ]
    let on = false
    for (const face of faces){
        if (hasRedstonePower(block[face](), ['north', 'south', 'west', 'east', 'below'], true)){
            on = true
            break
        }
    }
    if (on){
        mc.world.setDynamicProperty(`${block.dimension.id},${JSON.stringify(block.location)}`, `vtng_rt:${block.permutation.getState('vtng_rt:color')}`)
    } else if (!on){
        mc.world.setDynamicProperty(`${block.dimension.id},${JSON.stringify(block.location)}`, undefined)
    }
    if (block.permutation.getState("vtng_rt:on") == false && on){
        const loc = {
            north:{x:block.center().x, y:block.center().y+0.25, z:block.center().z-0.19},
            south:{x:block.center().x, y:block.center().y+0.25, z:block.center().z+0.19},
            west:{x:block.center().x-0.19, y:block.center().y+0.25, z:block.center().z},
            east:{x:block.center().x+0.19, y:block.center().y+0.25, z:block.center().z}
        }
        block.dimension.spawnParticle('vtng_rt:redstone_out', loc[block.permutation.getState('minecraft:cardinal_direction')])
    }
    racoAPI.setPermutation(block, 'vtng_rt:on', on)
}

export function transmitterInteract(player, block){
    const item = player.getComponent('equippable')?.getEquipment("Mainhand")
    if (item?.typeId == 'vtng_rt:pliers'){
        const selectedColor = colorSelected(player, block, setModule)
    }
}
function setModule(block, color){
    racoAPI.setPermutation(block, 'vtng_rt:color', color)
} 