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

mc.world.afterEvents.itemUse.subscribe((data)=>{
    const player = data.source
    const item = data.itemStack
    if (item?.typeId == 'vtng_rt:wireless_remote'){
        if (player.isSneaking || !item?.getDynamicProperty('vtng_rt:frequency')){
            colorSelected(player, undefined, setRemote)
        } else {
            if (item?.getDynamicProperty('vtng_rt:frequency') && !player.isSneaking) 
                mc.world.setDynamicProperty(player.nameTag, item?.getDynamicProperty('vtng_rt:frequency'))
            mc.system.runTimeout(()=>{
                mc.world.setDynamicProperty(player.nameTag, undefined)
            },5)
        }
    }
})
function setRemote(block, color, player){
    const item = player.getComponent('equippable')?.getEquipment("Mainhand")
    item?.setDynamicProperty('vtng_rt:frequency', color)
    item.setLore([`§r${colorsCode[colors.indexOf(color)]}${racoAPI.formatString(color)}`])
    player.getComponent('equippable')?.setEquipment("Mainhand", item)
} 