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.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.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(((((AbstractStateHolder<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 byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        switch (byj.<AttachFace>getValue(LeverBlock.FACE)) {
            case FLOOR: {
                switch (byj.<Direction>getValue((Property<Direction>)LeverBlock.FACING).getAxis()) {
                    case X: {
                        return LeverBlock.UP_AABB_X;
                    }
                    default: {
                        return LeverBlock.UP_AABB_Z;
                    }
                }
                break;
            }
            case WALL: {
                switch (byj.<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 (byj.<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 byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        if (bjw.isClientSide) {
            final BlockState byj2 = ((AbstractStateHolder<O, BlockState>)byj).<Comparable>cycle((Property<Comparable>)LeverBlock.POWERED);
            if (byj2.<Boolean>getValue((Property<Boolean>)LeverBlock.POWERED)) {
                makeParticle(byj2, bjw, fk, 1.0f);
            }
            return InteractionResult.SUCCESS;
        }
        final BlockState byj2 = this.pull(byj, bjw, fk);
        final float float9 = byj2.<Boolean>getValue((Property<Boolean>)LeverBlock.POWERED) ? 0.6f : 0.5f;
        bjw.playSound(null, fk, SoundEvents.LEVER_CLICK, SoundSource.BLOCKS, 0.3f, float9);
        return InteractionResult.SUCCESS;
    }
    
    public BlockState pull(BlockState byj, final Level bjw, final BlockPos fk) {
        byj = ((AbstractStateHolder<O, BlockState>)byj).<Comparable>cycle((Property<Comparable>)LeverBlock.POWERED);
        bjw.setBlock(fk, byj, 3);
        this.updateNeighbours(byj, bjw, fk);
        return byj;
    }
    
    private static void makeParticle(final BlockState byj, final LevelAccessor bjx, final BlockPos fk, final float float4) {
        final Direction fp5 = byj.<Direction>getValue((Property<Direction>)LeverBlock.FACING).getOpposite();
        final Direction fp6 = FaceAttachedHorizontalDirectionalBlock.getConnectedDirection(byj).getOpposite();
        final double double7 = fk.getX() + 0.5 + 0.1 * fp5.getStepX() + 0.2 * fp6.getStepX();
        final double double8 = fk.getY() + 0.5 + 0.1 * fp5.getStepY() + 0.2 * fp6.getStepY();
        final double double9 = fk.getZ() + 0.5 + 0.1 * fp5.getStepZ() + 0.2 * fp6.getStepZ();
        bjx.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 byj, final Level bjw, final BlockPos fk, final Random random) {
        if (byj.<Boolean>getValue((Property<Boolean>)LeverBlock.POWERED) && random.nextFloat() < 0.25f) {
            makeParticle(byj, bjw, fk, 0.5f);
        }
    }
    
    @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;
        }
        if (byj1.<Boolean>getValue((Property<Boolean>)LeverBlock.POWERED)) {
            this.updateNeighbours(byj1, bjw, fk);
        }
        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>)LeverBlock.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>)LeverBlock.POWERED) && FaceAttachedHorizontalDirectionalBlock.getConnectedDirection(byj) == fp) {
            return 15;
        }
        return 0;
    }
    
    @Override
    public boolean isSignalSource(final BlockState byj) {
        return true;
    }
    
    private void updateNeighbours(final BlockState byj, final Level bjw, final BlockPos fk) {
        bjw.updateNeighborsAt(fk, this);
        bjw.updateNeighborsAt(fk.relative(FaceAttachedHorizontalDirectionalBlock.getConnectedDirection(byj).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);
    }
}
