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.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.level.block.state.BlockBehaviour;
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(((((StateHolder<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 cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        switch (cfj.<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 cfj, final LevelReader bqd, final BlockPos fu) {
        final Direction fz5 = cfj.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING);
        final BlockPos fu2 = fu.relative(fz5.getOpposite());
        final BlockState cfj2 = bqd.getBlockState(fu2);
        return fz5.getAxis().isHorizontal() && cfj2.isFaceSturdy(bqd, fu2, fz5);
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        if (fz.getOpposite() == cfj1.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING) && !cfj1.canSurvive(bqc, fu5)) {
            return Blocks.AIR.defaultBlockState();
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        BlockState cfj3 = (((StateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)TripWireHookBlock.POWERED, false)).<Comparable, Boolean>setValue((Property<Comparable>)TripWireHookBlock.ATTACHED, false);
        final LevelReader bqd4 = bin.getLevel();
        final BlockPos fu5 = bin.getClickedPos();
        final Direction[] nearestLookingDirections;
        final Direction[] arr6 = nearestLookingDirections = bin.getNearestLookingDirections();
        for (final Direction fz10 : nearestLookingDirections) {
            if (fz10.getAxis().isHorizontal()) {
                final Direction fz11 = fz10.getOpposite();
                cfj3 = ((StateHolder<O, BlockState>)cfj3).<Comparable, Direction>setValue((Property<Comparable>)TripWireHookBlock.FACING, fz11);
                if (cfj3.canSurvive(bqd4, fu5)) {
                    return cfj3;
                }
            }
        }
        return null;
    }
    
    @Override
    public void setPlacedBy(final Level bqb, final BlockPos fu, final BlockState cfj, final LivingEntity aoy, final ItemStack bki) {
        this.calculateState(bqb, fu, cfj, false, false, -1, null);
    }
    
    public void calculateState(final Level bqb, final BlockPos fu, final BlockState cfj3, final boolean boolean4, final boolean boolean5, final int integer, @Nullable final BlockState cfj7) {
        final Direction fz9 = cfj3.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING);
        final boolean boolean6 = cfj3.<Boolean>getValue((Property<Boolean>)TripWireHookBlock.ATTACHED);
        final boolean boolean7 = cfj3.<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 fu2 = fu.relative(fz9, integer3);
            BlockState cfj8 = bqb.getBlockState(fu2);
            if (cfj8.is(Blocks.TRIPWIRE_HOOK)) {
                if (cfj8.<Comparable>getValue((Property<Comparable>)TripWireHookBlock.FACING) == fz9.getOpposite()) {
                    integer2 = integer3;
                    break;
                }
                break;
            }
            else {
                if (cfj8.is(Blocks.TRIPWIRE) || integer3 == integer) {
                    if (integer3 == integer) {
                        cfj8 = MoreObjects.<BlockState>firstNonNull(cfj7, cfj8);
                    }
                    final boolean boolean10 = !cfj8.<Boolean>getValue((Property<Boolean>)TripWireBlock.DISARMED);
                    final boolean boolean11 = cfj8.<Boolean>getValue((Property<Boolean>)TripWireBlock.POWERED);
                    boolean9 |= (boolean10 && boolean11);
                    arr15[integer3] = cfj8;
                    if (integer3 == integer) {
                        bqb.getBlockTicks().scheduleTick(fu, this, 10);
                        boolean8 &= boolean10;
                    }
                }
                else {
                    arr15[integer3] = null;
                    boolean8 = false;
                }
                ++integer3;
            }
        }
        boolean8 &= (integer2 > 1);
        boolean9 &= boolean8;
        final BlockState cfj9 = (((StateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)TripWireHookBlock.ATTACHED, boolean8)).<Comparable, Boolean>setValue((Property<Comparable>)TripWireHookBlock.POWERED, boolean9);
        if (integer2 > 0) {
            final BlockPos fu2 = fu.relative(fz9, integer2);
            final Direction fz10 = fz9.getOpposite();
            bqb.setBlock(fu2, ((StateHolder<O, BlockState>)cfj9).<Comparable, Direction>setValue((Property<Comparable>)TripWireHookBlock.FACING, fz10), 3);
            this.notifyNeighbors(bqb, fu2, fz10);
            this.playSound(bqb, fu2, boolean8, boolean9, boolean6, boolean7);
        }
        this.playSound(bqb, fu, boolean8, boolean9, boolean6, boolean7);
        if (!boolean4) {
            bqb.setBlock(fu, ((StateHolder<O, BlockState>)cfj9).<Comparable, Direction>setValue((Property<Comparable>)TripWireHookBlock.FACING, fz9), 3);
            if (boolean5) {
                this.notifyNeighbors(bqb, fu, fz9);
            }
        }
        if (boolean6 != boolean8) {
            for (int integer4 = 1; integer4 < integer2; ++integer4) {
                final BlockPos fu3 = fu.relative(fz9, integer4);
                final BlockState cfj10 = arr15[integer4];
                if (cfj10 != null) {
                    bqb.setBlock(fu3, ((StateHolder<O, BlockState>)cfj10).<Comparable, Boolean>setValue((Property<Comparable>)TripWireHookBlock.ATTACHED, boolean8), 3);
                    if (!bqb.getBlockState(fu3).isAir()) {}
                }
            }
        }
    }
    
    @Override
    public void tick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        this.calculateState(zd, fu, cfj, false, true, -1, null);
    }
    
    private void playSound(final Level bqb, final BlockPos fu, final boolean boolean3, final boolean boolean4, final boolean boolean5, final boolean boolean6) {
        if (boolean4 && !boolean6) {
            bqb.playSound(null, fu, SoundEvents.TRIPWIRE_CLICK_ON, SoundSource.BLOCKS, 0.4f, 0.6f);
        }
        else if (!boolean4 && boolean6) {
            bqb.playSound(null, fu, SoundEvents.TRIPWIRE_CLICK_OFF, SoundSource.BLOCKS, 0.4f, 0.5f);
        }
        else if (boolean3 && !boolean5) {
            bqb.playSound(null, fu, SoundEvents.TRIPWIRE_ATTACH, SoundSource.BLOCKS, 0.4f, 0.7f);
        }
        else if (!boolean3 && boolean5) {
            bqb.playSound(null, fu, SoundEvents.TRIPWIRE_DETACH, SoundSource.BLOCKS, 0.4f, 1.2f / (bqb.random.nextFloat() * 0.2f + 0.9f));
        }
    }
    
    private void notifyNeighbors(final Level bqb, final BlockPos fu, final Direction fz) {
        bqb.updateNeighborsAt(fu, this);
        bqb.updateNeighborsAt(fu.relative(fz.getOpposite()), this);
    }
    
    @Override
    public void onRemove(final BlockState cfj1, final Level bqb, final BlockPos fu, final BlockState cfj4, final boolean boolean5) {
        if (boolean5 || cfj1.is(cfj4.getBlock())) {
            return;
        }
        final boolean boolean6 = cfj1.<Boolean>getValue((Property<Boolean>)TripWireHookBlock.ATTACHED);
        final boolean boolean7 = cfj1.<Boolean>getValue((Property<Boolean>)TripWireHookBlock.POWERED);
        if (boolean6 || boolean7) {
            this.calculateState(bqb, fu, cfj1, true, false, -1, null);
        }
        if (boolean7) {
            bqb.updateNeighborsAt(fu, this);
            bqb.updateNeighborsAt(fu.relative(cfj1.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING).getOpposite()), this);
        }
        super.onRemove(cfj1, bqb, fu, cfj4, boolean5);
    }
    
    @Override
    public int getSignal(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final Direction fz) {
        return cfj.<Boolean>getValue((Property<Boolean>)TripWireHookBlock.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>)TripWireHookBlock.POWERED)) {
            return 0;
        }
        if (cfj.<Comparable>getValue((Property<Comparable>)TripWireHookBlock.FACING) == fz) {
            return 15;
        }
        return 0;
    }
    
    @Override
    public boolean isSignalSource(final BlockState cfj) {
        return true;
    }
    
    @Override
    public BlockState rotate(final BlockState cfj, final Rotation cap) {
        return ((StateHolder<O, BlockState>)cfj).<Comparable, Direction>setValue((Property<Comparable>)TripWireHookBlock.FACING, cap.rotate(cfj.<Direction>getValue((Property<Direction>)TripWireHookBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState cfj, final Mirror bzj) {
        return cfj.rotate(bzj.getRotation(cfj.<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);
    }
}
