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.LevelAccessor;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.item.BlockPlaceContext;
import javax.annotation.Nullable;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.material.Fluids;
import net.minecraft.world.level.material.Material;
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.level.pathfinder.PathComputationType;
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.Half;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.BooleanProperty;

public class TrapDoorBlock extends HorizontalDirectionalBlock implements SimpleWaterloggedBlock {
    public static final BooleanProperty OPEN;
    public static final EnumProperty<Half> HALF;
    public static final BooleanProperty POWERED;
    public static final BooleanProperty WATERLOGGED;
    protected static final VoxelShape EAST_OPEN_AABB;
    protected static final VoxelShape WEST_OPEN_AABB;
    protected static final VoxelShape SOUTH_OPEN_AABB;
    protected static final VoxelShape NORTH_OPEN_AABB;
    protected static final VoxelShape BOTTOM_AABB;
    protected static final VoxelShape TOP_AABB;
    
    protected TrapDoorBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((((((StateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)TrapDoorBlock.FACING, Direction.NORTH)).setValue((Property<Comparable>)TrapDoorBlock.OPEN, false)).setValue(TrapDoorBlock.HALF, Half.BOTTOM)).setValue((Property<Comparable>)TrapDoorBlock.POWERED, false)).<Comparable, Boolean>setValue((Property<Comparable>)TrapDoorBlock.WATERLOGGED, false));
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        if (!cfj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.OPEN)) {
            return (cfj.<Half>getValue(TrapDoorBlock.HALF) == Half.TOP) ? TrapDoorBlock.TOP_AABB : TrapDoorBlock.BOTTOM_AABB;
        }
        switch (cfj.<Direction>getValue((Property<Direction>)TrapDoorBlock.FACING)) {
            default: {
                return TrapDoorBlock.NORTH_OPEN_AABB;
            }
            case SOUTH: {
                return TrapDoorBlock.SOUTH_OPEN_AABB;
            }
            case WEST: {
                return TrapDoorBlock.WEST_OPEN_AABB;
            }
            case EAST: {
                return TrapDoorBlock.EAST_OPEN_AABB;
            }
        }
    }
    
    @Override
    public boolean isPathfindable(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final PathComputationType czg) {
        switch (czg) {
            case LAND: {
                return cfj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.OPEN);
            }
            case WATER: {
                return cfj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.WATERLOGGED);
            }
            case AIR: {
                return cfj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.OPEN);
            }
            default: {
                return false;
            }
        }
    }
    
    @Override
    public InteractionResult use(BlockState cfj, final Level bqb, final BlockPos fu, final Player bec, final InteractionHand anf, final BlockHitResult deh) {
        if (this.material == Material.METAL) {
            return InteractionResult.PASS;
        }
        cfj = ((StateHolder<O, BlockState>)cfj).<Comparable>cycle((Property<Comparable>)TrapDoorBlock.OPEN);
        bqb.setBlock(fu, cfj, 2);
        if (cfj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.WATERLOGGED)) {
            bqb.getLiquidTicks().scheduleTick(fu, Fluids.WATER, Fluids.WATER.getTickDelay(bqb));
        }
        this.playSound(bec, bqb, fu, cfj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.OPEN));
        return InteractionResult.sidedSuccess(bqb.isClientSide);
    }
    
    protected void playSound(@Nullable final Player bec, final Level bqb, final BlockPos fu, final boolean boolean4) {
        if (boolean4) {
            final int integer6 = (this.material == Material.METAL) ? 1037 : 1007;
            bqb.levelEvent(bec, integer6, fu, 0);
        }
        else {
            final int integer6 = (this.material == Material.METAL) ? 1036 : 1013;
            bqb.levelEvent(bec, integer6, fu, 0);
        }
    }
    
    @Override
    public void neighborChanged(BlockState cfj, final Level bqb, final BlockPos fu3, final Block bvr, final BlockPos fu5, final boolean boolean6) {
        if (bqb.isClientSide) {
            return;
        }
        final boolean boolean7 = bqb.hasNeighborSignal(fu3);
        if (boolean7 != cfj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.POWERED)) {
            if (cfj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.OPEN) != boolean7) {
                cfj = ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)TrapDoorBlock.OPEN, boolean7);
                this.playSound(null, bqb, fu3, boolean7);
            }
            bqb.setBlock(fu3, ((StateHolder<O, BlockState>)cfj).<Comparable, Boolean>setValue((Property<Comparable>)TrapDoorBlock.POWERED, boolean7), 2);
            if (cfj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.WATERLOGGED)) {
                bqb.getLiquidTicks().scheduleTick(fu3, Fluids.WATER, Fluids.WATER.getTickDelay(bqb));
            }
        }
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        BlockState cfj3 = this.defaultBlockState();
        final FluidState cxa4 = bin.getLevel().getFluidState(bin.getClickedPos());
        final Direction fz5 = bin.getClickedFace();
        if (bin.replacingClickedOnBlock() || !fz5.getAxis().isHorizontal()) {
            cfj3 = (((StateHolder<O, BlockState>)cfj3).setValue((Property<Comparable>)TrapDoorBlock.FACING, bin.getHorizontalDirection().getOpposite())).<Half, Half>setValue(TrapDoorBlock.HALF, (fz5 == Direction.UP) ? Half.BOTTOM : Half.TOP);
        }
        else {
            cfj3 = (((StateHolder<O, BlockState>)cfj3).setValue((Property<Comparable>)TrapDoorBlock.FACING, fz5)).<Half, Half>setValue(TrapDoorBlock.HALF, (bin.getClickLocation().y - bin.getClickedPos().getY() > 0.5) ? Half.TOP : Half.BOTTOM);
        }
        if (bin.getLevel().hasNeighborSignal(bin.getClickedPos())) {
            cfj3 = (((StateHolder<O, BlockState>)cfj3).setValue((Property<Comparable>)TrapDoorBlock.OPEN, true)).<Comparable, Boolean>setValue((Property<Comparable>)TrapDoorBlock.POWERED, true);
        }
        return ((StateHolder<O, BlockState>)cfj3).<Comparable, Boolean>setValue((Property<Comparable>)TrapDoorBlock.WATERLOGGED, cxa4.getType() == Fluids.WATER);
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(TrapDoorBlock.FACING, TrapDoorBlock.OPEN, TrapDoorBlock.HALF, TrapDoorBlock.POWERED, TrapDoorBlock.WATERLOGGED);
    }
    
    @Override
    public FluidState getFluidState(final BlockState cfj) {
        if (cfj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.WATERLOGGED)) {
            return Fluids.WATER.getSource(false);
        }
        return super.getFluidState(cfj);
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        if (cfj1.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.WATERLOGGED)) {
            bqc.getLiquidTicks().scheduleTick(fu5, Fluids.WATER, Fluids.WATER.getTickDelay(bqc));
        }
        return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
    }
    
    static {
        OPEN = BlockStateProperties.OPEN;
        HALF = BlockStateProperties.HALF;
        POWERED = BlockStateProperties.POWERED;
        WATERLOGGED = BlockStateProperties.WATERLOGGED;
        EAST_OPEN_AABB = Block.box(0.0, 0.0, 0.0, 3.0, 16.0, 16.0);
        WEST_OPEN_AABB = Block.box(13.0, 0.0, 0.0, 16.0, 16.0, 16.0);
        SOUTH_OPEN_AABB = Block.box(0.0, 0.0, 0.0, 16.0, 16.0, 3.0);
        NORTH_OPEN_AABB = Block.box(0.0, 0.0, 13.0, 16.0, 16.0, 16.0);
        BOTTOM_AABB = Block.box(0.0, 0.0, 0.0, 16.0, 3.0, 16.0);
        TOP_AABB = Block.box(0.0, 13.0, 0.0, 16.0, 16.0, 16.0);
    }
}
