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.entity.LivingEntity;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.core.Direction;
import net.minecraft.world.level.TickPriority;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.properties.Property;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.phys.shapes.VoxelShape;

public abstract class DiodeBlock extends HorizontalDirectionalBlock {
    protected static final VoxelShape SHAPE;
    public static final BooleanProperty POWERED;
    
    protected DiodeBlock(final Properties c) {
        super(c);
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        return DiodeBlock.SHAPE;
    }
    
    @Override
    public boolean canSurvive(final BlockState cfj, final LevelReader bqd, final BlockPos fu) {
        return Block.canSupportRigidBlock(bqd, fu.below());
    }
    
    @Override
    public void tick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        if (this.isLocked(zd, fu, cfj)) {
            return;
        }
        final boolean boolean6 = cfj.<Boolean>getValue((Property<Boolean>)DiodeBlock.POWERED);
        final boolean boolean7 = this.shouldTurnOn(zd, fu, cfj);
        if (boolean6 && !boolean7) {
            zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)DiodeBlock.POWERED, false), 2);
        }
        else if (!boolean6) {
            zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)DiodeBlock.POWERED, true), 2);
            if (!boolean7) {
                zd.getBlockTicks().scheduleTick(fu, this, this.getDelay(cfj), TickPriority.VERY_HIGH);
            }
        }
    }
    
    @Override
    public int getDirectSignal(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final Direction fz) {
        return cfj.getSignal(bpg, fu, fz);
    }
    
    @Override
    public int getSignal(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final Direction fz) {
        if (!cfj.<Boolean>getValue((Property<Boolean>)DiodeBlock.POWERED)) {
            return 0;
        }
        if (cfj.<Comparable>getValue((Property<Comparable>)DiodeBlock.FACING) == fz) {
            return this.getOutputSignal(bpg, fu, cfj);
        }
        return 0;
    }
    
    @Override
    public void neighborChanged(final BlockState cfj, final Level bqb, final BlockPos fu3, final Block bvr, final BlockPos fu5, final boolean boolean6) {
        if (cfj.canSurvive(bqb, fu3)) {
            this.checkTickOnNeighbor(bqb, fu3, cfj);
            return;
        }
        final BlockEntity cdl8 = this.isEntityBlock() ? bqb.getBlockEntity(fu3) : null;
        Block.dropResources(cfj, bqb, fu3, cdl8);
        bqb.removeBlock(fu3, false);
        for (final Direction fz12 : Direction.values()) {
            bqb.updateNeighborsAt(fu3.relative(fz12), this);
        }
    }
    
    protected void checkTickOnNeighbor(final Level bqb, final BlockPos fu, final BlockState cfj) {
        if (this.isLocked(bqb, fu, cfj)) {
            return;
        }
        final boolean boolean5 = cfj.<Boolean>getValue((Property<Boolean>)DiodeBlock.POWERED);
        final boolean boolean6 = this.shouldTurnOn(bqb, fu, cfj);
        if (boolean5 != boolean6 && !bqb.getBlockTicks().willTickThisTick(fu, this)) {
            TickPriority bqt7 = TickPriority.HIGH;
            if (this.shouldPrioritize(bqb, fu, cfj)) {
                bqt7 = TickPriority.EXTREMELY_HIGH;
            }
            else if (boolean5) {
                bqt7 = TickPriority.VERY_HIGH;
            }
            bqb.getBlockTicks().scheduleTick(fu, this, this.getDelay(cfj), bqt7);
        }
    }
    
    public boolean isLocked(final LevelReader bqd, final BlockPos fu, final BlockState cfj) {
        return false;
    }
    
    protected boolean shouldTurnOn(final Level bqb, final BlockPos fu, final BlockState cfj) {
        return this.getInputSignal(bqb, fu, cfj) > 0;
    }
    
    protected int getInputSignal(final Level bqb, final BlockPos fu, final BlockState cfj) {
        final Direction fz5 = cfj.<Direction>getValue((Property<Direction>)DiodeBlock.FACING);
        final BlockPos fu2 = fu.relative(fz5);
        final int integer7 = bqb.getSignal(fu2, fz5);
        if (integer7 >= 15) {
            return integer7;
        }
        final BlockState cfj2 = bqb.getBlockState(fu2);
        return Math.max(integer7, cfj2.is(Blocks.REDSTONE_WIRE) ? ((int)cfj2.<Integer>getValue((Property<Integer>)RedStoneWireBlock.POWER)) : 0);
    }
    
    protected int getAlternateSignal(final LevelReader bqd, final BlockPos fu, final BlockState cfj) {
        final Direction fz5 = cfj.<Direction>getValue((Property<Direction>)DiodeBlock.FACING);
        final Direction fz6 = fz5.getClockWise();
        final Direction fz7 = fz5.getCounterClockWise();
        return Math.max(this.getAlternateSignalAt(bqd, fu.relative(fz6), fz6), this.getAlternateSignalAt(bqd, fu.relative(fz7), fz7));
    }
    
    protected int getAlternateSignalAt(final LevelReader bqd, final BlockPos fu, final Direction fz) {
        final BlockState cfj5 = bqd.getBlockState(fu);
        if (!this.isAlternateInput(cfj5)) {
            return 0;
        }
        if (cfj5.is(Blocks.REDSTONE_BLOCK)) {
            return 15;
        }
        if (cfj5.is(Blocks.REDSTONE_WIRE)) {
            return cfj5.<Integer>getValue((Property<Integer>)RedStoneWireBlock.POWER);
        }
        return bqd.getDirectSignal(fu, fz);
    }
    
    @Override
    public boolean isSignalSource(final BlockState cfj) {
        return true;
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        return ((StateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)DiodeBlock.FACING, bin.getHorizontalDirection().getOpposite());
    }
    
    @Override
    public void setPlacedBy(final Level bqb, final BlockPos fu, final BlockState cfj, final LivingEntity aoy, final ItemStack bki) {
        if (this.shouldTurnOn(bqb, fu, cfj)) {
            bqb.getBlockTicks().scheduleTick(fu, this, 1);
        }
    }
    
    @Override
    public void onPlace(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        this.updateNeighborsInFront(bqb, fu, cfj1);
    }
    
    @Override
    public void onRemove(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        if (boolean5 || cfj1.is(cfj4.getBlock())) {
            return;
        }
        super.onRemove(cfj1, bqb, fu, cfj4, boolean5);
        this.updateNeighborsInFront(bqb, fu, cfj1);
    }
    
    protected void updateNeighborsInFront(final Level bqb, final BlockPos fu, final BlockState cfj) {
        final Direction fz5 = cfj.<Direction>getValue((Property<Direction>)DiodeBlock.FACING);
        final BlockPos fu2 = fu.relative(fz5.getOpposite());
        bqb.neighborChanged(fu2, this, fu);
        bqb.updateNeighborsAtExceptFromFacing(fu2, this, fz5);
    }
    
    protected boolean isAlternateInput(final BlockState cfj) {
        return cfj.isSignalSource();
    }
    
    protected int getOutputSignal(final BlockGetter bpg, final BlockPos fu, final BlockState cfj) {
        return 15;
    }
    
    public static boolean isDiode(final BlockState cfj) {
        return cfj.getBlock() instanceof DiodeBlock;
    }
    
    public boolean shouldPrioritize(final BlockGetter bpg, final BlockPos fu, final BlockState cfj) {
        final Direction fz5 = cfj.<Direction>getValue((Property<Direction>)DiodeBlock.FACING).getOpposite();
        final BlockState cfj2 = bpg.getBlockState(fu.relative(fz5));
        return isDiode(cfj2) && cfj2.<Comparable>getValue((Property<Comparable>)DiodeBlock.FACING) != fz5;
    }
    
    protected abstract int getDelay(final BlockState cfj);
    
    static {
        SHAPE = Block.box(0.0, 0.0, 0.0, 16.0, 2.0, 16.0);
        POWERED = BlockStateProperties.POWERED;
    }
}
