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 java.util.Random;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.DustParticleOptions;
import net.minecraft.sounds.SoundSource;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
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.AttachFace;
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;

public class LeverBlock extends FaceAttachedHorizontalDirectionalBlock {
    public static final BooleanProperty POWERED;
    protected static final VoxelShape NORTH_AABB;
    protected static final VoxelShape SOUTH_AABB;
    protected static final VoxelShape WEST_AABB;
    protected static final VoxelShape EAST_AABB;
    protected static final VoxelShape UP_AABB_Z;
    protected static final VoxelShape UP_AABB_X;
    protected static final VoxelShape DOWN_AABB_Z;
    protected static final VoxelShape DOWN_AABB_X;
    
    protected LeverBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((((StateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)LeverBlock.FACING, Direction.NORTH)).setValue((Property<Comparable>)LeverBlock.POWERED, false)).<AttachFace, AttachFace>setValue(LeverBlock.FACE, AttachFace.WALL));
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        switch (cfj.<AttachFace>getValue(LeverBlock.FACE)) {
            case FLOOR: {
                switch (cfj.<Direction>getValue((Property<Direction>)LeverBlock.FACING).getAxis()) {
                    case X: {
                        return LeverBlock.UP_AABB_X;
                    }
                    default: {
                        return LeverBlock.UP_AABB_Z;
                    }
                }
                break;
            }
            case WALL: {
                switch (cfj.<Direction>getValue((Property<Direction>)LeverBlock.FACING)) {
                    case EAST: {
                        return LeverBlock.EAST_AABB;
                    }
                    case WEST: {
                        return LeverBlock.WEST_AABB;
                    }
                    case SOUTH: {
                        return LeverBlock.SOUTH_AABB;
                    }
                    default: {
                        return LeverBlock.NORTH_AABB;
                    }
                }
                break;
            }
            default: {
                switch (cfj.<Direction>getValue((Property<Direction>)LeverBlock.FACING).getAxis()) {
                    case X: {
                        return LeverBlock.DOWN_AABB_X;
                    }
                    default: {
                        return LeverBlock.DOWN_AABB_Z;
                    }
                }
                break;
            }
        }
    }
    
    @Override
    public InteractionResult use(final BlockState cfj, final Level bqb, final BlockPos fu, final Player bec, final InteractionHand anf, final BlockHitResult deh) {
        if (bqb.isClientSide) {
            final BlockState cfj2 = ((StateHolder<O, BlockState>)cfj).<Comparable>cycle((Property<Comparable>)LeverBlock.POWERED);
            if (cfj2.<Boolean>getValue((Property<Boolean>)LeverBlock.POWERED)) {
                makeParticle(cfj2, bqb, fu, 1.0f);
            }
            return InteractionResult.SUCCESS;
        }
        final BlockState cfj2 = this.pull(cfj, bqb, fu);
        final float float9 = cfj2.<Boolean>getValue((Property<Boolean>)LeverBlock.POWERED) ? 0.6f : 0.5f;
        bqb.playSound(null, fu, SoundEvents.LEVER_CLICK, SoundSource.BLOCKS, 0.3f, float9);
        return InteractionResult.CONSUME;
    }
    
    public BlockState pull(BlockState cfj, final Level bqb, final BlockPos fu) {
        cfj = ((StateHolder<O, BlockState>)cfj).<Comparable>cycle((Property<Comparable>)LeverBlock.POWERED);
        bqb.setBlock(fu, cfj, 3);
        this.updateNeighbours(cfj, bqb, fu);
        return cfj;
    }
    
    private static void makeParticle(final BlockState cfj, final LevelAccessor bqc, final BlockPos fu, final float float4) {
        final Direction fz5 = cfj.<Direction>getValue((Property<Direction>)LeverBlock.FACING).getOpposite();
        final Direction fz6 = FaceAttachedHorizontalDirectionalBlock.getConnectedDirection(cfj).getOpposite();
        final double double7 = fu.getX() + 0.5 + 0.1 * fz5.getStepX() + 0.2 * fz6.getStepX();
        final double double8 = fu.getY() + 0.5 + 0.1 * fz5.getStepY() + 0.2 * fz6.getStepY();
        final double double9 = fu.getZ() + 0.5 + 0.1 * fz5.getStepZ() + 0.2 * fz6.getStepZ();
        bqc.addParticle(new DustParticleOptions(1.0f, 0.0f, 0.0f, float4), double7, double8, double9, 0.0, 0.0, 0.0);
    }
    
    @Override
    public void animateTick(final BlockState cfj, final Level bqb, final BlockPos fu, final Random random) {
        if (cfj.<Boolean>getValue((Property<Boolean>)LeverBlock.POWERED) && random.nextFloat() < 0.25f) {
            makeParticle(cfj, bqb, fu, 0.5f);
        }
    }
    
    @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;
        }
        if (cfj1.<Boolean>getValue((Property<Boolean>)LeverBlock.POWERED)) {
            this.updateNeighbours(cfj1, bqb, fu);
        }
        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>)LeverBlock.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>)LeverBlock.POWERED) && FaceAttachedHorizontalDirectionalBlock.getConnectedDirection(cfj) == fz) {
            return 15;
        }
        return 0;
    }
    
    @Override
    public boolean isSignalSource(final BlockState cfj) {
        return true;
    }
    
    private void updateNeighbours(final BlockState cfj, final Level bqb, final BlockPos fu) {
        bqb.updateNeighborsAt(fu, this);
        bqb.updateNeighborsAt(fu.relative(FaceAttachedHorizontalDirectionalBlock.getConnectedDirection(cfj).getOpposite()), this);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(LeverBlock.FACE, LeverBlock.FACING, LeverBlock.POWERED);
    }
    
    static {
        POWERED = BlockStateProperties.POWERED;
        NORTH_AABB = Block.box(5.0, 4.0, 10.0, 11.0, 12.0, 16.0);
        SOUTH_AABB = Block.box(5.0, 4.0, 0.0, 11.0, 12.0, 6.0);
        WEST_AABB = Block.box(10.0, 4.0, 5.0, 16.0, 12.0, 11.0);
        EAST_AABB = Block.box(0.0, 4.0, 5.0, 6.0, 12.0, 11.0);
        UP_AABB_Z = Block.box(5.0, 0.0, 4.0, 11.0, 6.0, 12.0);
        UP_AABB_X = Block.box(4.0, 0.0, 5.0, 12.0, 6.0, 11.0);
        DOWN_AABB_Z = Block.box(5.0, 10.0, 4.0, 11.0, 16.0, 12.0);
        DOWN_AABB_X = Block.box(4.0, 10.0, 5.0, 12.0, 16.0, 11.0);
    }
}
