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.level.block.state.StateDefinition;
import net.minecraft.world.phys.AABB;
import javax.annotation.Nullable;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.Container;
import net.minecraft.world.entity.EntitySelector;
import net.minecraft.world.entity.vehicle.MinecartCommandBlock;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;
import net.minecraft.world.entity.vehicle.AbstractMinecart;
import net.minecraft.core.Direction;
import net.minecraft.world.level.BlockGetter;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
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.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.RailShape;
import net.minecraft.world.level.block.state.properties.EnumProperty;

public class DetectorRailBlock extends BaseRailBlock {
    public static final EnumProperty<RailShape> SHAPE;
    public static final BooleanProperty POWERED;
    
    public DetectorRailBlock(final Properties c) {
        super(true, c);
        this.registerDefaultState((((StateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)DetectorRailBlock.POWERED, false)).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_SOUTH));
    }
    
    @Override
    public boolean isSignalSource(final BlockState cfj) {
        return true;
    }
    
    @Override
    public void entityInside(final BlockState cfj, final Level bqb, final BlockPos fu, final Entity aom) {
        if (bqb.isClientSide) {
            return;
        }
        if (cfj.<Boolean>getValue((Property<Boolean>)DetectorRailBlock.POWERED)) {
            return;
        }
        this.checkPressed(bqb, fu, cfj);
    }
    
    @Override
    public void tick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        if (!cfj.<Boolean>getValue((Property<Boolean>)DetectorRailBlock.POWERED)) {
            return;
        }
        this.checkPressed(zd, fu, cfj);
    }
    
    @Override
    public int getSignal(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final Direction fz) {
        return cfj.<Boolean>getValue((Property<Boolean>)DetectorRailBlock.POWERED) ? 15 : 0;
    }
    
    @Override
    public int getDirectSignal(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final Direction fz) {
        if (!cfj.<Boolean>getValue((Property<Boolean>)DetectorRailBlock.POWERED)) {
            return 0;
        }
        return (fz == Direction.UP) ? 15 : 0;
    }
    
    private void checkPressed(final Level bqb, final BlockPos fu, final BlockState cfj) {
        final boolean boolean5 = cfj.<Boolean>getValue((Property<Boolean>)DetectorRailBlock.POWERED);
        boolean boolean6 = false;
        final List<AbstractMinecart> list7 = this.<AbstractMinecart>getInteractingMinecartOfType(bqb, fu, AbstractMinecart.class, null);
        if (!list7.isEmpty()) {
            boolean6 = true;
        }
        if (boolean6 && !boolean5) {
            final BlockState cfj2 = ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)DetectorRailBlock.POWERED, true);
            bqb.setBlock(fu, cfj2, 3);
            this.updatePowerToConnected(bqb, fu, cfj2, true);
            bqb.updateNeighborsAt(fu, this);
            bqb.updateNeighborsAt(fu.below(), this);
            bqb.setBlocksDirty(fu, cfj, cfj2);
        }
        if (!boolean6 && boolean5) {
            final BlockState cfj2 = ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)DetectorRailBlock.POWERED, false);
            bqb.setBlock(fu, cfj2, 3);
            this.updatePowerToConnected(bqb, fu, cfj2, false);
            bqb.updateNeighborsAt(fu, this);
            bqb.updateNeighborsAt(fu.below(), this);
            bqb.setBlocksDirty(fu, cfj, cfj2);
        }
        if (boolean6) {
            bqb.getBlockTicks().scheduleTick(fu, this, 20);
        }
        bqb.updateNeighbourForOutputSignal(fu, this);
    }
    
    protected void updatePowerToConnected(final Level bqb, final BlockPos fu, final BlockState cfj, final boolean boolean4) {
        final RailState cae6 = new RailState(bqb, fu, cfj);
        final List<BlockPos> list7 = cae6.getConnections();
        for (final BlockPos fu2 : list7) {
            final BlockState cfj2 = bqb.getBlockState(fu2);
            cfj2.neighborChanged(bqb, fu2, cfj2.getBlock(), fu, false);
        }
    }
    
    @Override
    public void onPlace(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        if (cfj4.is(cfj1.getBlock())) {
            return;
        }
        this.checkPressed(bqb, fu, this.updateState(cfj1, bqb, fu, boolean5));
    }
    
    @Override
    public Property<RailShape> getShapeProperty() {
        return DetectorRailBlock.SHAPE;
    }
    
    @Override
    public boolean hasAnalogOutputSignal(final BlockState cfj) {
        return true;
    }
    
    @Override
    public int getAnalogOutputSignal(final BlockState cfj, final Level bqb, final BlockPos fu) {
        if (cfj.<Boolean>getValue((Property<Boolean>)DetectorRailBlock.POWERED)) {
            final List<MinecartCommandBlock> list5 = this.<MinecartCommandBlock>getInteractingMinecartOfType(bqb, fu, MinecartCommandBlock.class, null);
            if (!list5.isEmpty()) {
                return list5.get(0).getCommandBlock().getSuccessCount();
            }
            final List<AbstractMinecart> list6 = this.<AbstractMinecart>getInteractingMinecartOfType(bqb, fu, AbstractMinecart.class, EntitySelector.CONTAINER_ENTITY_SELECTOR);
            if (!list6.isEmpty()) {
                return AbstractContainerMenu.getRedstoneSignalFromContainer((Container)list6.get(0));
            }
        }
        return 0;
    }
    
    protected <T extends AbstractMinecart> List<T> getInteractingMinecartOfType(final Level bqb, final BlockPos fu, final Class<T> class3, @Nullable final Predicate<Entity> predicate) {
        return bqb.<T>getEntitiesOfClass(class3, this.getSearchBB(fu), predicate);
    }
    
    private AABB getSearchBB(final BlockPos fu) {
        final double double3 = 0.2;
        return new AABB(fu.getX() + 0.2, fu.getY(), fu.getZ() + 0.2, fu.getX() + 1 - 0.2, fu.getY() + 1 - 0.2, fu.getZ() + 1 - 0.2);
    }
    
    @Override
    public BlockState rotate(final BlockState cfj, final Rotation cap) {
        Label_0424: {
            switch (cap) {
                case CLOCKWISE_180: {
                    switch (cfj.<RailShape>getValue(DetectorRailBlock.SHAPE)) {
                        case ASCENDING_EAST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_WEST);
                        }
                        case ASCENDING_WEST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_EAST);
                        }
                        case ASCENDING_NORTH: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_SOUTH);
                        }
                        case ASCENDING_SOUTH: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_NORTH);
                        }
                        case SOUTH_EAST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_WEST);
                        }
                        case SOUTH_WEST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_EAST);
                        }
                        case NORTH_WEST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_EAST);
                        }
                        case NORTH_EAST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_WEST);
                        }
                        default: {
                            break Label_0424;
                        }
                    }
                    break;
                }
                case COUNTERCLOCKWISE_90: {
                    switch (cfj.<RailShape>getValue(DetectorRailBlock.SHAPE)) {
                        case NORTH_SOUTH: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.EAST_WEST);
                        }
                        case EAST_WEST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_SOUTH);
                        }
                        case ASCENDING_EAST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_NORTH);
                        }
                        case ASCENDING_WEST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_SOUTH);
                        }
                        case ASCENDING_NORTH: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_WEST);
                        }
                        case ASCENDING_SOUTH: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_EAST);
                        }
                        case SOUTH_EAST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_EAST);
                        }
                        case SOUTH_WEST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_EAST);
                        }
                        case NORTH_WEST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_WEST);
                        }
                        case NORTH_EAST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_WEST);
                        }
                        default: {
                            break Label_0424;
                        }
                    }
                    break;
                }
                case CLOCKWISE_90: {
                    switch (cfj.<RailShape>getValue(DetectorRailBlock.SHAPE)) {
                        case NORTH_SOUTH: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.EAST_WEST);
                        }
                        case EAST_WEST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_SOUTH);
                        }
                        case ASCENDING_EAST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_SOUTH);
                        }
                        case ASCENDING_WEST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_NORTH);
                        }
                        case ASCENDING_NORTH: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_EAST);
                        }
                        case ASCENDING_SOUTH: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_WEST);
                        }
                        case SOUTH_EAST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_WEST);
                        }
                        case SOUTH_WEST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_WEST);
                        }
                        case NORTH_WEST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_EAST);
                        }
                        case NORTH_EAST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_EAST);
                        }
                        default: {
                            break Label_0424;
                        }
                    }
                    break;
                }
            }
        }
        return cfj;
    }
    
    @Override
    public BlockState mirror(final BlockState cfj, final Mirror bzj) {
        final RailShape cgm4 = cfj.<RailShape>getValue(DetectorRailBlock.SHAPE);
        Label_0319: {
            switch (bzj) {
                case LEFT_RIGHT: {
                    switch (cgm4) {
                        case ASCENDING_NORTH: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_SOUTH);
                        }
                        case ASCENDING_SOUTH: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_NORTH);
                        }
                        case SOUTH_EAST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_EAST);
                        }
                        case SOUTH_WEST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_WEST);
                        }
                        case NORTH_WEST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_WEST);
                        }
                        case NORTH_EAST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_EAST);
                        }
                        default: {
                            break Label_0319;
                        }
                    }
                    break;
                }
                case FRONT_BACK: {
                    switch (cgm4) {
                        case ASCENDING_EAST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_WEST);
                        }
                        case ASCENDING_WEST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_EAST);
                        }
                        case SOUTH_EAST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_WEST);
                        }
                        case SOUTH_WEST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_EAST);
                        }
                        case NORTH_WEST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_EAST);
                        }
                        case NORTH_EAST: {
                            return ((StateHolder<O, BlockState>)cfj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_WEST);
                        }
                        default: {
                            break Label_0319;
                        }
                    }
                    break;
                }
            }
        }
        return super.mirror(cfj, bzj);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(DetectorRailBlock.SHAPE, DetectorRailBlock.POWERED);
    }
    
    static {
        SHAPE = BlockStateProperties.RAIL_SHAPE_STRAIGHT;
        POWERED = BlockStateProperties.POWERED;
    }
}
