package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.level.lighting.LayerLightEngine;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.state.BlockState;

public abstract class SpreadingSnowyDirtBlock extends SnowyDirtBlock {
    protected SpreadingSnowyDirtBlock(final Properties c) {
        super(c);
    }
    
    private static boolean canBeGrass(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        final BlockPos fk2 = fk.above();
        final BlockState byj2 = bjz.getBlockState(fk2);
        if (byj2.getBlock() == Blocks.SNOW && byj2.<Integer>getValue((Property<Integer>)SnowLayerBlock.LAYERS) == 1) {
            return true;
        }
        final int integer6 = LayerLightEngine.getLightBlockInto(bjz, byj, fk, byj2, fk2, Direction.UP, byj2.getLightBlock(bjz, fk2));
        return integer6 < bjz.getMaxLightLevel();
    }
    
    private static boolean canPropagate(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        final BlockPos fk2 = fk.above();
        return canBeGrass(byj, bjz, fk) && !bjz.getFluidState(fk2).is(FluidTags.WATER);
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        if (!canBeGrass(byj, xd, fk)) {
            xd.setBlockAndUpdate(fk, Blocks.DIRT.defaultBlockState());
            return;
        }
        if (xd.getMaxLocalRawBrightness(fk.above()) >= 9) {
            final BlockState byj2 = this.defaultBlockState();
            for (int integer7 = 0; integer7 < 4; ++integer7) {
                final BlockPos fk2 = fk.offset(random.nextInt(3) - 1, random.nextInt(5) - 3, random.nextInt(3) - 1);
                if (xd.getBlockState(fk2).getBlock() == Blocks.DIRT && canPropagate(byj2, xd, fk2)) {
                    xd.setBlockAndUpdate(fk2, ((AbstractStateHolder<O, BlockState>)byj2).<Comparable, Boolean>setValue((Property<Comparable>)SpreadingSnowyDirtBlock.SNOWY, xd.getBlockState(fk2.above()).getBlock() == Blocks.SNOW));
                }
            }
        }
    }
}
