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.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.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 byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return DiodeBlock.SHAPE;
    }
    
    @Override
    public boolean canSurvive(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        return Block.canSupportRigidBlock(bjz, fk.below());
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        if (this.isLocked(xd, fk, byj)) {
            return;
        }
        final boolean boolean6 = byj.<Boolean>getValue((Property<Boolean>)DiodeBlock.POWERED);
        final boolean boolean7 = this.shouldTurnOn(xd, fk, byj);
        if (boolean6 && !boolean7) {
            xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)DiodeBlock.POWERED, false), 2);
        }
        else if (!boolean6) {
            xd.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)DiodeBlock.POWERED, true), 2);
            if (!boolean7) {
                xd.getBlockTicks().scheduleTick(fk, this, this.getDelay(byj), TickPriority.VERY_HIGH);
            }
        }
    }
    
    @Override
    public int getDirectSignal(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final Direction fp) {
        return byj.getSignal(bjg, fk, fp);
    }
    
    @Override
    public int getSignal(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final Direction fp) {
        if (!byj.<Boolean>getValue((Property<Boolean>)DiodeBlock.POWERED)) {
            return 0;
        }
        if (byj.<Comparable>getValue((Property<Comparable>)DiodeBlock.FACING) == fp) {
            return this.getOutputSignal(bjg, fk, byj);
        }
        return 0;
    }
    
    @Override
    public void neighborChanged(final BlockState byj, final Level bjw, final BlockPos fk3, final Block bph, final BlockPos fk5, final boolean boolean6) {
        if (byj.canSurvive(bjw, fk3)) {
            this.checkTickOnNeighbor(bjw, fk3, byj);
            return;
        }
        final BlockEntity bwl8 = this.isEntityBlock() ? bjw.getBlockEntity(fk3) : null;
        Block.dropResources(byj, bjw, fk3, bwl8);
        bjw.removeBlock(fk3, false);
        for (final Direction fp12 : Direction.values()) {
            bjw.updateNeighborsAt(fk3.relative(fp12), this);
        }
    }
    
    protected void checkTickOnNeighbor(final Level bjw, final BlockPos fk, final BlockState byj) {
        if (this.isLocked(bjw, fk, byj)) {
            return;
        }
        final boolean boolean5 = byj.<Boolean>getValue((Property<Boolean>)DiodeBlock.POWERED);
        final boolean boolean6 = this.shouldTurnOn(bjw, fk, byj);
        if (boolean5 != boolean6 && !bjw.getBlockTicks().willTickThisTick(fk, this)) {
            TickPriority bkn7 = TickPriority.HIGH;
            if (this.shouldPrioritize(bjw, fk, byj)) {
                bkn7 = TickPriority.EXTREMELY_HIGH;
            }
            else if (boolean5) {
                bkn7 = TickPriority.VERY_HIGH;
            }
            bjw.getBlockTicks().scheduleTick(fk, this, this.getDelay(byj), bkn7);
        }
    }
    
    public boolean isLocked(final LevelReader bjz, final BlockPos fk, final BlockState byj) {
        return false;
    }
    
    protected boolean shouldTurnOn(final Level bjw, final BlockPos fk, final BlockState byj) {
        return this.getInputSignal(bjw, fk, byj) > 0;
    }
    
    protected int getInputSignal(final Level bjw, final BlockPos fk, final BlockState byj) {
        final Direction fp5 = byj.<Direction>getValue((Property<Direction>)DiodeBlock.FACING);
        final BlockPos fk2 = fk.relative(fp5);
        final int integer7 = bjw.getSignal(fk2, fp5);
        if (integer7 >= 15) {
            return integer7;
        }
        final BlockState byj2 = bjw.getBlockState(fk2);
        return Math.max(integer7, (byj2.getBlock() == Blocks.REDSTONE_WIRE) ? ((int)byj2.<Integer>getValue((Property<Integer>)RedStoneWireBlock.POWER)) : 0);
    }
    
    protected int getAlternateSignal(final LevelReader bjz, final BlockPos fk, final BlockState byj) {
        final Direction fp5 = byj.<Direction>getValue((Property<Direction>)DiodeBlock.FACING);
        final Direction fp6 = fp5.getClockWise();
        final Direction fp7 = fp5.getCounterClockWise();
        return Math.max(this.getAlternateSignalAt(bjz, fk.relative(fp6), fp6), this.getAlternateSignalAt(bjz, fk.relative(fp7), fp7));
    }
    
    protected int getAlternateSignalAt(final LevelReader bjz, final BlockPos fk, final Direction fp) {
        final BlockState byj5 = bjz.getBlockState(fk);
        final Block bph6 = byj5.getBlock();
        if (!this.isAlternateInput(byj5)) {
            return 0;
        }
        if (bph6 == Blocks.REDSTONE_BLOCK) {
            return 15;
        }
        if (bph6 == Blocks.REDSTONE_WIRE) {
            return byj5.<Integer>getValue((Property<Integer>)RedStoneWireBlock.POWER);
        }
        return bjz.getDirectSignal(fk, fp);
    }
    
    @Override
    public boolean isSignalSource(final BlockState byj) {
        return true;
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        return ((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).<Comparable, Direction>setValue((Property<Comparable>)DiodeBlock.FACING, bcq.getHorizontalDirection().getOpposite());
    }
    
    @Override
    public void setPlacedBy(final Level bjw, final BlockPos fk, final BlockState byj, final LivingEntity akz, final ItemStack ben) {
        if (this.shouldTurnOn(bjw, fk, byj)) {
            bjw.getBlockTicks().scheduleTick(fk, this, 1);
        }
    }
    
    @Override
    public void onPlace(final BlockState byj1, final Level bjw, final BlockPos fk, final BlockState byj4, final boolean boolean5) {
        this.updateNeighborsInFront(bjw, fk, byj1);
    }
    
    @Override
    public void onRemove(final BlockState byj1, final Level bjw, final BlockPos fk, final BlockState byj4, final boolean boolean5) {
        if (boolean5 || byj1.getBlock() == byj4.getBlock()) {
            return;
        }
        super.onRemove(byj1, bjw, fk, byj4, boolean5);
        this.updateNeighborsInFront(bjw, fk, byj1);
    }
    
    protected void updateNeighborsInFront(final Level bjw, final BlockPos fk, final BlockState byj) {
        final Direction fp5 = byj.<Direction>getValue((Property<Direction>)DiodeBlock.FACING);
        final BlockPos fk2 = fk.relative(fp5.getOpposite());
        bjw.neighborChanged(fk2, this, fk);
        bjw.updateNeighborsAtExceptFromFacing(fk2, this, fp5);
    }
    
    protected boolean isAlternateInput(final BlockState byj) {
        return byj.isSignalSource();
    }
    
    protected int getOutputSignal(final BlockGetter bjg, final BlockPos fk, final BlockState byj) {
        return 15;
    }
    
    public static boolean isDiode(final BlockState byj) {
        return byj.getBlock() instanceof DiodeBlock;
    }
    
    public boolean shouldPrioritize(final BlockGetter bjg, final BlockPos fk, final BlockState byj) {
        final Direction fp5 = byj.<Direction>getValue((Property<Direction>)DiodeBlock.FACING).getOpposite();
        final BlockState byj2 = bjg.getBlockState(fk.relative(fp5));
        return isDiode(byj2) && byj2.<Comparable>getValue((Property<Comparable>)DiodeBlock.FACING) != fp5;
    }
    
    protected abstract int getDelay(final BlockState byj);
    
    static {
        SHAPE = Block.box(0.0, 0.0, 0.0, 16.0, 2.0, 16.0);
        POWERED = BlockStateProperties.POWERED;
    }
}
