package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.StateDefinition;
import javax.annotation.Nullable;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LightLayer;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.core.Direction;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.level.block.state.properties.IntegerProperty;

public class SnowLayerBlock extends Block {
    public static final IntegerProperty LAYERS;
    protected static final VoxelShape[] SHAPE_BY_LAYER;
    
    protected SnowLayerBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)SnowLayerBlock.LAYERS, 1));
    }
    
    @Override
    public boolean isPathfindable(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final PathComputationType cqr) {
        switch (cqr) {
            case LAND: {
                return byj.<Integer>getValue((Property<Integer>)SnowLayerBlock.LAYERS) < 5;
            }
            case WATER: {
                return false;
            }
            case AIR: {
                return false;
            }
            default: {
                return false;
            }
        }
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return SnowLayerBlock.SHAPE_BY_LAYER[byj.<Integer>getValue((Property<Integer>)SnowLayerBlock.LAYERS)];
    }
    
    @Override
    public VoxelShape getCollisionShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return SnowLayerBlock.SHAPE_BY_LAYER[byj.<Integer>getValue((Property<Integer>)SnowLayerBlock.LAYERS) - 1];
    }
    
    @Override
    public boolean useShapeForLightOcclusion(final BlockState byj) {
        return true;
    }
    
    @Override
    public boolean canSurvive(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        final BlockState byj2 = bjz.getBlockState(fk.below());
        final Block bph6 = byj2.getBlock();
        return bph6 != Blocks.ICE && bph6 != Blocks.PACKED_ICE && bph6 != Blocks.BARRIER && (bph6 == Blocks.HONEY_BLOCK || bph6 == Blocks.SOUL_SAND || Block.isFaceFull(byj2.getCollisionShape(bjz, fk.below()), Direction.UP) || (bph6 == this && byj2.<Integer>getValue((Property<Integer>)SnowLayerBlock.LAYERS) == 8));
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (!byj1.canSurvive(bjx, fk5)) {
            return Blocks.AIR.defaultBlockState();
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        if (xd.getBrightness(LightLayer.BLOCK, fk) > 11) {
            Block.dropResources(byj, xd, fk);
            xd.removeBlock(fk, false);
        }
    }
    
    @Override
    public boolean canBeReplaced(final BlockState byj, final BlockPlaceContext bcq) {
        final int integer4 = byj.<Integer>getValue((Property<Integer>)SnowLayerBlock.LAYERS);
        if (bcq.getItemInHand().getItem() == this.asItem() && integer4 < 8) {
            return !bcq.replacingClickedOnBlock() || bcq.getClickedFace() == Direction.UP;
        }
        return integer4 == 1;
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        final BlockState byj3 = bcq.getLevel().getBlockState(bcq.getClickedPos());
        if (byj3.getBlock() == this) {
            final int integer4 = byj3.<Integer>getValue((Property<Integer>)SnowLayerBlock.LAYERS);
            return ((AbstractStateHolder<O, BlockState>)byj3).<Comparable, Integer>setValue((Property<Comparable>)SnowLayerBlock.LAYERS, Math.min(8, integer4 + 1));
        }
        return super.getStateForPlacement(bcq);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(SnowLayerBlock.LAYERS);
    }
    
    static {
        LAYERS = BlockStateProperties.LAYERS;
        SHAPE_BY_LAYER = new VoxelShape[] { Shapes.empty(), Block.box(0.0, 0.0, 0.0, 16.0, 2.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 4.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 6.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 8.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 10.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 12.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 14.0, 16.0), Block.box(0.0, 0.0, 0.0, 16.0, 16.0, 16.0) };
    }
}
