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.level.block.state.StateDefinition;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.DustParticleOptions;
import java.util.Random;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.IntegerProperty;
import net.minecraft.world.level.block.state.properties.BooleanProperty;

public class RepeaterBlock extends DiodeBlock {
    public static final BooleanProperty LOCKED;
    public static final IntegerProperty DELAY;
    
    protected RepeaterBlock(final Properties c) {
        super(c);
        this.registerDefaultState((((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)RepeaterBlock.FACING, Direction.NORTH)).setValue((Property<Comparable>)RepeaterBlock.DELAY, 1)).setValue((Property<Comparable>)RepeaterBlock.LOCKED, false)).<Comparable, Boolean>setValue((Property<Comparable>)RepeaterBlock.POWERED, false));
    }
    
    @Override
    public InteractionResult use(final BlockState byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        if (!ayj.abilities.mayBuild) {
            return InteractionResult.PASS;
        }
        bjw.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable>cycle((Property<Comparable>)RepeaterBlock.DELAY), 3);
        return InteractionResult.SUCCESS;
    }
    
    @Override
    protected int getDelay(final BlockState byj) {
        return byj.<Integer>getValue((Property<Integer>)RepeaterBlock.DELAY) * 2;
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        final BlockState byj3 = super.getStateForPlacement(bcq);
        return ((AbstractStateHolder<O, BlockState>)byj3).<Comparable, Boolean>setValue((Property<Comparable>)RepeaterBlock.LOCKED, this.isLocked(bcq.getLevel(), bcq.getClickedPos(), byj3));
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (!bjx.isClientSide() && fp.getAxis() != byj1.<Direction>getValue((Property<Direction>)RepeaterBlock.FACING).getAxis()) {
            return ((AbstractStateHolder<O, BlockState>)byj1).<Comparable, Boolean>setValue((Property<Comparable>)RepeaterBlock.LOCKED, this.isLocked(bjx, fk5, byj1));
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Override
    public boolean isLocked(final LevelReader bjz, final BlockPos fk, final BlockState byj) {
        return this.getAlternateSignal(bjz, fk, byj) > 0;
    }
    
    @Override
    protected boolean isAlternateInput(final BlockState byj) {
        return DiodeBlock.isDiode(byj);
    }
    
    @Override
    public void animateTick(final BlockState byj, final Level bjw, final BlockPos fk, final Random random) {
        if (!byj.<Boolean>getValue((Property<Boolean>)RepeaterBlock.POWERED)) {
            return;
        }
        final Direction fp6 = byj.<Direction>getValue((Property<Direction>)RepeaterBlock.FACING);
        final double double7 = fk.getX() + 0.5f + (random.nextFloat() - 0.5f) * 0.2;
        final double double8 = fk.getY() + 0.4f + (random.nextFloat() - 0.5f) * 0.2;
        final double double9 = fk.getZ() + 0.5f + (random.nextFloat() - 0.5f) * 0.2;
        float float13 = -5.0f;
        if (random.nextBoolean()) {
            float13 = (float)(byj.<Integer>getValue((Property<Integer>)RepeaterBlock.DELAY) * 2 - 1);
        }
        float13 /= 16.0f;
        final double double10 = float13 * fp6.getStepX();
        final double double11 = float13 * fp6.getStepZ();
        bjw.addParticle(DustParticleOptions.REDSTONE, double7 + double10, double8, double9 + double11, 0.0, 0.0, 0.0);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(RepeaterBlock.FACING, RepeaterBlock.DELAY, RepeaterBlock.LOCKED, RepeaterBlock.POWERED);
    }
    
    static {
        LOCKED = BlockStateProperties.LOCKED;
        DELAY = BlockStateProperties.DELAY;
    }
}
