package net.minecraft.world.level.block;

import net.minecraft.world.level.block.state.AbstractStateHolder;
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 net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.DustParticleOptions;
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.LevelReader;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
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);
        this.registerDefaultState(((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).<Comparable, Boolean>setValue((Property<Comparable>)RedstoneTorchBlock.LIT, true));
    }
    
    @Override
    public int getTickDelay(final LevelReader bjz) {
        return 2;
    }
    
    @Override
    public void onPlace(final BlockState byj1, final Level bjw, final BlockPos fk, final BlockState byj4, final boolean boolean5) {
        for (final Direction fp10 : Direction.values()) {
            bjw.updateNeighborsAt(fk.relative(fp10), this);
        }
    }
    
    @Override
    public void onRemove(final BlockState byj1, final Level bjw, final BlockPos fk, final BlockState byj4, final boolean boolean5) {
        if (boolean5) {
            return;
        }
        for (final Direction fp10 : Direction.values()) {
            bjw.updateNeighborsAt(fk.relative(fp10), this);
        }
    }
    
    @Override
    public int getSignal(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final Direction fp) {
        if (byj.<Boolean>getValue((Property<Boolean>)RedstoneTorchBlock.LIT) && Direction.UP != fp) {
            return 15;
        }
        return 0;
    }
    
    protected boolean hasNeighborSignal(final Level bjw, final BlockPos fk, final BlockState byj) {
        return bjw.hasSignal(fk.below(), Direction.DOWN);
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        handleTick(byj, xd, fk, random, this.hasNeighborSignal(xd, fk, byj));
    }
    
    public static void handleTick(final BlockState byj, final Level bjw, final BlockPos fk, final Random random, final boolean boolean5) {
        final List<Toggle> list6 = RedstoneTorchBlock.RECENT_TOGGLES.get(bjw);
        while (list6 != null && !list6.isEmpty() && bjw.getGameTime() - list6.get(0).when > 60L) {
            list6.remove(0);
        }
        if (byj.<Boolean>getValue((Property<Boolean>)RedstoneTorchBlock.LIT)) {
            if (boolean5) {
                bjw.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)RedstoneTorchBlock.LIT, false), 3);
                if (isToggledTooFrequently(bjw, fk, true)) {
                    bjw.levelEvent(1502, fk, 0);
                    bjw.getBlockTicks().scheduleTick(fk, bjw.getBlockState(fk).getBlock(), 160);
                }
            }
        }
        else if (!boolean5 && !isToggledTooFrequently(bjw, fk, false)) {
            bjw.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)RedstoneTorchBlock.LIT, true), 3);
        }
    }
    
    @Override
    public void neighborChanged(final BlockState byj, final Level bjw, final BlockPos fk3, final Block bph, final BlockPos fk5, final boolean boolean6) {
        if (byj.<Boolean>getValue((Property<Boolean>)RedstoneTorchBlock.LIT) == this.hasNeighborSignal(bjw, fk3, byj) && !bjw.getBlockTicks().willTickThisTick(fk3, this)) {
            bjw.getBlockTicks().scheduleTick(fk3, this, this.getTickDelay(bjw));
        }
    }
    
    @Override
    public int getDirectSignal(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final Direction fp) {
        if (fp == Direction.DOWN) {
            return byj.getSignal(bjg, fk, fp);
        }
        return 0;
    }
    
    @Override
    public boolean isSignalSource(final BlockState byj) {
        return true;
    }
    
    @Override
    public void animateTick(final BlockState byj, final Level bjw, final BlockPos fk, final Random random) {
        if (!byj.<Boolean>getValue((Property<Boolean>)RedstoneTorchBlock.LIT)) {
            return;
        }
        final double double6 = fk.getX() + 0.5 + (random.nextDouble() - 0.5) * 0.2;
        final double double7 = fk.getY() + 0.7 + (random.nextDouble() - 0.5) * 0.2;
        final double double8 = fk.getZ() + 0.5 + (random.nextDouble() - 0.5) * 0.2;
        bjw.addParticle(DustParticleOptions.REDSTONE, double6, double7, double8, 0.0, 0.0, 0.0);
    }
    
    @Override
    public int getLightEmission(final BlockState byj) {
        return byj.<Boolean>getValue((Property<Boolean>)RedstoneTorchBlock.LIT) ? super.getLightEmission(byj) : 0;
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(RedstoneTorchBlock.LIT);
    }
    
    private static boolean isToggledTooFrequently(final Level bjw, final BlockPos fk, final boolean boolean3) {
        final List<Toggle> list4 = RedstoneTorchBlock.RECENT_TOGGLES.computeIfAbsent(bjw, bjg -> Lists.newArrayList());
        if (boolean3) {
            list4.add(new Toggle(fk.immutable(), bjw.getGameTime()));
        }
        int integer5 = 0;
        for (int integer6 = 0; integer6 < list4.size(); ++integer6) {
            final Toggle a7 = list4.get(integer6);
            if (a7.pos.equals(fk) && ++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 fk, final long long2) {
            this.pos = fk;
            this.when = long2;
        }
    }
}
