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.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.LevelReader;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
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((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)DetectorRailBlock.POWERED, false)).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_SOUTH));
    }
    
    @Override
    public int getTickDelay(final LevelReader bjz) {
        return 20;
    }
    
    @Override
    public boolean isSignalSource(final BlockState byj) {
        return true;
    }
    
    @Override
    public void entityInside(final BlockState byj, final Level bjw, final BlockPos fk, final Entity akq) {
        if (bjw.isClientSide) {
            return;
        }
        if (byj.<Boolean>getValue((Property<Boolean>)DetectorRailBlock.POWERED)) {
            return;
        }
        this.checkPressed(bjw, fk, byj);
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        if (!byj.<Boolean>getValue((Property<Boolean>)DetectorRailBlock.POWERED)) {
            return;
        }
        this.checkPressed(xd, fk, byj);
    }
    
    @Override
    public int getSignal(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final Direction fp) {
        return byj.<Boolean>getValue((Property<Boolean>)DetectorRailBlock.POWERED) ? 15 : 0;
    }
    
    @Override
    public int getDirectSignal(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final Direction fp) {
        if (!byj.<Boolean>getValue((Property<Boolean>)DetectorRailBlock.POWERED)) {
            return 0;
        }
        return (fp == Direction.UP) ? 15 : 0;
    }
    
    private void checkPressed(final Level bjw, final BlockPos fk, final BlockState byj) {
        final boolean boolean5 = byj.<Boolean>getValue((Property<Boolean>)DetectorRailBlock.POWERED);
        boolean boolean6 = false;
        final List<AbstractMinecart> list7 = this.<AbstractMinecart>getInteractingMinecartOfType(bjw, fk, AbstractMinecart.class, null);
        if (!list7.isEmpty()) {
            boolean6 = true;
        }
        if (boolean6 && !boolean5) {
            final BlockState byj2 = ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)DetectorRailBlock.POWERED, true);
            bjw.setBlock(fk, byj2, 3);
            this.updatePowerToConnected(bjw, fk, byj2, true);
            bjw.updateNeighborsAt(fk, this);
            bjw.updateNeighborsAt(fk.below(), this);
            bjw.setBlocksDirty(fk, byj, byj2);
        }
        if (!boolean6 && boolean5) {
            final BlockState byj2 = ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)DetectorRailBlock.POWERED, false);
            bjw.setBlock(fk, byj2, 3);
            this.updatePowerToConnected(bjw, fk, byj2, false);
            bjw.updateNeighborsAt(fk, this);
            bjw.updateNeighborsAt(fk.below(), this);
            bjw.setBlocksDirty(fk, byj, byj2);
        }
        if (boolean6) {
            bjw.getBlockTicks().scheduleTick(fk, this, this.getTickDelay(bjw));
        }
        bjw.updateNeighbourForOutputSignal(fk, this);
    }
    
    protected void updatePowerToConnected(final Level bjw, final BlockPos fk, final BlockState byj, final boolean boolean4) {
        final RailState btl6 = new RailState(bjw, fk, byj);
        final List<BlockPos> list7 = btl6.getConnections();
        for (final BlockPos fk2 : list7) {
            final BlockState byj2 = bjw.getBlockState(fk2);
            byj2.neighborChanged(bjw, fk2, byj2.getBlock(), fk, false);
        }
    }
    
    @Override
    public void onPlace(final BlockState byj1, final Level bjw, final BlockPos fk, final BlockState byj4, final boolean boolean5) {
        if (byj4.getBlock() == byj1.getBlock()) {
            return;
        }
        super.onPlace(byj1, bjw, fk, byj4, boolean5);
        this.checkPressed(bjw, fk, byj1);
    }
    
    @Override
    public Property<RailShape> getShapeProperty() {
        return DetectorRailBlock.SHAPE;
    }
    
    @Override
    public boolean hasAnalogOutputSignal(final BlockState byj) {
        return true;
    }
    
    @Override
    public int getAnalogOutputSignal(final BlockState byj, final Level bjw, final BlockPos fk) {
        if (byj.<Boolean>getValue((Property<Boolean>)DetectorRailBlock.POWERED)) {
            final List<MinecartCommandBlock> list5 = this.<MinecartCommandBlock>getInteractingMinecartOfType(bjw, fk, MinecartCommandBlock.class, null);
            if (!list5.isEmpty()) {
                return list5.get(0).getCommandBlock().getSuccessCount();
            }
            final List<AbstractMinecart> list6 = this.<AbstractMinecart>getInteractingMinecartOfType(bjw, fk, 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 bjw, final BlockPos fk, final Class<T> class3, @Nullable final Predicate<Entity> predicate) {
        return bjw.<T>getEntitiesOfClass(class3, this.getSearchBB(fk), predicate);
    }
    
    private AABB getSearchBB(final BlockPos fk) {
        final float float3 = 0.2f;
        return new AABB(fk.getX() + 0.2f, fk.getY(), fk.getZ() + 0.2f, fk.getX() + 1 - 0.2f, fk.getY() + 1 - 0.2f, fk.getZ() + 1 - 0.2f);
    }
    
    @Override
    public BlockState rotate(final BlockState byj, final Rotation btu) {
        Label_0424: {
            switch (btu) {
                case CLOCKWISE_180: {
                    switch (byj.<RailShape>getValue(DetectorRailBlock.SHAPE)) {
                        case ASCENDING_EAST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_WEST);
                        }
                        case ASCENDING_WEST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_EAST);
                        }
                        case ASCENDING_NORTH: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_SOUTH);
                        }
                        case ASCENDING_SOUTH: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_NORTH);
                        }
                        case SOUTH_EAST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_WEST);
                        }
                        case SOUTH_WEST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_EAST);
                        }
                        case NORTH_WEST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_EAST);
                        }
                        case NORTH_EAST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_WEST);
                        }
                        default: {
                            break Label_0424;
                        }
                    }
                    break;
                }
                case COUNTERCLOCKWISE_90: {
                    switch (byj.<RailShape>getValue(DetectorRailBlock.SHAPE)) {
                        case NORTH_SOUTH: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.EAST_WEST);
                        }
                        case EAST_WEST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_SOUTH);
                        }
                        case ASCENDING_EAST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_NORTH);
                        }
                        case ASCENDING_WEST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_SOUTH);
                        }
                        case ASCENDING_NORTH: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_WEST);
                        }
                        case ASCENDING_SOUTH: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_EAST);
                        }
                        case SOUTH_EAST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_EAST);
                        }
                        case SOUTH_WEST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_EAST);
                        }
                        case NORTH_WEST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_WEST);
                        }
                        case NORTH_EAST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_WEST);
                        }
                        default: {
                            break Label_0424;
                        }
                    }
                    break;
                }
                case CLOCKWISE_90: {
                    switch (byj.<RailShape>getValue(DetectorRailBlock.SHAPE)) {
                        case NORTH_SOUTH: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.EAST_WEST);
                        }
                        case EAST_WEST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_SOUTH);
                        }
                        case ASCENDING_EAST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_SOUTH);
                        }
                        case ASCENDING_WEST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_NORTH);
                        }
                        case ASCENDING_NORTH: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_EAST);
                        }
                        case ASCENDING_SOUTH: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_WEST);
                        }
                        case SOUTH_EAST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_WEST);
                        }
                        case SOUTH_WEST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_WEST);
                        }
                        case NORTH_WEST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_EAST);
                        }
                        case NORTH_EAST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_EAST);
                        }
                        default: {
                            break Label_0424;
                        }
                    }
                    break;
                }
            }
        }
        return byj;
    }
    
    @Override
    public BlockState mirror(final BlockState byj, final Mirror bsu) {
        final RailShape bzn4 = byj.<RailShape>getValue(DetectorRailBlock.SHAPE);
        Label_0319: {
            switch (bsu) {
                case LEFT_RIGHT: {
                    switch (bzn4) {
                        case ASCENDING_NORTH: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_SOUTH);
                        }
                        case ASCENDING_SOUTH: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_NORTH);
                        }
                        case SOUTH_EAST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_EAST);
                        }
                        case SOUTH_WEST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_WEST);
                        }
                        case NORTH_WEST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_WEST);
                        }
                        case NORTH_EAST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_EAST);
                        }
                        default: {
                            break Label_0319;
                        }
                    }
                    break;
                }
                case FRONT_BACK: {
                    switch (bzn4) {
                        case ASCENDING_EAST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_WEST);
                        }
                        case ASCENDING_WEST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.ASCENDING_EAST);
                        }
                        case SOUTH_EAST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_WEST);
                        }
                        case SOUTH_WEST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.SOUTH_EAST);
                        }
                        case NORTH_WEST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_EAST);
                        }
                        case NORTH_EAST: {
                            return ((AbstractStateHolder<O, BlockState>)byj).<RailShape, RailShape>setValue(DetectorRailBlock.SHAPE, RailShape.NORTH_WEST);
                        }
                        default: {
                            break Label_0319;
                        }
                    }
                    break;
                }
            }
        }
        return super.mirror(byj, bsu);
    }
    
    @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;
    }
}
