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.List;
import net.minecraft.core.Vec3i;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.entity.Entity;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import javax.annotation.Nullable;
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 abstract class ButtonBlock extends FaceAttachedHorizontalDirectionalBlock {
    public static final BooleanProperty POWERED;
    protected static final VoxelShape CEILING_AABB_X;
    protected static final VoxelShape CEILING_AABB_Z;
    protected static final VoxelShape FLOOR_AABB_X;
    protected static final VoxelShape FLOOR_AABB_Z;
    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 PRESSED_CEILING_AABB_X;
    protected static final VoxelShape PRESSED_CEILING_AABB_Z;
    protected static final VoxelShape PRESSED_FLOOR_AABB_X;
    protected static final VoxelShape PRESSED_FLOOR_AABB_Z;
    protected static final VoxelShape PRESSED_NORTH_AABB;
    protected static final VoxelShape PRESSED_SOUTH_AABB;
    protected static final VoxelShape PRESSED_WEST_AABB;
    protected static final VoxelShape PRESSED_EAST_AABB;
    private final boolean sensitive;
    
    protected ButtonBlock(final boolean boolean1, final Properties c) {
        super(c);
        this.registerDefaultState(((((StateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)ButtonBlock.FACING, Direction.NORTH)).setValue((Property<Comparable>)ButtonBlock.POWERED, false)).<AttachFace, AttachFace>setValue(ButtonBlock.FACE, AttachFace.WALL));
        this.sensitive = boolean1;
    }
    
    private int getPressDuration() {
        return this.sensitive ? 30 : 20;
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        final Direction fz6 = cfj.<Direction>getValue((Property<Direction>)ButtonBlock.FACING);
        final boolean boolean7 = cfj.<Boolean>getValue((Property<Boolean>)ButtonBlock.POWERED);
        switch (cfj.<AttachFace>getValue(ButtonBlock.FACE)) {
            case FLOOR: {
                if (fz6.getAxis() == Direction.Axis.X) {
                    return boolean7 ? ButtonBlock.PRESSED_FLOOR_AABB_X : ButtonBlock.FLOOR_AABB_X;
                }
                return boolean7 ? ButtonBlock.PRESSED_FLOOR_AABB_Z : ButtonBlock.FLOOR_AABB_Z;
            }
            case WALL: {
                switch (fz6) {
                    case EAST: {
                        return boolean7 ? ButtonBlock.PRESSED_EAST_AABB : ButtonBlock.EAST_AABB;
                    }
                    case WEST: {
                        return boolean7 ? ButtonBlock.PRESSED_WEST_AABB : ButtonBlock.WEST_AABB;
                    }
                    case SOUTH: {
                        return boolean7 ? ButtonBlock.PRESSED_SOUTH_AABB : ButtonBlock.SOUTH_AABB;
                    }
                    default: {
                        return boolean7 ? ButtonBlock.PRESSED_NORTH_AABB : ButtonBlock.NORTH_AABB;
                    }
                }
                break;
            }
            default: {
                if (fz6.getAxis() == Direction.Axis.X) {
                    return boolean7 ? ButtonBlock.PRESSED_CEILING_AABB_X : ButtonBlock.CEILING_AABB_X;
                }
                return boolean7 ? ButtonBlock.PRESSED_CEILING_AABB_Z : ButtonBlock.CEILING_AABB_Z;
            }
        }
    }
    
    @Override
    public InteractionResult use(final BlockState cfj, final Level bqb, final BlockPos fu, final Player bec, final InteractionHand anf, final BlockHitResult deh) {
        if (cfj.<Boolean>getValue((Property<Boolean>)ButtonBlock.POWERED)) {
            return InteractionResult.CONSUME;
        }
        this.press(cfj, bqb, fu);
        this.playSound(bec, bqb, fu, true);
        return InteractionResult.sidedSuccess(bqb.isClientSide);
    }
    
    public void press(final BlockState cfj, final Level bqb, final BlockPos fu) {
        bqb.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)ButtonBlock.POWERED, true), 3);
        this.updateNeighbours(cfj, bqb, fu);
        bqb.getBlockTicks().scheduleTick(fu, this, this.getPressDuration());
    }
    
    protected void playSound(@Nullable final Player bec, final LevelAccessor bqc, final BlockPos fu, final boolean boolean4) {
        bqc.playSound(boolean4 ? bec : null, fu, this.getSound(boolean4), SoundSource.BLOCKS, 0.3f, boolean4 ? 0.6f : 0.5f);
    }
    
    protected abstract SoundEvent getSound(final boolean boolean1);
    
    @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>)ButtonBlock.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>)ButtonBlock.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>)ButtonBlock.POWERED) && FaceAttachedHorizontalDirectionalBlock.getConnectedDirection(cfj) == fz) {
            return 15;
        }
        return 0;
    }
    
    @Override
    public boolean isSignalSource(final BlockState cfj) {
        return true;
    }
    
    @Override
    public void tick(final BlockState cfj, final ServerLevel zd, final BlockPos fu, final Random random) {
        if (!cfj.<Boolean>getValue((Property<Boolean>)ButtonBlock.POWERED)) {
            return;
        }
        if (this.sensitive) {
            this.checkPressed(cfj, zd, fu);
        }
        else {
            zd.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)ButtonBlock.POWERED, false), 3);
            this.updateNeighbours(cfj, zd, fu);
            this.playSound(null, zd, fu, false);
        }
    }
    
    @Override
    public void entityInside(final BlockState cfj, final Level bqb, final BlockPos fu, final Entity aom) {
        if (bqb.isClientSide || !this.sensitive || cfj.<Boolean>getValue((Property<Boolean>)ButtonBlock.POWERED)) {
            return;
        }
        this.checkPressed(cfj, bqb, fu);
    }
    
    private void checkPressed(final BlockState cfj, final Level bqb, final BlockPos fu) {
        final List<? extends Entity> list5 = bqb.getEntitiesOfClass(AbstractArrow.class, cfj.getShape(bqb, fu).bounds().move(fu));
        final boolean boolean6 = !list5.isEmpty();
        final boolean boolean7 = cfj.<Boolean>getValue((Property<Boolean>)ButtonBlock.POWERED);
        if (boolean6 != boolean7) {
            bqb.setBlock(fu, ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)ButtonBlock.POWERED, boolean6), 3);
            this.updateNeighbours(cfj, bqb, fu);
            this.playSound(null, bqb, fu, boolean6);
        }
        if (boolean6) {
            bqb.getBlockTicks().scheduleTick(new BlockPos(fu), this, this.getPressDuration());
        }
    }
    
    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(ButtonBlock.FACING, ButtonBlock.POWERED, ButtonBlock.FACE);
    }
    
    static {
        POWERED = BlockStateProperties.POWERED;
        CEILING_AABB_X = Block.box(6.0, 14.0, 5.0, 10.0, 16.0, 11.0);
        CEILING_AABB_Z = Block.box(5.0, 14.0, 6.0, 11.0, 16.0, 10.0);
        FLOOR_AABB_X = Block.box(6.0, 0.0, 5.0, 10.0, 2.0, 11.0);
        FLOOR_AABB_Z = Block.box(5.0, 0.0, 6.0, 11.0, 2.0, 10.0);
        NORTH_AABB = Block.box(5.0, 6.0, 14.0, 11.0, 10.0, 16.0);
        SOUTH_AABB = Block.box(5.0, 6.0, 0.0, 11.0, 10.0, 2.0);
        WEST_AABB = Block.box(14.0, 6.0, 5.0, 16.0, 10.0, 11.0);
        EAST_AABB = Block.box(0.0, 6.0, 5.0, 2.0, 10.0, 11.0);
        PRESSED_CEILING_AABB_X = Block.box(6.0, 15.0, 5.0, 10.0, 16.0, 11.0);
        PRESSED_CEILING_AABB_Z = Block.box(5.0, 15.0, 6.0, 11.0, 16.0, 10.0);
        PRESSED_FLOOR_AABB_X = Block.box(6.0, 0.0, 5.0, 10.0, 1.0, 11.0);
        PRESSED_FLOOR_AABB_Z = Block.box(5.0, 0.0, 6.0, 11.0, 1.0, 10.0);
        PRESSED_NORTH_AABB = Block.box(5.0, 6.0, 15.0, 11.0, 10.0, 16.0);
        PRESSED_SOUTH_AABB = Block.box(5.0, 6.0, 0.0, 11.0, 10.0, 1.0);
        PRESSED_WEST_AABB = Block.box(15.0, 6.0, 5.0, 16.0, 10.0, 11.0);
        PRESSED_EAST_AABB = Block.box(0.0, 6.0, 5.0, 1.0, 10.0, 11.0);
    }
}
