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

export function hasRedstonePower(block, faces, first){
    for (const thisFace of faces){
        try {
        const thisBlock = block[thisFace]()
        if (thisBlock?.typeId != 'minecraft:air'){
            if (thisBlock.typeId == 'minecraft:lever'){
                const cuBlock = thisBlock[racoAPI.invertFace(racoAPI.blockFaceToDirection(thisBlock.permutation.getState('lever_direction')))]()
                if (first || JSON.stringify(cuBlock.location) == JSON.stringify(block.location)){
                    if (thisBlock.permutation.getState('open_bit') == true){
                        return true
                    }
                }
            }
            if (thisBlock.typeId.includes('button')){
                const cuBlock = thisBlock[racoAPI.invertFace(racoAPI.invertUpDown(racoAPI.blockFaceToDirection(racoAPI.numToDirection(thisBlock.permutation.getState('facing_direction')))))]()
                if (first || JSON.stringify(cuBlock.location) == JSON.stringify(block.location)){
                    if (thisBlock.permutation.getState('button_pressed_bit') == true){
                        return true
                    }
                }
            }
            if (thisBlock.typeId == 'minecraft:powered_repeater' || thisBlock.typeId == 'minecraft:powered_comparator'){
                const cuBlock = thisBlock[racoAPI.invertFace(thisBlock.permutation.getState('minecraft:cardinal_direction'))]()
                if (JSON.stringify(cuBlock.location) == JSON.stringify(block.location)){
                    return true
                }
            }
            if (thisBlock.typeId == 'minecraft:redstone_block'){
                return true
            }
            if (thisBlock.typeId == 'minecraft:redstone_torch'){
                const direction = thisBlock.permutation.getState('torch_facing_direction')
                if (thisFace == 'below') return true
                if (direction == 'unknown' || direction == 'top'){
                    if (
                        thisFace == 'north' ||
                        thisFace == 'south' ||
                        thisFace == 'west' ||
                        thisFace == 'east'
                    ){
                        return true
                    }
                } else {
                    const cuBlock = thisBlock[direction](-1)
                    const cuBlock2 = thisBlock[direction]()
                    //if (JSON.stringify(cuBlock.location) == JSON.stringify(block.location)){
                        if (JSON.stringify(cuBlock2.location) != JSON.stringify(block.location)){
                            return true
                        }
                    //}
                }
            }
            if (thisBlock.typeId == 'rc_fb:wireless_redstone'){
                if (thisBlock.permutation.getState('rc_fb:on') == true && thisBlock.permutation.getState('rc_fb:mode') == 'receiver'){
                    if (
                        thisFace == 'north' ||
                        thisFace == 'south' ||
                        thisFace == 'west' ||
                        thisFace == 'above' ||
                        thisFace == 'east'
                    ){
                        return true
                    }}
            }
            if (thisBlock.typeId.includes('pressure_plate') || thisBlock.typeId.includes('daylight_detector')){
                if (thisBlock.permutation.getState('redstone_signal') > 0){
                    if (
                        thisFace == 'north' ||
                        thisFace == 'south' ||
                        thisFace == 'west' ||
                        thisFace == 'above' ||
                        thisFace == 'east'
                    ){
                        return true
                    }}
            }
            if (thisBlock.typeId == 'minecraft:observer'){
                const direction = thisBlock.permutation.getState('minecraft:facing_direction')
                if (thisBlock.permutation.getState('powered_bit') == true){
                    const cuBlock = thisBlock[racoAPI.invertFace(racoAPI.blockFaceToDirection(direction))]()
                    if (JSON.stringify(cuBlock.location) == JSON.stringify(block.location)){
                        return true
                    }
                }
            }
            if (thisBlock.typeId == 'minecraft:redstone_wire'){
                const signal = thisBlock.permutation.getState('redstone_signal')
                if (signal > 0){
                    if (thisFace == 'above') return true
                    if (block.typeId == 'minecraft:target') return true
                    if (thisFace == 'north' || thisFace == 'south'){
                        if (
                            !redstoneConnect(thisBlock, 'west') &&
                            !redstoneConnect(thisBlock, 'east')
                        ){
                            return true
                        }
                    }
                    if (thisFace == 'west' || thisFace == 'east'){
                        if (
                            !redstoneConnect(thisBlock, 'north') &&
                            !redstoneConnect(thisBlock, 'south')
                        ){
                            return true
                        }
                    }
                }
            }
            if (isSolid(thisBlock) && first){
                if (hasRedstonePower(thisBlock, ['north', 'south', 'west', 'east', 'above', 'below'], false)) return true
            }
        }
        } catch (error) {
        }
    }
}
function redstoneConnect(block, connectedFace){
    const connectedBlock = block[connectedFace]()
    if (block?.typeId == 'minecraft:redstone_wire'){
        if (connectedBlock?.typeId == 'minecraft:redstone_wire'){
            return true
        }
        if (connectedBlock.above()?.typeId == 'minecraft:redstone_wire'){
            if (!isSolid(block.above())){
                return true
            }
        }
        if (connectedBlock.below()?.typeId == 'minecraft:redstone_wire'){
            if (!isSolid(connectedBlock)){
                return true
            }
        }
        if (connectedBlock?.typeId == 'minecraft:target'){
            return true
        }
        if (connectedBlock?.typeId == 'minecraft:redstone_block'){
            return true
        }
        if (connectedBlock?.typeId.includes('button')){
            return true
        }
        if (connectedBlock?.typeId == 'minecraft:lever'){
            return true
        }
        if (connectedBlock?.typeId == 'minecraft:redstone_torch'){
            return true
        }
        if (connectedBlock?.typeId.includes('pressure_plate')){
            return true
        }
        if (connectedBlock?.typeId.includes('daylight_detector')){
            return true
        }
        if (connectedBlock?.typeId.includes('repeater') || connectedBlock?.typeId.includes('comparator')){
            if (connectedBlock.permutation.getState('minecraft:cardinal_direction') == connectedFace){
                return true
            }
            if (connectedBlock.permutation.getState('minecraft:cardinal_direction') == racoAPI.invertFace(connectedFace)){
                return true
            }
        }
        if (connectedBlock?.typeId == 'minecraft:observer'){
            if (connectedBlock.permutation.getState('minecraft:facing_direction') == connectedFace){
                return true
            }
        }
        if (connectedBlock?.typeId.includes('piston')){
            const cuBlock = connectedBlock[racoAPI.invertFace(racoAPI.invertUpDown(racoAPI.blockFaceToDirection(racoAPI.numToDirection(connectedBlock.permutation.getState('facing_direction')))))]()
            if (connectedBlock){
                if (JSON.stringify(cuBlock.location) != JSON.stringify(block.location)){
                    return true
                }
            }
        }
    }
}


function isSolid(blockTested) {
    let block = blockTested.typeId
    const nonSolidBlocks = new Set([
        "air", "dandelion", "poppy", "blue_orchid", "allium", "azure_bluet", "red_tulip", "orange_tulip", "white_tulip", "pink_tulip", "oxeye_daisy", "sunflower", "lilac",
        "rose_bush", "peony", "cornflower", "lily_of_the_valley", "tallgrass", "fern", "large_fern", "deadbush", "seagrass", "air", "waterlily", "vine", "ladder", "rail",
        "golden_rail", "activator_rail", "tripwire_hook", "beacon", "web", "snow_layer", "iron_bars", "chain", "scaffolding", "bamboo", "sea_pickle", "conduit",
        "lightning_rod", "sweet_berry_bush", "small_dripleaf_block", "big_dripleaf", "flower_pot", "amethyst_cluster", "budding_amethyst", "pointed_dripstone", 
        "glow_lichen",  "spore_blossom", "hanging_roots", "end_rod", "bell", "lantern", "soul_lantern", "chain", "soul_fire", "bubble_column", "redstone_wire", "rc_fb:vacuum_hopper"
    ]);
    if (block.includes('slab') || block.includes('glass') || block.includes('carpet') || block.includes('leaves') || block.includes('vines') || block.includes('logic_gate')
    || block.includes('coral') || block.includes('torch') || block.includes('mushroom') || block.includes('sign') || block.includes('banner')
    || block.includes('frame') || block.includes('portal') || block.includes('campfire') || nonSolidBlocks.has(block.replace('minecraft:', ''))) return false;
    else return true;
}