package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
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.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(((((AbstractStateHolder<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 byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        if (!cvq.isHoldingItem(byj.getBlock().asItem())) {
            return byj.<Boolean>getValue((Property<Boolean>)ScaffoldingBlock.BOTTOM) ? ScaffoldingBlock.UNSTABLE_SHAPE : ScaffoldingBlock.STABLE_SHAPE;
        }
        return Shapes.block();
    }
    
    @Override
    public VoxelShape getInteractionShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk) {
        return Shapes.block();
    }
    
    @Override
    public boolean canBeReplaced(final BlockState byj, final BlockPlaceContext bcq) {
        return bcq.getItemInHand().getItem() == this.asItem();
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        final BlockPos fk3 = bcq.getClickedPos();
        final Level bjw4 = bcq.getLevel();
        final int integer5 = getDistance(bjw4, fk3);
        return ((((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)ScaffoldingBlock.WATERLOGGED, bjw4.getFluidState(fk3).getType() == Fluids.WATER)).setValue((Property<Comparable>)ScaffoldingBlock.DISTANCE, integer5)).<Comparable, Boolean>setValue((Property<Comparable>)ScaffoldingBlock.BOTTOM, this.isBottom(bjw4, fk3, integer5));
    }
    
    @Override
    public void onPlace(final BlockState byj1, final Level bjw, final BlockPos fk, final BlockState byj4, final boolean boolean5) {
        if (!bjw.isClientSide) {
            bjw.getBlockTicks().scheduleTick(fk, this, 1);
        }
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (byj1.<Boolean>getValue((Property<Boolean>)ScaffoldingBlock.WATERLOGGED)) {
            bjx.getLiquidTicks().scheduleTick(fk5, Fluids.WATER, Fluids.WATER.getTickDelay(bjx));
        }
        if (!bjx.isClientSide()) {
            bjx.getBlockTicks().scheduleTick(fk5, this, 1);
        }
        return byj1;
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        final int integer6 = getDistance(xd, fk);
        final BlockState byj2 = (((AbstractStateHolder<O, BlockState>)byj).setValue((Property<Comparable>)ScaffoldingBlock.DISTANCE, integer6)).<Comparable, Boolean>setValue((Property<Comparable>)ScaffoldingBlock.BOTTOM, this.isBottom(xd, fk, integer6));
        if (byj2.<Integer>getValue((Property<Integer>)ScaffoldingBlock.DISTANCE) == 7) {
            if (byj.<Integer>getValue((Property<Integer>)ScaffoldingBlock.DISTANCE) == 7) {
                xd.addFreshEntity(new FallingBlockEntity(xd, fk.getX() + 0.5, fk.getY(), fk.getZ() + 0.5, ((AbstractStateHolder<O, BlockState>)byj2).<Comparable, Boolean>setValue((Property<Comparable>)ScaffoldingBlock.WATERLOGGED, false)));
            }
            else {
                xd.destroyBlock(fk, true);
            }
        }
        else if (byj != byj2) {
            xd.setBlock(fk, byj2, 3);
        }
    }
    
    @Override
    public boolean canSurvive(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        return getDistance(bjz, fk) < 7;
    }
    
    @Override
    public VoxelShape getCollisionShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        if (cvq.isAbove(Shapes.block(), fk, true) && !cvq.isDescending()) {
            return ScaffoldingBlock.STABLE_SHAPE;
        }
        if (byj.<Integer>getValue((Property<Integer>)ScaffoldingBlock.DISTANCE) != 0 && byj.<Boolean>getValue((Property<Boolean>)ScaffoldingBlock.BOTTOM) && cvq.isAbove(ScaffoldingBlock.BELOW_BLOCK, fk, true)) {
            return ScaffoldingBlock.UNSTABLE_SHAPE_BOTTOM;
        }
        return Shapes.empty();
    }
    
    @Override
    public FluidState getFluidState(final BlockState byj) {
        if (byj.<Boolean>getValue((Property<Boolean>)ScaffoldingBlock.WATERLOGGED)) {
            return Fluids.WATER.getSource(false);
        }
        return super.getFluidState(byj);
    }
    
    private boolean isBottom(final BlockGetter bjg, final BlockPos fk, final int integer) {
        return integer > 0 && bjg.getBlockState(fk.below()).getBlock() != this;
    }
    
    public static int getDistance(final BlockGetter bjg, final BlockPos fk) {
        final BlockPos.MutableBlockPos a3 = new BlockPos.MutableBlockPos(fk).move(Direction.DOWN);
        final BlockState byj4 = bjg.getBlockState(a3);
        int integer5 = 7;
        if (byj4.getBlock() == Blocks.SCAFFOLDING) {
            integer5 = byj4.<Integer>getValue((Property<Integer>)ScaffoldingBlock.DISTANCE);
        }
        else if (byj4.isFaceSturdy(bjg, a3, Direction.UP)) {
            return 0;
        }
        for (final Direction fp7 : Direction.Plane.HORIZONTAL) {
            final BlockState byj5 = bjg.getBlockState(a3.set(fk).move(fp7));
            if (byj5.getBlock() != Blocks.SCAFFOLDING) {
                continue;
            }
            integer5 = Math.min(integer5, byj5.<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 cwf1 = Block.box(0.0, 14.0, 0.0, 16.0, 16.0, 16.0);
        final VoxelShape cwf2 = Block.box(0.0, 0.0, 0.0, 2.0, 16.0, 2.0);
        final VoxelShape cwf3 = Block.box(14.0, 0.0, 0.0, 16.0, 16.0, 2.0);
        final VoxelShape cwf4 = Block.box(0.0, 0.0, 14.0, 2.0, 16.0, 16.0);
        final VoxelShape cwf5 = Block.box(14.0, 0.0, 14.0, 16.0, 16.0, 16.0);
        STABLE_SHAPE = Shapes.or(cwf1, cwf2, cwf3, cwf4, cwf5);
        final VoxelShape cwf6 = Block.box(0.0, 0.0, 0.0, 2.0, 2.0, 16.0);
        final VoxelShape cwf7 = Block.box(14.0, 0.0, 0.0, 16.0, 2.0, 16.0);
        final VoxelShape cwf8 = Block.box(0.0, 0.0, 14.0, 16.0, 2.0, 16.0);
        final VoxelShape cwf9 = 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, cwf7, cwf6, cwf9, cwf8);
    }
}
