import { BlockPermutation,system} from '@minecraft/server';

/*
    the code is pretty bad, however it works without constant ticking, which boost my fps and my ego
*/
export function updateState(block)
{
    if(block.hasTag('update'))
        block.setPermutation(block.permutation.withState('edx:update', true));
}

export function UpdateBlocksAround(block)
{
    const blocks = [ block.north(),block.south(),block.east(),block.west(),block.above(),block.below(),  block.north().above(),block.north().below(),block.south().above(),block.south().below(),block.east().above(),block.east().below(),block.west().above(),block.west().below() ]
    for(let i=0;i<blocks.length;i++)
    {
        updateState(blocks[i])
        updateGravity(blocks[i])
    }
}
export function updateGravity(block)
{
    if(block.hasTag('gravity') && block.below().isAir)
    {
        const id = block.typeId
        block.setType('air')
        block.dimension.spawnEntity(id,{ x: block.location.x + 0.5, y: block.location.y, z: block.location.z + 0.5 });
        system.runTimeout(() => { 
            updateGravity(block.above())
        },2)
        
    }
}
export function updateStairsAround(block)
{
    updateStair(block)
    if(block.west().hasTag('stairs'))
        updateStair(block.west(1))
    if(block.east().hasTag('stairs'))
        updateStair(block.east(1))
    if(block.north().hasTag('stairs'))
        updateStair(block.north(1))
    if(block.south().hasTag('stairs'))
        updateStair(block.south(1))
}

function updateStair(block)
{
    const west = block.west(1)//-1 0 0
    const east = block.east(1)//1 0 0
    const north = block.north(1)//0 0 -1
    const south = block.south(1)//0 0 1
    if(block.permutation?.getState('minecraft:vertical_half')=='top')
    {
        if( block.permutation?.getState('minecraft:cardinal_direction')=='west')
            {
                //west top corner
                if ( (east.hasTag('south_stairs') && east.hasTag('top_stairs')) && !(west.hasTag('south_stairs') && west.hasTag('top_stairs')) )
                {
                    block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "west"}).withState("edx:position", "inner_south"))
                }
                    
                else if ( (east.hasTag('north_stairs') && east.hasTag('top_stairs')) && !(west.hasTag('north_stairs') && west.hasTag('top_stairs')) )
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "west"}).withState("edx:position", "inner_north"))
                    }

                else if( west.hasTag('south_stairs') && west.hasTag('top_stairs') )
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "west"}).withState("edx:position", "outer_south"))
                    }
                    
                else if( west.hasTag('north_stairs') && west.hasTag('top_stairs') )
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "west"}).withState("edx:position", "outer_north"))
                    }
                    
                //west top straight
                else
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "west"}).withState("edx:position", "normal"))
                    }
                        

            }
        else if(block.permutation?.getState('minecraft:cardinal_direction')=='east')
            {
                //east top corner
                if ( !(east.hasTag('south_stairs') && east.hasTag('top_stairs')) && (west.hasTag('south_stairs') && west.hasTag('top_stairs')) )
                {
                    block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "east"}).withState("edx:position", "inner_south"))
                }
                else if ( !(east.hasTag('north_stairs') && east.hasTag('top_stairs')) && (west.hasTag('north_stairs') && west.hasTag('top_stairs')) )
                {
                    block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "east"}).withState("edx:position", "inner_north"))
                }
                    

                else if( east.hasTag('south_stairs')  && east.hasTag('top_stairs') )
                {
                    block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "east"}).withState("edx:position", "outer_south"))
                }
                    
                else if( east.hasTag('north_stairs') && east.hasTag('top_stairs') )
                {
                    block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "east"}).withState("edx:position", "outer_north"))
                }
                    
                //east top straight
                else
                {
                    block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "east"}).withState("edx:position", "normal"))
                }
                    
            }
        else if(block.permutation?.getState('minecraft:cardinal_direction')=='north')
            {
                //north top corner
                if( (south.hasTag('east_stairs') && south.hasTag('top_stairs')) && !(north.hasTag('east_stairs') && north.hasTag('top_stairs')))
                {
                    block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "north"}).withState("edx:position", "inner_east"))
                }
                    
                else if( (south.hasTag('west_stairs') && south.hasTag('top_stairs')) && !(north.hasTag('west_stairs') && north.hasTag('top_stairs')) )
                {
                    block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "north"}).withState("edx:position", "inner_west"))
                }
                
                else if( north.hasTag('east_stairs')  && north.hasTag('top_stairs') )
                {
                    block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "north"}).withState("edx:position", "outer_east"))
                }
                else if(  north.hasTag('west_stairs') && north.hasTag('top_stairs') )
                {
                    block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "north"}).withState("edx:position", "outer_west"))
                }
                //north top straight
                else
                {
                    block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "north"}).withState("edx:position", "normal"))
                }
            }
        else if(block.permutation?.getState('minecraft:cardinal_direction')=='south')
            {
                //south top corner
                if( !(south.hasTag('east_stairs') && south.hasTag('top_stairs')) && (north.hasTag('east_stairs') && north.hasTag('top_stairs')))
                {
                    block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "south"}).withState("edx:position", "inner_east"))
                }
                    
                else if( !(south.hasTag('west_stairs') && south.hasTag('top_stairs')) && (north.hasTag('west_stairs') && north.hasTag('top_stairs')) )
                {
                    block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "south"}).withState("edx:position", "inner_west"))
                }

                else if( south.hasTag('east_stairs')  && south.hasTag('top_stairs') )
                {
                    block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "south"}).withState("edx:position", "outer_east"))
                }
                else if(  south.hasTag('west_stairs') && south.hasTag('top_stairs') )
                {
                    block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "south"}).withState("edx:position", "outer_west"))
                }
                //south top straight
                else
                {
                    block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "top" , "minecraft:cardinal_direction": "south"}).withState("edx:position", "normal"))
                }
            }
    }
    if(block.permutation?.getState('minecraft:vertical_half')=='bottom')
        {
            if( block.permutation?.getState('minecraft:cardinal_direction')=='west')
                {
                    //west bottom corner
                    if ( (east.hasTag('south_stairs') && east.hasTag('bottom_stairs')) && !(west.hasTag('south_stairs') && west.hasTag('bottom_stairs')) )
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "west"}).withState("edx:position", "inner_south"))
                    }
                        
                    else if ( (east.hasTag('north_stairs') && east.hasTag('bottom_stairs')) && !(west.hasTag('north_stairs') && west.hasTag('bottom_stairs')) )
                        {
                            block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "west"}).withState("edx:position", "inner_north"))
                        }
    
                    else if( west.hasTag('south_stairs') && west.hasTag('bottom_stairs') )
                        {
                            block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "west"}).withState("edx:position", "outer_south"))
                        }
                        
                    else if( west.hasTag('north_stairs') && west.hasTag('bottom_stairs') )
                        {
                            block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "west"}).withState("edx:position", "outer_north"))
                        }
                        
                    //west bottom straight
                    else
                        {
                            block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "west"}).withState("edx:position", "normal"))
                        }
                            
    
                }
            else if(block.permutation?.getState('minecraft:cardinal_direction')=='east')
                {
                    //east bottom corner
                    if ( !(east.hasTag('south_stairs') && east.hasTag('bottom_stairs')) && (west.hasTag('south_stairs') && west.hasTag('bottom_stairs')) )
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "east"}).withState("edx:position", "inner_south"))
                    }
                    else if ( !(east.hasTag('north_stairs') && east.hasTag('bottom_stairs')) && (west.hasTag('north_stairs') && west.hasTag('bottom_stairs')) )
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "east"}).withState("edx:position", "inner_north"))
                    }
                        
    
                    else if( east.hasTag('south_stairs')  && east.hasTag('bottom_stairs') )
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "east"}).withState("edx:position", "outer_south"))
                    }
                        
                    else if( east.hasTag('north_stairs') && east.hasTag('bottom_stairs') )
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "east"}).withState("edx:position", "outer_north"))
                    }
                        
                    //east bottom straight
                    else
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "east"}).withState("edx:position", "normal"))
                    }
                        
                }
            else if(block.permutation?.getState('minecraft:cardinal_direction')=='north')
                {
                    //north bottom corner
                    if( (south.hasTag('east_stairs') && south.hasTag('bottom_stairs')) && !(north.hasTag('east_stairs') && north.hasTag('bottom_stairs')))
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "north"}).withState("edx:position", "inner_east"))
                    }
                        
                    else if( (south.hasTag('west_stairs') && south.hasTag('bottom_stairs')) && !(north.hasTag('west_stairs') && north.hasTag('bottom_stairs')) )
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "north"}).withState("edx:position", "inner_west"))
                    }
                    
                    else if( north.hasTag('east_stairs')  && north.hasTag('bottom_stairs') )
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "north"}).withState("edx:position", "outer_east"))
                    }
                    else if(  north.hasTag('west_stairs') && north.hasTag('bottom_stairs') )
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "north"}).withState("edx:position", "outer_west"))
                    }
                    //north bottom straight
                    else
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "north"}).withState("edx:position", "normal"))
                    }
                }
            else if(block.permutation?.getState('minecraft:cardinal_direction')=='south')
                {
                    //south bottom corner
                    if( !(south.hasTag('east_stairs') && south.hasTag('bottom_stairs')) && (north.hasTag('east_stairs') && north.hasTag('bottom_stairs')))
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "south"}).withState("edx:position", "inner_east"))
                    }
                        
                    else if( !(south.hasTag('west_stairs') && south.hasTag('bottom_stairs')) && (north.hasTag('west_stairs') && north.hasTag('bottom_stairs')) )
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "south"}).withState("edx:position", "inner_west"))
                    }
    
                    else if( south.hasTag('east_stairs')  && south.hasTag('bottom_stairs') )
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "south"}).withState("edx:position", "outer_east"))
                    }
                    else if(  south.hasTag('west_stairs') && south.hasTag('bottom_stairs') )
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "south"}).withState("edx:position", "outer_west"))
                    }
                    //south bottom straight
                    else
                    {
                        block.setPermutation(BlockPermutation.resolve(block.typeId, {"minecraft:vertical_half": "bottom" , "minecraft:cardinal_direction": "south"}).withState("edx:position", "normal"))
                    }
                }
        }
}