package net.minecraft.world.level.block;

import net.minecraft.world.level.material.PushReaction;
import net.minecraft.core.Vec3i;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import java.util.Random;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.core.Direction;
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.BlockState;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.shapes.VoxelShape;

public abstract class BasePressurePlateBlock extends Block {
    protected static final VoxelShape PRESSED_AABB;
    protected static final VoxelShape AABB;
    protected static final AABB TOUCH_AABB;
    
    protected BasePressurePlateBlock(final Properties c) {
        super(c);
    }
    
    @Override
    public VoxelShape getShape(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        return (this.getSignalForState(byj) > 0) ? BasePressurePlateBlock.PRESSED_AABB : BasePressurePlateBlock.AABB;
    }
    
    @Override
    public int getTickDelay(final LevelReader bjz) {
        return 20;
    }
    
    @Override
    public boolean isPossibleToRespawnInThis() {
        return true;
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (fp == Direction.DOWN && !byj1.canSurvive(bjx, fk5)) {
            return Blocks.AIR.defaultBlockState();
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Override
    public boolean canSurvive(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        final BlockPos fk2 = fk.below();
        return Block.canSupportRigidBlock(bjz, fk2) || Block.canSupportCenter(bjz, fk2, Direction.UP);
    }
    
    @Override
    public void tick(final BlockState byj, final ServerLevel xd, final BlockPos fk, final Random random) {
        final int integer6 = this.getSignalForState(byj);
        if (integer6 > 0) {
            this.checkPressed(xd, fk, byj, integer6);
        }
    }
    
    @Override
    public void entityInside(final BlockState byj, final Level bjw, final BlockPos fk, final Entity akq) {
        if (bjw.isClientSide) {
            return;
        }
        final int integer6 = this.getSignalForState(byj);
        if (integer6 == 0) {
            this.checkPressed(bjw, fk, byj, integer6);
        }
    }
    
    protected void checkPressed(final Level bjw, final BlockPos fk, final BlockState byj, final int integer) {
        final int integer2 = this.getSignalStrength(bjw, fk);
        final boolean boolean7 = integer > 0;
        final boolean boolean8 = integer2 > 0;
        if (integer != integer2) {
            final BlockState byj2 = this.setSignalForState(byj, integer2);
            bjw.setBlock(fk, byj2, 2);
            this.updateNeighbours(bjw, fk);
            bjw.setBlocksDirty(fk, byj, byj2);
        }
        if (!boolean8 && boolean7) {
            this.playOffSound(bjw, fk);
        }
        else if (boolean8 && !boolean7) {
            this.playOnSound(bjw, fk);
        }
        if (boolean8) {
            bjw.getBlockTicks().scheduleTick(new BlockPos(fk), this, this.getTickDelay(bjw));
        }
    }
    
    protected abstract void playOnSound(final LevelAccessor bjx, final BlockPos fk);
    
    protected abstract void playOffSound(final LevelAccessor bjx, final BlockPos fk);
    
    @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 (this.getSignalForState(byj1) > 0) {
            this.updateNeighbours(bjw, fk);
        }
        super.onRemove(byj1, bjw, fk, byj4, boolean5);
    }
    
    protected void updateNeighbours(final Level bjw, final BlockPos fk) {
        bjw.updateNeighborsAt(fk, this);
        bjw.updateNeighborsAt(fk.below(), this);
    }
    
    @Override
    public int getSignal(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final Direction fp) {
        return this.getSignalForState(byj);
    }
    
    @Override
    public int getDirectSignal(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final Direction fp) {
        if (fp == Direction.UP) {
            return this.getSignalForState(byj);
        }
        return 0;
    }
    
    @Override
    public boolean isSignalSource(final BlockState byj) {
        return true;
    }
    
    @Override
    public PushReaction getPistonPushReaction(final BlockState byj) {
        return PushReaction.DESTROY;
    }
    
    protected abstract int getSignalStrength(final Level bjw, final BlockPos fk);
    
    protected abstract int getSignalForState(final BlockState byj);
    
    protected abstract BlockState setSignalForState(final BlockState byj, final int integer);
    
    static {
        PRESSED_AABB = Block.box(1.0, 0.0, 1.0, 15.0, 0.5, 15.0);
        AABB = Block.box(1.0, 0.0, 1.0, 15.0, 1.0, 15.0);
        TOUCH_AABB = new AABB(0.125, 0.0, 0.125, 0.875, 0.25, 0.875);
    }
}
