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.entity.player.Player;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import com.google.common.base.MoreObjects;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import javax.annotation.Nullable;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.LevelAccessor;
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.properties.Property;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;

public class TripWireHookBlock extends Block {
    public static final DirectionProperty FACING;
    public static final BooleanProperty POWERED;
    public static final BooleanProperty ATTACHED;
    protected static final VoxelShape NORTH_AABB;
    protected static final VoxelShape SOUTH_AABB;
    protected static final VoxelShape WEST_AABB;
    protected static final VoxelShape EAST_AABB;
    
    public TripWireHookBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((((AbstractStateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)TripWireHookBlock.FACING, Direction.NORTH)).setValue((Property<Comparable>)TripWireHookBlock.POWERED, false)).<Comparable, Boolean>setValue((Property<Comparable>)TripWireHookBlock.ATTACHED, false));
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        switch (byj.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING)) {
            default: {
                return TripWireHookBlock.EAST_AABB;
            }
            case WEST: {
                return TripWireHookBlock.WEST_AABB;
            }
            case SOUTH: {
                return TripWireHookBlock.SOUTH_AABB;
            }
            case NORTH: {
                return TripWireHookBlock.NORTH_AABB;
            }
        }
    }
    
    @Override
    public boolean canSurvive(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        final Direction fp5 = byj.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING);
        final BlockPos fk2 = fk.relative(fp5.getOpposite());
        final BlockState byj2 = bjz.getBlockState(fk2);
        return fp5.getAxis().isHorizontal() && byj2.isFaceSturdy(bjz, fk2, fp5) && !byj2.isSignalSource();
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (fp.getOpposite() == byj1.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING) && !byj1.canSurvive(bjx, fk5)) {
            return Blocks.AIR.defaultBlockState();
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        BlockState byj3 = (((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)TripWireHookBlock.POWERED, false)).<Comparable, Boolean>setValue((Property<Comparable>)TripWireHookBlock.ATTACHED, false);
        final LevelReader bjz4 = bcq.getLevel();
        final BlockPos fk5 = bcq.getClickedPos();
        final Direction[] nearestLookingDirections;
        final Direction[] arr6 = nearestLookingDirections = bcq.getNearestLookingDirections();
        for (final Direction fp10 : nearestLookingDirections) {
            if (fp10.getAxis().isHorizontal()) {
                final Direction fp11 = fp10.getOpposite();
                byj3 = ((AbstractStateHolder<O, BlockState>)byj3).<Comparable, Direction>setValue((Property<Comparable>)TripWireHookBlock.FACING, fp11);
                if (byj3.canSurvive(bjz4, fk5)) {
                    return byj3;
                }
            }
        }
        return null;
    }
    
    @Override
    public void setPlacedBy(final Level bjw, final BlockPos fk, final BlockState byj, final LivingEntity akz, final ItemStack ben) {
        this.calculateState(bjw, fk, byj, false, false, -1, null);
    }
    
    public void calculateState(final Level bjw, final BlockPos fk, final BlockState byj3, final boolean boolean4, final boolean boolean5, final int integer, @Nullable final BlockState byj7) {
        final Direction fp9 = byj3.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING);
        final boolean boolean6 = byj3.<Boolean>getValue((Property<Boolean>)TripWireHookBlock.ATTACHED);
        final boolean boolean7 = byj3.<Boolean>getValue((Property<Boolean>)TripWireHookBlock.POWERED);
        boolean boolean8 = !boolean4;
        boolean boolean9 = false;
        int integer2 = 0;
        final BlockState[] arr15 = new BlockState[42];
        int integer3 = 1;
        while (integer3 < 42) {
            final BlockPos fk2 = fk.relative(fp9, integer3);
            BlockState byj8 = bjw.getBlockState(fk2);
            if (byj8.getBlock() == Blocks.TRIPWIRE_HOOK) {
                if (byj8.<Comparable>getValue((Property<Comparable>)TripWireHookBlock.FACING) == fp9.getOpposite()) {
                    integer2 = integer3;
                    break;
                }
                break;
            }
            else {
                if (byj8.getBlock() == Blocks.TRIPWIRE || integer3 == integer) {
                    if (integer3 == integer) {
                        byj8 = MoreObjects.<BlockState>firstNonNull(byj7, byj8);
                    }
                    final boolean boolean10 = !byj8.<Boolean>getValue((Property<Boolean>)TripWireBlock.DISARMED);
                    final boolean boolean11 = byj8.<Boolean>getValue((Property<Boolean>)TripWireBlock.POWERED);
                    boolean9 |= (boolean10 && boolean11);
                    arr15[integer3] = byj8;
                    if (integer3 == integer) {
                        bjw.getBlockTicks().scheduleTick(fk, this, this.getTickDelay(bjw));
                        boolean8 &= boolean10;
                    }
                }
                else {
                    arr15[integer3] = null;
                    boolean8 = false;
                }
                ++integer3;
            }
        }
        boolean8 &= (integer2 > 1);
        boolean9 &= boolean8;
        final BlockState byj9 = (((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)TripWireHookBlock.ATTACHED, boolean8)).<Comparable, Boolean>setValue((Property<Comparable>)TripWireHookBlock.POWERED, boolean9);
        if (integer2 > 0) {
            final BlockPos fk2 = fk.relative(fp9, integer2);
            final Direction fp10 = fp9.getOpposite();
            bjw.setBlock(fk2, ((AbstractStateHolder<O, BlockState>)byj9).<Comparable, Direction>setValue((Property<Comparable>)TripWireHookBlock.FACING, fp10), 3);
            this.notifyNeighbors(bjw, fk2, fp10);
            this.playSound(bjw, fk2, boolean8, boolean9, boolean6, boolean7);
        }
        this.playSound(bjw, fk, boolean8, boolean9, boolean6, boolean7);
        if (!boolean4) {
            bjw.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj9).<Comparable, Direction>setValue((Property<Comparable>)TripWireHookBlock.FACING, fp9), 3);
            if (boolean5) {
                this.notifyNeighbors(bjw, fk, fp9);
            }
        }
        if (boolean6 != boolean8) {
            for (int integer4 = 1; integer4 < integer2; ++integer4) {
                final BlockPos fk3 = fk.relative(fp9, integer4);
                final BlockState byj10 = arr15[integer4];
                if (byj10 != null) {
                    bjw.setBlock(fk3, ((AbstractStateHolder<O, BlockState>)byj10).<Comparable, Boolean>setValue((Property<Comparable>)TripWireHookBlock.ATTACHED, boolean8), 3);
                    if (!bjw.getBlockState(fk3).isAir()) {}
                }
            }
        }
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        this.calculateState(xd, fk, byj, false, true, -1, null);
    }
    
    private void playSound(final Level bjw, final BlockPos fk, final boolean boolean3, final boolean boolean4, final boolean boolean5, final boolean boolean6) {
        if (boolean4 && !boolean6) {
            bjw.playSound(null, fk, SoundEvents.TRIPWIRE_CLICK_ON, SoundSource.BLOCKS, 0.4f, 0.6f);
        }
        else if (!boolean4 && boolean6) {
            bjw.playSound(null, fk, SoundEvents.TRIPWIRE_CLICK_OFF, SoundSource.BLOCKS, 0.4f, 0.5f);
        }
        else if (boolean3 && !boolean5) {
            bjw.playSound(null, fk, SoundEvents.TRIPWIRE_ATTACH, SoundSource.BLOCKS, 0.4f, 0.7f);
        }
        else if (!boolean3 && boolean5) {
            bjw.playSound(null, fk, SoundEvents.TRIPWIRE_DETACH, SoundSource.BLOCKS, 0.4f, 1.2f / (bjw.random.nextFloat() * 0.2f + 0.9f));
        }
    }
    
    private void notifyNeighbors(final Level bjw, final BlockPos fk, final Direction fp) {
        bjw.updateNeighborsAt(fk, this);
        bjw.updateNeighborsAt(fk.relative(fp.getOpposite()), this);
    }
    
    @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;
        }
        final boolean boolean6 = byj1.<Boolean>getValue((Property<Boolean>)TripWireHookBlock.ATTACHED);
        final boolean boolean7 = byj1.<Boolean>getValue((Property<Boolean>)TripWireHookBlock.POWERED);
        if (boolean6 || boolean7) {
            this.calculateState(bjw, fk, byj1, true, false, -1, null);
        }
        if (boolean7) {
            bjw.updateNeighborsAt(fk, this);
            bjw.updateNeighborsAt(fk.relative(byj1.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING).getOpposite()), this);
        }
        super.onRemove(byj1, bjw, fk, byj4, boolean5);
    }
    
    @Override
    public int getSignal(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final Direction fp) {
        return byj.<Boolean>getValue((Property<Boolean>)TripWireHookBlock.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>)TripWireHookBlock.POWERED)) {
            return 0;
        }
        if (byj.<Comparable>getValue((Property<Comparable>)TripWireHookBlock.FACING) == fp) {
            return 15;
        }
        return 0;
    }
    
    @Override
    public boolean isSignalSource(final BlockState byj) {
        return true;
    }
    
    @Override
    public BlockState rotate(final BlockState byj, final Rotation btu) {
        return ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Direction>setValue((Property<Comparable>)TripWireHookBlock.FACING, btu.rotate(byj.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState byj, final Mirror bsu) {
        return byj.rotate(bsu.getRotation(byj.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING)));
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(TripWireHookBlock.FACING, TripWireHookBlock.POWERED, TripWireHookBlock.ATTACHED);
    }
    
    static {
        FACING = HorizontalDirectionalBlock.FACING;
        POWERED = BlockStateProperties.POWERED;
        ATTACHED = BlockStateProperties.ATTACHED;
        NORTH_AABB = Block.box(5.0, 0.0, 10.0, 11.0, 10.0, 16.0);
        SOUTH_AABB = Block.box(5.0, 0.0, 0.0, 11.0, 10.0, 6.0);
        WEST_AABB = Block.box(10.0, 0.0, 5.0, 16.0, 10.0, 11.0);
        EAST_AABB = Block.box(0.0, 0.0, 5.0, 6.0, 10.0, 11.0);
    }
}
