import * as mc from '@minecraft/server';
import * as mcui from '@minecraft/server-ui';
import * as racoAPI from '../raco-API';

export const colors = [
    "black",
    "gray",
    "light_gray",
    "white",
    "blue",
    "light_blue",
    "cyan",
    "green",
    "lime",
    "yellow",
    "orange",
    "brown",
    "red",
    "pink",
    "magenta",
    "purple"
]

export const colorsCode = [
    "§0",
    "§8",
    "§8",
    "§f",
    "§9",
    "§b",
    "§3",
    "§2",
    "§a",
    "§e",
    "§6",
    "§6",
    "§4",
    "§c",
    "§d",
    "§5" 
]

export function colorSelected(player, block, setModuleFunction){
    const colorForm = new mcui.ActionFormData()
    .title("Select the color:")
    for (const color of colors){
        colorForm.button(`${colorsCode[colors.indexOf(color)]}${racoAPI.formatString(color)}`, `textures/vtng_rt/common/colors/${color}`)
    }
    colorForm.show(player)?.then((form)=>{
        if (!form?.canceled){
            setModuleFunction(block, colors[form?.selection], player)
        }
    })
}
const wirelessBlocks = [
    "vtng_rt:wireless_transmitter",
    "vtng_rt:wireless_button",
    "vtng_rt:wireless_lever",
    "vtng_rt:wireless_pressure_plate"
]
const delayBlocks = [
    "vtng_rt:adjustable_pulse_repeater"
]
mc.world.beforeEvents.playerBreakBlock.subscribe((data)=>{
    const player = data.player
    const block = data.block
    if (wirelessBlocks.includes(block.typeId)){
        if (mc.world.getDynamicProperty(`${block.dimension.id},${JSON.stringify(block.location)}`) != undefined){
            mc.world.setDynamicProperty(`${block.dimension.id},${JSON.stringify(block.location)}`, undefined)
        }
    }
    if (delayBlocks.includes(block.typeId)){
        if (mc.world.getDynamicProperty(`vtng_rt:${block.dimension.id},${JSON.stringify(block.location)},delay`) != undefined){
            mc.world.setDynamicProperty(`vtng_rt:${block.dimension.id},${JSON.stringify(block.location)},delay`, undefined)
        }
    }
})