package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.util.Mth;
import net.minecraft.core.Vec3i;
import net.minecraft.core.Direction;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.BlockGetter;
import java.util.Random;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
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.IntegerProperty;

public class FrostedIceBlock extends IceBlock {
    public static final IntegerProperty AGE;
    
    public FrostedIceBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((StateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)FrostedIceBlock.AGE, 0));
    }
    
    @Override
    public void randomTick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        this.tick(cfj, zd, fu, random);
    }
    
    @Override
    public void tick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        if ((random.nextInt(3) == 0 || this.fewerNeigboursThan(zd, fu, 4)) && zd.getMaxLocalRawBrightness(fu) > 11 - cfj.<Integer>getValue((Property<Integer>)FrostedIceBlock.AGE) - cfj.getLightBlock(zd, fu) && this.slightlyMelt(cfj, zd, fu)) {
            final BlockPos.MutableBlockPos a6 = new BlockPos.MutableBlockPos();
            for (final Direction fz10 : Direction.values()) {
                a6.setWithOffset(fu, fz10);
                final BlockState cfj2 = zd.getBlockState(a6);
                if (cfj2.is(this) && !this.slightlyMelt(cfj2, zd, a6)) {
                    zd.getBlockTicks().scheduleTick(a6, this, Mth.nextInt(random, 20, 40));
                }
            }
            return;
        }
        zd.getBlockTicks().scheduleTick(fu, this, Mth.nextInt(random, 20, 40));
    }
    
    private boolean slightlyMelt(final BlockState cfj, final Level bqb, final BlockPos fu) {
        final int integer5 = cfj.<Integer>getValue((Property<Integer>)FrostedIceBlock.AGE);
        if (integer5 < 3) {
            bqb.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Integer>setValue((Property<Comparable>)FrostedIceBlock.AGE, integer5 + 1), 2);
            return false;
        }
        this.melt(cfj, bqb, fu);
        return true;
    }
    
    @Override
    public void neighborChanged(final BlockState cfj, final Level bqb, final BlockPos fu3, final Block bvr, final BlockPos fu5, final boolean boolean6) {
        if (bvr == this && this.fewerNeigboursThan(bqb, fu3, 2)) {
            this.melt(cfj, bqb, fu3);
        }
        super.neighborChanged(cfj, bqb, fu3, bvr, fu5, boolean6);
    }
    
    private boolean fewerNeigboursThan(final BlockGetter bpg, final BlockPos fu, final int integer) {
        int integer2 = 0;
        final BlockPos.MutableBlockPos a6 = new BlockPos.MutableBlockPos();
        for (final Direction fz10 : Direction.values()) {
            a6.setWithOffset(fu, fz10);
            if (bpg.getBlockState(a6).is(this) && ++integer2 >= integer) {
                return false;
            }
        }
        return true;
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(FrostedIceBlock.AGE);
    }
    
    @Override
    public ItemStack getCloneItemStack(final BlockGetter bpg, final BlockPos fu, final BlockState cfj) {
        return ItemStack.EMPTY;
    }
    
    static {
        AGE = BlockStateProperties.AGE_3;
    }
}
