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 net.minecraft.util.Mth;
import net.minecraft.world.level.material.PushReaction;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.LivingEntity;
import javax.annotation.Nullable;
import net.minecraft.world.item.BlockPlaceContext;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.LevelAccessor;
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.DoubleBlockHalf;
import net.minecraft.world.level.block.state.properties.DoorHingeSide;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;

public class DoorBlock extends Block {
    public static final DirectionProperty FACING;
    public static final BooleanProperty OPEN;
    public static final EnumProperty<DoorHingeSide> HINGE;
    public static final BooleanProperty POWERED;
    public static final EnumProperty<DoubleBlockHalf> HALF;
    protected static final VoxelShape SOUTH_AABB;
    protected static final VoxelShape NORTH_AABB;
    protected static final VoxelShape WEST_AABB;
    protected static final VoxelShape EAST_AABB;
    
    protected DoorBlock(final Properties c) {
        super(c);
        this.registerDefaultState(((((((StateHolder<O, BlockState>)this.stateDefinition.any()).setValue((Property<Comparable>)DoorBlock.FACING, Direction.NORTH)).setValue((Property<Comparable>)DoorBlock.OPEN, false)).setValue(DoorBlock.HINGE, DoorHingeSide.LEFT)).setValue((Property<Comparable>)DoorBlock.POWERED, false)).<DoubleBlockHalf, DoubleBlockHalf>setValue(DoorBlock.HALF, DoubleBlockHalf.LOWER));
    }
    
    @Override
    public VoxelShape getShape(final BlockState cfj, final BlockGetter bpg, final BlockPos fu, final CollisionContext der) {
        final Direction fz6 = cfj.<Direction>getValue((Property<Direction>)DoorBlock.FACING);
        final boolean boolean7 = !cfj.<Boolean>getValue((Property<Boolean>)DoorBlock.OPEN);
        final boolean boolean8 = cfj.<DoorHingeSide>getValue(DoorBlock.HINGE) == DoorHingeSide.RIGHT;
        switch (fz6) {
            default: {
                return boolean7 ? DoorBlock.EAST_AABB : (boolean8 ? DoorBlock.NORTH_AABB : DoorBlock.SOUTH_AABB);
            }
            case SOUTH: {
                return boolean7 ? DoorBlock.SOUTH_AABB : (boolean8 ? DoorBlock.EAST_AABB : DoorBlock.WEST_AABB);
            }
            case WEST: {
                return boolean7 ? DoorBlock.WEST_AABB : (boolean8 ? DoorBlock.SOUTH_AABB : DoorBlock.NORTH_AABB);
            }
            case NORTH: {
                return boolean7 ? DoorBlock.NORTH_AABB : (boolean8 ? DoorBlock.WEST_AABB : DoorBlock.EAST_AABB);
            }
        }
    }
    
    @Override
    public BlockState updateShape(final BlockState cfj1, final Direction fz, final BlockState cfj3, final LevelAccessor bqc, final BlockPos fu5, final BlockPos fu6) {
        final DoubleBlockHalf cgf8 = cfj1.<DoubleBlockHalf>getValue(DoorBlock.HALF);
        if (fz.getAxis() == Direction.Axis.Y && cgf8 == DoubleBlockHalf.LOWER == (fz == Direction.UP)) {
            if (cfj3.is(this) && cfj3.<DoubleBlockHalf>getValue(DoorBlock.HALF) != cgf8) {
                return (((((StateHolder<O, BlockState>)cfj1).setValue((Property<Comparable>)DoorBlock.FACING, (Comparable)cfj3.<V>getValue((Property<V>)DoorBlock.FACING))).setValue((Property<Comparable>)DoorBlock.OPEN, (Comparable)cfj3.<V>getValue((Property<V>)DoorBlock.OPEN))).setValue(DoorBlock.HINGE, (Comparable)cfj3.<V>getValue((Property<V>)DoorBlock.HINGE))).<Comparable, Comparable>setValue((Property<Comparable>)DoorBlock.POWERED, (Comparable)cfj3.<V>getValue((Property<V>)DoorBlock.POWERED));
            }
            return Blocks.AIR.defaultBlockState();
        }
        else {
            if (cgf8 == DoubleBlockHalf.LOWER && fz == Direction.DOWN && !cfj1.canSurvive(bqc, fu5)) {
                return Blocks.AIR.defaultBlockState();
            }
            return super.updateShape(cfj1, fz, cfj3, bqc, fu5, fu6);
        }
    }
    
    @Override
    public void playerWillDestroy(final Level bqb, final BlockPos fu, final BlockState cfj, final Player bec) {
        if (!bqb.isClientSide && bec.isCreative()) {
            DoublePlantBlock.preventCreativeDropFromBottomPart(bqb, fu, cfj, bec);
        }
        super.playerWillDestroy(bqb, fu, cfj, bec);
    }
    
    @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>)DoorBlock.OPEN);
            }
            case WATER: {
                return false;
            }
            case AIR: {
                return cfj.<Boolean>getValue((Property<Boolean>)DoorBlock.OPEN);
            }
            default: {
                return false;
            }
        }
    }
    
    private int getCloseSound() {
        return (this.material == Material.METAL) ? 1011 : 1012;
    }
    
    private int getOpenSound() {
        return (this.material == Material.METAL) ? 1005 : 1006;
    }
    
    @Nullable
    @Override
    public BlockState getStateForPlacement(final BlockPlaceContext bin) {
        final BlockPos fu3 = bin.getClickedPos();
        if (fu3.getY() < 255 && bin.getLevel().getBlockState(fu3.above()).canBeReplaced(bin)) {
            final Level bqb4 = bin.getLevel();
            final boolean boolean5 = bqb4.hasNeighborSignal(fu3) || bqb4.hasNeighborSignal(fu3.above());
            return ((((((StateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)DoorBlock.FACING, bin.getHorizontalDirection())).setValue(DoorBlock.HINGE, this.getHinge(bin))).setValue((Property<Comparable>)DoorBlock.POWERED, boolean5)).setValue((Property<Comparable>)DoorBlock.OPEN, boolean5)).<DoubleBlockHalf, DoubleBlockHalf>setValue(DoorBlock.HALF, DoubleBlockHalf.LOWER);
        }
        return null;
    }
    
    @Override
    public void setPlacedBy(final Level bqb, final BlockPos fu, final BlockState cfj, final LivingEntity aoy, final ItemStack bki) {
        bqb.setBlock(fu.above(), ((StateHolder<O, BlockState>)cfj).<DoubleBlockHalf, DoubleBlockHalf>setValue(DoorBlock.HALF, DoubleBlockHalf.UPPER), 3);
    }
    
    private DoorHingeSide getHinge(final BlockPlaceContext bin) {
        final BlockGetter bpg3 = bin.getLevel();
        final BlockPos fu4 = bin.getClickedPos();
        final Direction fz5 = bin.getHorizontalDirection();
        final BlockPos fu5 = fu4.above();
        final Direction fz6 = fz5.getCounterClockWise();
        final BlockPos fu6 = fu4.relative(fz6);
        final BlockState cfj9 = bpg3.getBlockState(fu6);
        final BlockPos fu7 = fu5.relative(fz6);
        final BlockState cfj10 = bpg3.getBlockState(fu7);
        final Direction fz7 = fz5.getClockWise();
        final BlockPos fu8 = fu4.relative(fz7);
        final BlockState cfj11 = bpg3.getBlockState(fu8);
        final BlockPos fu9 = fu5.relative(fz7);
        final BlockState cfj12 = bpg3.getBlockState(fu9);
        final int integer17 = (cfj9.isCollisionShapeFullBlock(bpg3, fu6) ? -1 : 0) + (cfj10.isCollisionShapeFullBlock(bpg3, fu7) ? -1 : 0) + (cfj11.isCollisionShapeFullBlock(bpg3, fu8) ? 1 : 0) + (cfj12.isCollisionShapeFullBlock(bpg3, fu9) ? 1 : 0);
        final boolean boolean18 = cfj9.is(this) && cfj9.<DoubleBlockHalf>getValue(DoorBlock.HALF) == DoubleBlockHalf.LOWER;
        final boolean boolean19 = cfj11.is(this) && cfj11.<DoubleBlockHalf>getValue(DoorBlock.HALF) == DoubleBlockHalf.LOWER;
        if ((boolean18 && !boolean19) || integer17 > 0) {
            return DoorHingeSide.RIGHT;
        }
        if ((boolean19 && !boolean18) || integer17 < 0) {
            return DoorHingeSide.LEFT;
        }
        final int integer18 = fz5.getStepX();
        final int integer19 = fz5.getStepZ();
        final Vec3 dem22 = bin.getClickLocation();
        final double double23 = dem22.x - fu4.getX();
        final double double24 = dem22.z - fu4.getZ();
        return ((integer18 < 0 && double24 < 0.5) || (integer18 > 0 && double24 > 0.5) || (integer19 < 0 && double23 > 0.5) || (integer19 > 0 && double23 < 0.5)) ? DoorHingeSide.RIGHT : DoorHingeSide.LEFT;
    }
    
    @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>)DoorBlock.OPEN);
        bqb.setBlock(fu, cfj, 10);
        bqb.levelEvent(bec, ((boolean)cfj.<Boolean>getValue((Property<Boolean>)DoorBlock.OPEN)) ? this.getOpenSound() : this.getCloseSound(), fu, 0);
        return InteractionResult.sidedSuccess(bqb.isClientSide);
    }
    
    public void setOpen(final Level bqb, final BlockPos fu, final boolean boolean3) {
        final BlockState cfj5 = bqb.getBlockState(fu);
        if (!cfj5.is(this) || cfj5.<Boolean>getValue((Property<Boolean>)DoorBlock.OPEN) == boolean3) {
            return;
        }
        bqb.setBlock(fu, ((StateHolder<O, BlockState>)cfj5).<Comparable, Boolean>setValue((Property<Comparable>)DoorBlock.OPEN, boolean3), 10);
        this.playSound(bqb, fu, boolean3);
    }
    
    @Override
    public void neighborChanged(final BlockState cfj, final Level bqb, final BlockPos fu3, final Block bvr, final BlockPos fu5, final boolean boolean6) {
        final boolean boolean7 = bqb.hasNeighborSignal(fu3) || bqb.hasNeighborSignal(fu3.relative((cfj.<DoubleBlockHalf>getValue(DoorBlock.HALF) == DoubleBlockHalf.LOWER) ? Direction.UP : Direction.DOWN));
        if (bvr != this && boolean7 != cfj.<Boolean>getValue((Property<Boolean>)DoorBlock.POWERED)) {
            if (boolean7 != cfj.<Boolean>getValue((Property<Boolean>)DoorBlock.OPEN)) {
                this.playSound(bqb, fu3, boolean7);
            }
            bqb.setBlock(fu3, (((StateHolder<O, BlockState>)cfj).setValue((Property<Comparable>)DoorBlock.POWERED, boolean7)).<Comparable, Boolean>setValue((Property<Comparable>)DoorBlock.OPEN, boolean7), 2);
        }
    }
    
    @Override
    public boolean canSurvive(final BlockState cfj, final LevelReader bqd, final BlockPos fu) {
        final BlockPos fu2 = fu.below();
        final BlockState cfj2 = bqd.getBlockState(fu2);
        if (cfj.<DoubleBlockHalf>getValue(DoorBlock.HALF) == DoubleBlockHalf.LOWER) {
            return cfj2.isFaceSturdy(bqd, fu2, Direction.UP);
        }
        return cfj2.is(this);
    }
    
    private void playSound(final Level bqb, final BlockPos fu, final boolean boolean3) {
        bqb.levelEvent(null, boolean3 ? this.getOpenSound() : this.getCloseSound(), fu, 0);
    }
    
    @Override
    public PushReaction getPistonPushReaction(final BlockState cfj) {
        return PushReaction.DESTROY;
    }
    
    @Override
    public BlockState rotate(final BlockState cfj, final Rotation cap) {
        return ((StateHolder<O, BlockState>)cfj).<Comparable, Direction>setValue((Property<Comparable>)DoorBlock.FACING, cap.rotate(cfj.<Direction>getValue((Property<Direction>)DoorBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState cfj, final Mirror bzj) {
        if (bzj == Mirror.NONE) {
            return cfj;
        }
        return ((StateHolder<O, BlockState>)cfj.rotate(bzj.getRotation(cfj.<Direction>getValue((Property<Direction>)DoorBlock.FACING)))).<DoorHingeSide>cycle(DoorBlock.HINGE);
    }
    
    @Override
    public long getSeed(final BlockState cfj, final BlockPos fu) {
        return Mth.getSeed(fu.getX(), fu.below((int)((cfj.<DoubleBlockHalf>getValue(DoorBlock.HALF) != DoubleBlockHalf.LOWER) ? 1 : 0)).getY(), fu.getZ());
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(DoorBlock.HALF, DoorBlock.FACING, DoorBlock.OPEN, DoorBlock.HINGE, DoorBlock.POWERED);
    }
    
    public static boolean isWoodenDoor(final Level bqb, final BlockPos fu) {
        return isWoodenDoor(bqb.getBlockState(fu));
    }
    
    public static boolean isWoodenDoor(final BlockState cfj) {
        return cfj.getBlock() instanceof DoorBlock && (cfj.getMaterial() == Material.WOOD || cfj.getMaterial() == Material.NETHER_WOOD);
    }
    
    static {
        FACING = HorizontalDirectionalBlock.FACING;
        OPEN = BlockStateProperties.OPEN;
        HINGE = BlockStateProperties.DOOR_HINGE;
        POWERED = BlockStateProperties.POWERED;
        HALF = BlockStateProperties.DOUBLE_BLOCK_HALF;
        SOUTH_AABB = Block.box(0.0, 0.0, 0.0, 16.0, 16.0, 3.0);
        NORTH_AABB = Block.box(0.0, 0.0, 13.0, 16.0, 16.0, 16.0);
        WEST_AABB = Block.box(13.0, 0.0, 0.0, 16.0, 16.0, 16.0);
        EAST_AABB = Block.box(0.0, 0.0, 0.0, 3.0, 16.0, 16.0);
    }
}
