package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.material.Fluid;
import net.minecraft.tags.Tag;
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;
import net.minecraft.world.level.block.state.BlockBehaviour;

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