package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.StateHolder;
import java.util.WeakHashMap;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import com.google.common.collect.Lists;
import net.minecraft.world.level.block.state.StateDefinition;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.core.Direction;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.DustParticleOptions;
import net.minecraft.world.level.block.state.BlockBehaviour;
import java.util.List;
import net.minecraft.world.level.BlockGetter;
import java.util.Map;
import net.minecraft.world.level.block.state.properties.BooleanProperty;

public class RedstoneTorchBlock extends TorchBlock {
    public static final BooleanProperty LIT;
    private static final Map<BlockGetter, List<Toggle>> RECENT_TOGGLES;
    
    protected RedstoneTorchBlock(final Properties c) {
        super(c, DustParticleOptions.REDSTONE);
        this.registerDefaultState(((StateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Boolean>setValue((Property<Comparable>)RedstoneTorchBlock.LIT, true));
    }
    
    @Override
    public void onPlace(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        for (final Direction fz10 : Direction.values()) {
            bqb.updateNeighborsAt(fu.relative(fz10), this);
        }
    }
    
    @Override
    public void onRemove(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        if (boolean5) {
            return;
        }
        for (final Direction fz10 : Direction.values()) {
            bqb.updateNeighborsAt(fu.relative(fz10), this);
        }
    }
    
    @Override
    public int getSignal(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final Direction fz) {
        if (cfj.<Boolean>getValue((Property<Boolean>)RedstoneTorchBlock.LIT) && Direction.UP != fz) {
            return 15;
        }
        return 0;
    }
    
    protected boolean hasNeighborSignal(final Level bqb, final BlockPos fu, final BlockState cfj) {
        return bqb.hasSignal(fu.below(), Direction.DOWN);
    }
    
    @Override
    public void tick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        final boolean boolean6 = this.hasNeighborSignal(zd, fu, cfj);
        final List<Toggle> list7 = RedstoneTorchBlock.RECENT_TOGGLES.get(zd);
        while (list7 != null && !list7.isEmpty() && zd.getGameTime() - list7.get(0).when > 60L) {
            list7.remove(0);
        }
        if (cfj.<Boolean>getValue((Property<Boolean>)RedstoneTorchBlock.LIT)) {
            if (boolean6) {
                zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)RedstoneTorchBlock.LIT, false), 3);
                if (isToggledTooFrequently(zd, fu, true)) {
                    zd.levelEvent(1502, fu, 0);
                    zd.getBlockTicks().scheduleTick(fu, zd.getBlockState(fu).getBlock(), 160);
                }
            }
        }
        else if (!boolean6 && !isToggledTooFrequently(zd, fu, false)) {
            zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)RedstoneTorchBlock.LIT, true), 3);
        }
    }
    
    @Override
    public void neighborChanged(final BlockState cfj, final Level bqb, final BlockPos fu3, final Block bvr, final BlockPos fu5, final boolean boolean6) {
        if (cfj.<Boolean>getValue((Property<Boolean>)RedstoneTorchBlock.LIT) == this.hasNeighborSignal(bqb, fu3, cfj) && !bqb.getBlockTicks().willTickThisTick(fu3, this)) {
            bqb.getBlockTicks().scheduleTick(fu3, this, 2);
        }
    }
    
    @Override
    public int getDirectSignal(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final Direction fz) {
        if (fz == Direction.DOWN) {
            return cfj.getSignal(bpg, fu, fz);
        }
        return 0;
    }
    
    @Override
    public boolean isSignalSource(final BlockState cfj) {
        return true;
    }
    
    @Override
    public void animateTick(final BlockState cfj, final Level bqb, final BlockPos fu, final Random random) {
        if (!cfj.<Boolean>getValue((Property<Boolean>)RedstoneTorchBlock.LIT)) {
            return;
        }
        final double double6 = fu.getX() + 0.5 + (random.nextDouble() - 0.5) * 0.2;
        final double double7 = fu.getY() + 0.7 + (random.nextDouble() - 0.5) * 0.2;
        final double double8 = fu.getZ() + 0.5 + (random.nextDouble() - 0.5) * 0.2;
        bqb.addParticle(this.flameParticle, double6, double7, double8, 0.0, 0.0, 0.0);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(RedstoneTorchBlock.LIT);
    }
    
    private static boolean isToggledTooFrequently(final Level bqb, final BlockPos fu, final boolean boolean3) {
        final List<Toggle> list4 = RedstoneTorchBlock.RECENT_TOGGLES.computeIfAbsent(bqb, bpg -> Lists.newArrayList());
        if (boolean3) {
            list4.add(new Toggle(fu.immutable(), bqb.getGameTime()));
        }
        int integer5 = 0;
        for (int integer6 = 0; integer6 < list4.size(); ++integer6) {
            final Toggle a7 = list4.get(integer6);
            if (a7.pos.equals(fu) && ++integer5 >= 8) {
                return true;
            }
        }
        return false;
    }
    
    static {
        LIT = BlockStateProperties.LIT;
        RECENT_TOGGLES = new WeakHashMap<BlockGetter, List<Toggle>>();
    }
    
    public static class Toggle {
        private final BlockPos pos;
        private final long when;
        
        public Toggle(final BlockPos fu, final long long2) {
            this.pos = fu;
            this.when = long2;
        }
    }
}
