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

export function growSensorTick(block){
    const on = block[block.permutation.getState('minecraft:cardinal_direction')]()?.permutation.getState('growth') == 7
    if (on){
        const faces = [
            block.north(),
            block.south(),
            block.west(),
            block.east()
        ]
        for (const sendBlock of faces){
            if (sendBlock?.typeId == 'minecraft:unpowered_repeater'){
                const sendDirection = sendBlock.permutation.getState('minecraft:cardinal_direction')
                const sendDelay = sendBlock.permutation.getState('repeater_delay')
                if (JSON.stringify(sendBlock[sendDirection]().location) == JSON.stringify(block.location)){
                    block.dimension.runCommand(`setblock ${sendBlock.x} ${sendBlock.y} ${sendBlock.z} powered_repeater ["minecraft:cardinal_direction"="${sendDirection}", "repeater_delay"=${sendDelay}]`)
                }
            }
        }
    }
    if (block.permutation.getState('vtng_rt:on') != on) racoAPI.setPermutation(block, 'vtng_rt:on', on)
}
export function eyeSensorTick(block){
    let on = false
    const players = block.dimension.getEntities({type:'minecraft:player', location:block[block.permutation.getState('minecraft:cardinal_direction')](3), maxDistance:4})
    for (const player of players){
        if (JSON.stringify(player.getBlockFromViewDirection()?.block.center()) == JSON.stringify(block.center()) && racoAPI.blockFaceToDirection(player.getBlockFromViewDirection()?.face) == (block.permutation.getState('minecraft:cardinal_direction'))){
            on = true
        }
    }
    if (on){
        const faces = [
            block.north(),
            block.south(),
            block.west(),
            block.east()
        ]
        for (const sendBlock of faces){
            if (sendBlock?.typeId == 'minecraft:unpowered_repeater'){
                const sendDirection = sendBlock.permutation.getState('minecraft:cardinal_direction')
                const sendDelay = sendBlock.permutation.getState('repeater_delay')
                if (JSON.stringify(sendBlock[sendDirection]().location) == JSON.stringify(block.location)){
                    block.dimension.runCommand(`setblock ${sendBlock.x} ${sendBlock.y} ${sendBlock.z} powered_repeater ["minecraft:cardinal_direction"="${sendDirection}", "repeater_delay"=${sendDelay}]`)
                }
            }
        }
    }
    if (block.permutation.getState('vtng_rt:on') != on) racoAPI.setPermutation(block, 'vtng_rt:on', on)
}

export function monsterSensorTick(block){
    const on = block.dimension.getEntities({families:['monster'], location:block.center(), maxDistance:10})?.length > 0
    if (on){
        const faces = [
            block.north(),
            block.south(),
            block.west(),
            block.east()
        ]
        for (const sendBlock of faces){
            if (sendBlock?.typeId == 'minecraft:unpowered_repeater'){
                const sendDirection = sendBlock.permutation.getState('minecraft:cardinal_direction')
                const sendDelay = sendBlock.permutation.getState('repeater_delay')
                if (JSON.stringify(sendBlock[sendDirection]().location) == JSON.stringify(block.location)){
                    block.dimension.runCommand(`setblock ${sendBlock.x} ${sendBlock.y} ${sendBlock.z} powered_repeater ["minecraft:cardinal_direction"="${sendDirection}", "repeater_delay"=${sendDelay}]`)
                }
            }
        }
    }
    if (block.permutation.getState('vtng_rt:on') != on) racoAPI.setPermutation(block, 'vtng_rt:on', on)
}
mc.system.afterEvents.scriptEventReceive.subscribe((data)=>{
    if (data.id == 'vtng_rt:activate_repeaters'){
        const block = data?.sourceEntity?.dimension?.getBlock(data?.sourceEntity?.location).below()
        if (block.permutation.getState('vtng_rt:on') == false && block.typeId == 'vtng_rt:rain_sensor') racoAPI.setPermutation(block, 'vtng_rt:on', true)
        if (block){
            const faces = [
                block.north(),
                block.south(),
                block.west(),
                block.east()
            ]
            for (const sendBlock of faces){
                if (sendBlock?.typeId == 'minecraft:unpowered_repeater'){
                    const sendDirection = sendBlock.permutation.getState('minecraft:cardinal_direction')
                    const sendDelay = sendBlock.permutation.getState('repeater_delay')
                    if (JSON.stringify(sendBlock[sendDirection]().location) == JSON.stringify(block.location)){
                        block.dimension.runCommand(`setblock ${sendBlock.x} ${sendBlock.y} ${sendBlock.z} powered_repeater ["minecraft:cardinal_direction"="${sendDirection}", "repeater_delay"=${sendDelay}]`)
                    }
                }
            }
        }
    }
    if (data.id == 'vtng_rt:deactivate_repeaters'){
        const block = data?.sourceEntity?.dimension?.getBlock(data?.sourceEntity?.location).below()
        if (block.permutation.getState('vtng_rt:on') == true && block.typeId == 'vtng_rt:rain_sensor') racoAPI.setPermutation(block, 'vtng_rt:on', false)
    }
})