package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
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.properties.IntegerProperty;

public class FrostedIceBlock extends IceBlock {
    public static final IntegerProperty AGE;
    
    public FrostedIceBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Integer>setValue((Property<Comparable>)FrostedIceBlock.AGE, 0));
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        if ((random.nextInt(3) == 0 || this.fewerNeigboursThan(xd, fk, 4)) && xd.getMaxLocalRawBrightness(fk) > 11 - byj.<Integer>getValue((Property<Integer>)FrostedIceBlock.AGE) - byj.getLightBlock(xd, fk) && this.slightlyMelt(byj, xd, fk)) {
            try (final BlockPos.PooledMutableBlockPos b6 = BlockPos.PooledMutableBlockPos.acquire()) {
                for (final Direction fp11 : Direction.values()) {
                    b6.set((Vec3i)fk).move(fp11);
                    final BlockState byj2 = xd.getBlockState(b6);
                    if (byj2.getBlock() == this && !this.slightlyMelt(byj2, xd, b6)) {
                        xd.getBlockTicks().scheduleTick(b6, this, Mth.nextInt(random, 20, 40));
                    }
                }
            }
            return;
        }
        xd.getBlockTicks().scheduleTick(fk, this, Mth.nextInt(random, 20, 40));
    }
    
    private boolean slightlyMelt(final BlockState byj, final Level bjw, final BlockPos fk) {
        final int integer5 = byj.<Integer>getValue((Property<Integer>)FrostedIceBlock.AGE);
        if (integer5 < 3) {
            bjw.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Integer>setValue((Property<Comparable>)FrostedIceBlock.AGE, integer5 + 1), 2);
            return false;
        }
        this.melt(byj, bjw, fk);
        return true;
    }
    
    @Override
    public void neighborChanged(final BlockState byj, final Level bjw, final BlockPos fk3, final Block bph, final BlockPos fk5, final boolean boolean6) {
        if (bph == this && this.fewerNeigboursThan(bjw, fk3, 2)) {
            this.melt(byj, bjw, fk3);
        }
        super.neighborChanged(byj, bjw, fk3, bph, fk5, boolean6);
    }
    
    private boolean fewerNeigboursThan(final BlockGetter bjg, final BlockPos fk, final int integer) {
        int integer2 = 0;
        try (final BlockPos.PooledMutableBlockPos b6 = BlockPos.PooledMutableBlockPos.acquire()) {
            for (final Direction fp11 : Direction.values()) {
                b6.set((Vec3i)fk).move(fp11);
                if (bjg.getBlockState(b6).getBlock() == 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 bjg, final BlockPos fk, final BlockState byj) {
        return ItemStack.EMPTY;
    }
    
    static {
        AGE = BlockStateProperties.AGE_3;
    }
}
