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 java.util.Iterator;
import java.util.List;
import net.minecraft.world.level.LevelReader;
import net.minecraft.core.Vec3i;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.item.Items;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.item.BlockPlaceContext;
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.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.core.Direction;
import java.util.Map;
import net.minecraft.world.level.block.state.properties.BooleanProperty;

public class TripWireBlock extends Block {
    public static final BooleanProperty POWERED;
    public static final BooleanProperty ATTACHED;
    public static final BooleanProperty DISARMED;
    public static final BooleanProperty NORTH;
    public static final BooleanProperty EAST;
    public static final BooleanProperty SOUTH;
    public static final BooleanProperty WEST;
    private static final Map<Direction, BooleanProperty> PROPERTY_BY_DIRECTION;
    protected static final VoxelShape AABB;
    protected static final VoxelShape NOT_ATTACHED_AABB;
    private final TripWireHookBlock hook;
    
    public TripWireBlock(final TripWireHookBlock bvk, final Properties c) {
        super(c);
        this.registerDefaultState(((((((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)TripWireBlock.POWERED, false)).setValue((Property<Comparable>)TripWireBlock.ATTACHED, false)).setValue((Property<Comparable>)TripWireBlock.DISARMED, false)).setValue((Property<Comparable>)TripWireBlock.NORTH, false)).setValue((Property<Comparable>)TripWireBlock.EAST, false)).setValue((Property<Comparable>)TripWireBlock.SOUTH, false)).<Comparable, Boolean>setValue((Property<Comparable>)TripWireBlock.WEST, false));
        this.hook = bvk;
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return byj.<Boolean>getValue((Property<Boolean>)TripWireBlock.ATTACHED) ? TripWireBlock.AABB : TripWireBlock.NOT_ATTACHED_AABB;
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        final BlockGetter bjg3 = bcq.getLevel();
        final BlockPos fk4 = bcq.getClickedPos();
        return (((((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)TripWireBlock.NORTH, this.shouldConnectTo(bjg3.getBlockState(fk4.north()), Direction.NORTH))).setValue((Property<Comparable>)TripWireBlock.EAST, this.shouldConnectTo(bjg3.getBlockState(fk4.east()), Direction.EAST))).setValue((Property<Comparable>)TripWireBlock.SOUTH, this.shouldConnectTo(bjg3.getBlockState(fk4.south()), Direction.SOUTH))).<Comparable, Boolean>setValue((Property<Comparable>)TripWireBlock.WEST, this.shouldConnectTo(bjg3.getBlockState(fk4.west()), Direction.WEST));
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (fp.getAxis().isHorizontal()) {
            return ((AbstractStateHolder<O, BlockState>)byj1).<Comparable, Boolean>setValue((Property<Comparable>)TripWireBlock.PROPERTY_BY_DIRECTION.get(fp), this.shouldConnectTo(byj3, fp));
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @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;
        }
        this.updateSource(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;
        }
        this.updateSource(bjw, fk, ((AbstractStateHolder<O, BlockState>)byj1).<Comparable, Boolean>setValue((Property<Comparable>)TripWireBlock.POWERED, true));
    }
    
    @Override
    public void playerWillDestroy(final Level bjw, final BlockPos fk, final BlockState byj, final Player ayj) {
        if (!bjw.isClientSide && !ayj.getMainHandItem().isEmpty() && ayj.getMainHandItem().getItem() == Items.SHEARS) {
            bjw.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)TripWireBlock.DISARMED, true), 4);
        }
        super.playerWillDestroy(bjw, fk, byj, ayj);
    }
    
    private void updateSource(final Level bjw, final BlockPos fk, final BlockState byj) {
        for (final Direction fp8 : new Direction[] { Direction.SOUTH, Direction.WEST }) {
            int integer9 = 1;
            while (integer9 < 42) {
                final BlockPos fk2 = fk.relative(fp8, integer9);
                final BlockState byj2 = bjw.getBlockState(fk2);
                if (byj2.getBlock() == this.hook) {
                    if (byj2.<Comparable>getValue((Property<Comparable>)TripWireHookBlock.FACING) == fp8.getOpposite()) {
                        this.hook.calculateState(bjw, fk2, byj2, false, true, integer9, byj);
                        break;
                    }
                    break;
                }
                else {
                    if (byj2.getBlock() != this) {
                        break;
                    }
                    ++integer9;
                }
            }
        }
    }
    
    @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>)TripWireBlock.POWERED)) {
            return;
        }
        this.checkPressed(bjw, fk);
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        if (!xd.getBlockState(fk).<Boolean>getValue((Property<Boolean>)TripWireBlock.POWERED)) {
            return;
        }
        this.checkPressed(xd, fk);
    }
    
    private void checkPressed(final Level bjw, final BlockPos fk) {
        BlockState byj4 = bjw.getBlockState(fk);
        final boolean boolean5 = byj4.<Boolean>getValue((Property<Boolean>)TripWireBlock.POWERED);
        boolean boolean6 = false;
        final List<? extends Entity> list7 = bjw.getEntities(null, byj4.getShape(bjw, fk).bounds().move(fk));
        if (!list7.isEmpty()) {
            for (final Entity akq9 : list7) {
                if (!akq9.isIgnoringBlockTriggers()) {
                    boolean6 = true;
                    break;
                }
            }
        }
        if (boolean6 != boolean5) {
            byj4 = ((AbstractStateHolder<O, BlockState>)byj4).<Comparable, Boolean>setValue((Property<Comparable>)TripWireBlock.POWERED, boolean6);
            bjw.setBlock(fk, byj4, 3);
            this.updateSource(bjw, fk, byj4);
        }
        if (boolean6) {
            bjw.getBlockTicks().scheduleTick(new BlockPos(fk), this, this.getTickDelay(bjw));
        }
    }
    
    public boolean shouldConnectTo(final BlockState byj, final Direction fp) {
        final Block bph4 = byj.getBlock();
        if (bph4 == this.hook) {
            return byj.<Comparable>getValue((Property<Comparable>)TripWireHookBlock.FACING) == fp.getOpposite();
        }
        return bph4 == this;
    }
    
    @Override
    public BlockState rotate(final BlockState byj, final Rotation btu) {
        switch (btu) {
            case CLOCKWISE_180: {
                return (((((AbstractStateHolder<O, BlockState>)byj).setValue((Property<Comparable>)TripWireBlock.NORTH, (Comparable)byj.<V>getValue((Property<V>)TripWireBlock.SOUTH))).setValue((Property<Comparable>)TripWireBlock.EAST, (Comparable)byj.<V>getValue((Property<V>)TripWireBlock.WEST))).setValue((Property<Comparable>)TripWireBlock.SOUTH, (Comparable)byj.<V>getValue((Property<V>)TripWireBlock.NORTH))).<Comparable, Comparable>setValue((Property<Comparable>)TripWireBlock.WEST, (Comparable)byj.<V>getValue((Property<V>)TripWireBlock.EAST));
            }
            case COUNTERCLOCKWISE_90: {
                return (((((AbstractStateHolder<O, BlockState>)byj).setValue((Property<Comparable>)TripWireBlock.NORTH, (Comparable)byj.<V>getValue((Property<V>)TripWireBlock.EAST))).setValue((Property<Comparable>)TripWireBlock.EAST, (Comparable)byj.<V>getValue((Property<V>)TripWireBlock.SOUTH))).setValue((Property<Comparable>)TripWireBlock.SOUTH, (Comparable)byj.<V>getValue((Property<V>)TripWireBlock.WEST))).<Comparable, Comparable>setValue((Property<Comparable>)TripWireBlock.WEST, (Comparable)byj.<V>getValue((Property<V>)TripWireBlock.NORTH));
            }
            case CLOCKWISE_90: {
                return (((((AbstractStateHolder<O, BlockState>)byj).setValue((Property<Comparable>)TripWireBlock.NORTH, (Comparable)byj.<V>getValue((Property<V>)TripWireBlock.WEST))).setValue((Property<Comparable>)TripWireBlock.EAST, (Comparable)byj.<V>getValue((Property<V>)TripWireBlock.NORTH))).setValue((Property<Comparable>)TripWireBlock.SOUTH, (Comparable)byj.<V>getValue((Property<V>)TripWireBlock.EAST))).<Comparable, Comparable>setValue((Property<Comparable>)TripWireBlock.WEST, (Comparable)byj.<V>getValue((Property<V>)TripWireBlock.SOUTH));
            }
            default: {
                return byj;
            }
        }
    }
    
    @Override
    public BlockState mirror(final BlockState byj, final Mirror bsu) {
        switch (bsu) {
            case LEFT_RIGHT: {
                return (((AbstractStateHolder<O, BlockState>)byj).setValue((Property<Comparable>)TripWireBlock.NORTH, (Comparable)byj.<V>getValue((Property<V>)TripWireBlock.SOUTH))).<Comparable, Comparable>setValue((Property<Comparable>)TripWireBlock.SOUTH, (Comparable)byj.<V>getValue((Property<V>)TripWireBlock.NORTH));
            }
            case FRONT_BACK: {
                return (((AbstractStateHolder<O, BlockState>)byj).setValue((Property<Comparable>)TripWireBlock.EAST, (Comparable)byj.<V>getValue((Property<V>)TripWireBlock.WEST))).<Comparable, Comparable>setValue((Property<Comparable>)TripWireBlock.WEST, (Comparable)byj.<V>getValue((Property<V>)TripWireBlock.EAST));
            }
            default: {
                return super.mirror(byj, bsu);
            }
        }
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(TripWireBlock.POWERED, TripWireBlock.ATTACHED, TripWireBlock.DISARMED, TripWireBlock.NORTH, TripWireBlock.EAST, TripWireBlock.WEST, TripWireBlock.SOUTH);
    }
    
    static {
        POWERED = BlockStateProperties.POWERED;
        ATTACHED = BlockStateProperties.ATTACHED;
        DISARMED = BlockStateProperties.DISARMED;
        NORTH = PipeBlock.NORTH;
        EAST = PipeBlock.EAST;
        SOUTH = PipeBlock.SOUTH;
        WEST = PipeBlock.WEST;
        PROPERTY_BY_DIRECTION = CrossCollisionBlock.PROPERTY_BY_DIRECTION;
        AABB = Block.box(0.0, 1.0, 0.0, 16.0, 2.5, 16.0);
        NOT_ATTACHED_AABB = Block.box(0.0, 0.0, 0.0, 16.0, 8.0, 16.0);
    }
}
