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.entity.EntityType;
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.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(((((((AbstractStateHolder<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 byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        if (!byj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.OPEN)) {
            return (byj.<Half>getValue(TrapDoorBlock.HALF) == Half.TOP) ? TrapDoorBlock.TOP_AABB : TrapDoorBlock.BOTTOM_AABB;
        }
        switch (byj.<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 byj, final BlockGetter bjg, final BlockPos fk, final PathComputationType cqr) {
        switch (cqr) {
            case LAND: {
                return byj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.OPEN);
            }
            case WATER: {
                return byj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.WATERLOGGED);
            }
            case AIR: {
                return byj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.OPEN);
            }
            default: {
                return false;
            }
        }
    }
    
    @Override
    public InteractionResult use(BlockState byj, final Level bjw, final BlockPos fk, final Player ayj, final InteractionHand ajk, final BlockHitResult cvg) {
        if (this.material == Material.METAL) {
            return InteractionResult.PASS;
        }
        byj = ((AbstractStateHolder<O, BlockState>)byj).<Comparable>cycle((Property<Comparable>)TrapDoorBlock.OPEN);
        bjw.setBlock(fk, byj, 2);
        if (byj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.WATERLOGGED)) {
            bjw.getLiquidTicks().scheduleTick(fk, Fluids.WATER, Fluids.WATER.getTickDelay(bjw));
        }
        this.playSound(ayj, bjw, fk, byj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.OPEN));
        return InteractionResult.SUCCESS;
    }
    
    protected void playSound(@Nullable final Player ayj, final Level bjw, final BlockPos fk, final boolean boolean4) {
        if (boolean4) {
            final int integer6 = (this.material == Material.METAL) ? 1037 : 1007;
            bjw.levelEvent(ayj, integer6, fk, 0);
        }
        else {
            final int integer6 = (this.material == Material.METAL) ? 1036 : 1013;
            bjw.levelEvent(ayj, integer6, fk, 0);
        }
    }
    
    @Override
    public void neighborChanged(BlockState byj, final Level bjw, final BlockPos fk3, final Block bph, final BlockPos fk5, final boolean boolean6) {
        if (bjw.isClientSide) {
            return;
        }
        final boolean boolean7 = bjw.hasNeighborSignal(fk3);
        if (boolean7 != byj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.POWERED)) {
            if (byj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.OPEN) != boolean7) {
                byj = ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)TrapDoorBlock.OPEN, boolean7);
                this.playSound(null, bjw, fk3, boolean7);
            }
            bjw.setBlock(fk3, ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Boolean>setValue((Property<Comparable>)TrapDoorBlock.POWERED, boolean7), 2);
            if (byj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.WATERLOGGED)) {
                bjw.getLiquidTicks().scheduleTick(fk3, Fluids.WATER, Fluids.WATER.getTickDelay(bjw));
            }
        }
    }
    
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bcq) {
        BlockState byj3 = this.defaultBlockState();
        final FluidState coj4 = bcq.getLevel().getFluidState(bcq.getClickedPos());
        final Direction fp5 = bcq.getClickedFace();
        if (bcq.replacingClickedOnBlock() || !fp5.getAxis().isHorizontal()) {
            byj3 = (((AbstractStateHolder<O, BlockState>)byj3).setValue((Property<Comparable>)TrapDoorBlock.FACING, bcq.getHorizontalDirection().getOpposite())).<Half, Half>setValue(TrapDoorBlock.HALF, (fp5 == Direction.UP) ? Half.BOTTOM : Half.TOP);
        }
        else {
            byj3 = (((AbstractStateHolder<O, BlockState>)byj3).setValue((Property<Comparable>)TrapDoorBlock.FACING, fp5)).<Half, Half>setValue(TrapDoorBlock.HALF, (bcq.getClickLocation().y - bcq.getClickedPos().getY() > 0.5) ? Half.TOP : Half.BOTTOM);
        }
        if (bcq.getLevel().hasNeighborSignal(bcq.getClickedPos())) {
            byj3 = (((AbstractStateHolder<O, BlockState>)byj3).setValue((Property<Comparable>)TrapDoorBlock.OPEN, true)).<Comparable, Boolean>setValue((Property<Comparable>)TrapDoorBlock.POWERED, true);
        }
        return ((AbstractStateHolder<O, BlockState>)byj3).<Comparable, Boolean>setValue((Property<Comparable>)TrapDoorBlock.WATERLOGGED, coj4.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 byj) {
        if (byj.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.WATERLOGGED)) {
            return Fluids.WATER.getSource(false);
        }
        return super.getFluidState(byj);
    }
    
    @Override
    public BlockState updateShape(final BlockState byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        if (byj1.<Boolean>getValue((Property<Boolean>)TrapDoorBlock.WATERLOGGED)) {
            bjx.getLiquidTicks().scheduleTick(fk5, Fluids.WATER, Fluids.WATER.getTickDelay(bjx));
        }
        return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
    }
    
    @Override
    public boolean isValidSpawn(final BlockState byj, final BlockGetter bjg, final BlockPos fk, final EntityType<?> aku) {
        return false;
    }
    
    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);
    }
}
