package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import java.util.Iterator;
import net.minecraft.core.Vec3i;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.item.FallingBlockEntity;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.core.Direction;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.phys.shapes.VoxelShape;

public class ScaffoldingBlock extends Block implements SimpleWaterloggedBlock {
    private static final VoxelShape STABLE_SHAPE;
    private static final VoxelShape UNSTABLE_SHAPE;
    private static final VoxelShape UNSTABLE_SHAPE_BOTTOM;
    private static final VoxelShape BELOW_BLOCK;
    public static final IntegerProperty DISTANCE;
    public static final BooleanProperty WATERLOGGED;
    public static final BooleanProperty BOTTOM;
    
    protected ScaffoldingBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((((StateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)ScaffoldingBlock.DISTANCE, 7)).setValue((Property<Comparable>)ScaffoldingBlock.WATERLOGGED, false)).<Comparable, Boolean>setValue((Property<Comparable>)ScaffoldingBlock.BOTTOM, false));
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(ScaffoldingBlock.DISTANCE, ScaffoldingBlock.WATERLOGGED, ScaffoldingBlock.BOTTOM);
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        if (!der.isHoldingItem(cfj.getBlock().asItem())) {
            return cfj.<Boolean>getValue((Property<Boolean>)ScaffoldingBlock.BOTTOM) ? ScaffoldingBlock.UNSTABLE_SHAPE : ScaffoldingBlock.STABLE_SHAPE;
        }
        return Shapes.block();
    }
    
    @Override
    public VoxelShape getInteractionShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu) {
        return Shapes.block();
    }
    
    @Override
    public boolean canBeReplaced(final BlockState cfj, final BlockPlaceContext bin) {
        return bin.getItemInHand().getItem() == this.asItem();
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        final BlockPos fu3 = bin.getClickedPos();
        final Level bqb4 = bin.getLevel();
        final int integer5 = getDistance(bqb4, fu3);
        return ((((StateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)ScaffoldingBlock.WATERLOGGED, bqb4.getFluidState(fu3).getType() == Fluids.WATER)).setValue((Property<Comparable>)ScaffoldingBlock.DISTANCE, integer5)).<Comparable, Boolean>setValue((Property<Comparable>)ScaffoldingBlock.BOTTOM, this.isBottom(bqb4, fu3, integer5));
    }
    
    @Override
    public void onPlace(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        if (!bqb.isClientSide) {
            bqb.getBlockTicks().scheduleTick(fu, this, 1);
        }
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        if (cfj1.<Boolean>getValue((Property<Boolean>)ScaffoldingBlock.WATERLOGGED)) {
            bqc.getLiquidTicks().scheduleTick(fu5, Fluids.WATER, Fluids.WATER.getTickDelay(bqc));
        }
        if (!bqc.isClientSide()) {
            bqc.getBlockTicks().scheduleTick(fu5, this, 1);
        }
        return cfj1;
    }
    
    @Override
    public void tick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        final int integer6 = getDistance(zd, fu);
        final BlockState cfj2 = (((StateHolder<O, BlockState>)cfj).setValue((Property<Comparable>)ScaffoldingBlock.DISTANCE, integer6)).<Comparable, Boolean>setValue((Property<Comparable>)ScaffoldingBlock.BOTTOM, this.isBottom(zd, fu, integer6));
        if (cfj2.<Integer>getValue((Property<Integer>)ScaffoldingBlock.DISTANCE) == 7) {
            if (cfj.<Integer>getValue((Property<Integer>)ScaffoldingBlock.DISTANCE) == 7) {
                zd.addFreshEntity(new FallingBlockEntity(zd, fu.getX() + 0.5, fu.getY(), fu.getZ() + 0.5, ((StateHolder<O, BlockState>)cfj2).<Comparable, Boolean>setValue((Property<Comparable>)ScaffoldingBlock.WATERLOGGED, false)));
            }
            else {
                zd.destroyBlock(fu, true);
            }
        }
        else if (cfj != cfj2) {
            zd.setBlock(fu, cfj2, 3);
        }
    }
    
    @Override
    public boolean canSurvive(final BlockState cfj, final LevelReader bqd, final BlockPos fu) {
        return getDistance(bqd, fu) < 7;
    }
    
    @Override
    public VoxelShape getCollisionShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        if (der.isAbove(Shapes.block(), fu, true) && !der.isDescending()) {
            return ScaffoldingBlock.STABLE_SHAPE;
        }
        if (cfj.<Integer>getValue((Property<Integer>)ScaffoldingBlock.DISTANCE) != 0 && cfj.<Boolean>getValue((Property<Boolean>)ScaffoldingBlock.BOTTOM) && der.isAbove(ScaffoldingBlock.BELOW_BLOCK, fu, true)) {
            return ScaffoldingBlock.UNSTABLE_SHAPE_BOTTOM;
        }
        return Shapes.empty();
    }
    
    @Override
    public FluidState getFluidState(final BlockState cfj) {
        if (cfj.<Boolean>getValue((Property<Boolean>)ScaffoldingBlock.WATERLOGGED)) {
            return Fluids.WATER.getSource(false);
        }
        return super.getFluidState(cfj);
    }
    
    private boolean isBottom(final BlockGetter bpg, final BlockPos fu, final int integer) {
        return integer > 0 && !bpg.getBlockState(fu.below()).is(this);
    }
    
    public static int getDistance(final BlockGetter bpg, final BlockPos fu) {
        final BlockPos.MutableBlockPos a3 = fu.mutable().move(Direction.DOWN);
        final BlockState cfj4 = bpg.getBlockState(a3);
        int integer5 = 7;
        if (cfj4.is(Blocks.SCAFFOLDING)) {
            integer5 = cfj4.<Integer>getValue((Property<Integer>)ScaffoldingBlock.DISTANCE);
        }
        else if (cfj4.isFaceSturdy(bpg, a3, Direction.UP)) {
            return 0;
        }
        for (final Direction fz7 : Direction.Plane.HORIZONTAL) {
            final BlockState cfj5 = bpg.getBlockState(a3.setWithOffset(fu, fz7));
            if (!cfj5.is(Blocks.SCAFFOLDING)) {
                continue;
            }
            integer5 = Math.min(integer5, cfj5.<Integer>getValue((Property<Integer>)ScaffoldingBlock.DISTANCE) + 1);
            if (integer5 == 1) {
                break;
            }
        }
        return integer5;
    }
    
    static {
        UNSTABLE_SHAPE_BOTTOM = Block.box(0.0, 0.0, 0.0, 16.0, 2.0, 16.0);
        BELOW_BLOCK = Shapes.block().move(0.0, -1.0, 0.0);
        DISTANCE = BlockStateProperties.STABILITY_DISTANCE;
        WATERLOGGED = BlockStateProperties.WATERLOGGED;
        BOTTOM = BlockStateProperties.BOTTOM;
        final VoxelShape dfg1 = Block.box(0.0, 14.0, 0.0, 16.0, 16.0, 16.0);
        final VoxelShape dfg2 = Block.box(0.0, 0.0, 0.0, 2.0, 16.0, 2.0);
        final VoxelShape dfg3 = Block.box(14.0, 0.0, 0.0, 16.0, 16.0, 2.0);
        final VoxelShape dfg4 = Block.box(0.0, 0.0, 14.0, 2.0, 16.0, 16.0);
        final VoxelShape dfg5 = Block.box(14.0, 0.0, 14.0, 16.0, 16.0, 16.0);
        STABLE_SHAPE = Shapes.or(dfg1, dfg2, dfg3, dfg4, dfg5);
        final VoxelShape dfg6 = Block.box(0.0, 0.0, 0.0, 2.0, 2.0, 16.0);
        final VoxelShape dfg7 = Block.box(14.0, 0.0, 0.0, 16.0, 2.0, 16.0);
        final VoxelShape dfg8 = Block.box(0.0, 0.0, 14.0, 16.0, 2.0, 16.0);
        final VoxelShape dfg9 = Block.box(0.0, 0.0, 0.0, 16.0, 2.0, 2.0);
        UNSTABLE_SHAPE = Shapes.or(ScaffoldingBlock.UNSTABLE_SHAPE_BOTTOM, ScaffoldingBlock.STABLE_SHAPE, dfg7, dfg6, dfg9, dfg8);
    }
}
