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.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.entity.LivingEntity;
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.Entity;
import net.minecraft.world.item.ItemStack;
import javax.annotation.Nullable;
import net.minecraft.world.level.block.entity.BlockEntity;
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.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(((((((AbstractStateHolder<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 byj, final BlockGetter bjg, final BlockPos fk, final CollisionContext cvq) {
        final Direction fp6 = byj.<Direction>getValue((Property<Direction>)DoorBlock.FACING);
        final boolean boolean7 = !byj.<Boolean>getValue((Property<Boolean>)DoorBlock.OPEN);
        final boolean boolean8 = byj.<DoorHingeSide>getValue(DoorBlock.HINGE) == DoorHingeSide.RIGHT;
        switch (fp6) {
            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 byj1, final Direction fp, final BlockState byj3, final LevelAccessor bjx, final BlockPos fk5, final BlockPos fk6) {
        final DoubleBlockHalf bzg8 = byj1.<DoubleBlockHalf>getValue(DoorBlock.HALF);
        if (fp.getAxis() == Direction.Axis.Y && bzg8 == DoubleBlockHalf.LOWER == (fp == Direction.UP)) {
            if (byj3.getBlock() == this && byj3.<DoubleBlockHalf>getValue(DoorBlock.HALF) != bzg8) {
                return (((((AbstractStateHolder<O, BlockState>)byj1).setValue((Property<Comparable>)DoorBlock.FACING, (Comparable)byj3.<V>getValue((Property<V>)DoorBlock.FACING))).setValue((Property<Comparable>)DoorBlock.OPEN, (Comparable)byj3.<V>getValue((Property<V>)DoorBlock.OPEN))).setValue(DoorBlock.HINGE, (Comparable)byj3.<V>getValue((Property<V>)DoorBlock.HINGE))).<Comparable, Comparable>setValue((Property<Comparable>)DoorBlock.POWERED, (Comparable)byj3.<V>getValue((Property<V>)DoorBlock.POWERED));
            }
            return Blocks.AIR.defaultBlockState();
        }
        else {
            if (bzg8 == DoubleBlockHalf.LOWER && fp == Direction.DOWN && !byj1.canSurvive(bjx, fk5)) {
                return Blocks.AIR.defaultBlockState();
            }
            return super.updateShape(byj1, fp, byj3, bjx, fk5, fk6);
        }
    }
    
    @Override
    public void playerDestroy(final Level bjw, final Player ayj, final BlockPos fk, final BlockState byj, @Nullable final BlockEntity bwl, final ItemStack ben) {
        super.playerDestroy(bjw, ayj, fk, Blocks.AIR.defaultBlockState(), bwl, ben);
    }
    
    @Override
    public void playerWillDestroy(final Level bjw, final BlockPos fk, final BlockState byj, final Player ayj) {
        final DoubleBlockHalf bzg6 = byj.<DoubleBlockHalf>getValue(DoorBlock.HALF);
        final BlockPos fk2 = (bzg6 == DoubleBlockHalf.LOWER) ? fk.above() : fk.below();
        final BlockState byj2 = bjw.getBlockState(fk2);
        if (byj2.getBlock() == this && byj2.<DoubleBlockHalf>getValue(DoorBlock.HALF) != bzg6) {
            bjw.setBlock(fk2, Blocks.AIR.defaultBlockState(), 35);
            bjw.levelEvent(ayj, 2001, fk2, Block.getId(byj2));
            final ItemStack ben9 = ayj.getMainHandItem();
            if (!bjw.isClientSide && !ayj.isCreative() && ayj.canDestroy(byj2)) {
                Block.dropResources(byj, bjw, fk, null, ayj, ben9);
                Block.dropResources(byj2, bjw, fk2, null, ayj, ben9);
            }
        }
        super.playerWillDestroy(bjw, fk, byj, ayj);
    }
    
    @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>)DoorBlock.OPEN);
            }
            case WATER: {
                return false;
            }
            case AIR: {
                return byj.<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 bcq) {
        final BlockPos fk3 = bcq.getClickedPos();
        if (fk3.getY() < 255 && bcq.getLevel().getBlockState(fk3.above()).canBeReplaced(bcq)) {
            final Level bjw4 = bcq.getLevel();
            final boolean boolean5 = bjw4.hasNeighborSignal(fk3) || bjw4.hasNeighborSignal(fk3.above());
            return ((((((AbstractStateHolder<O, BlockState>)this.defaultBlockState()).setValue((Property<Comparable>)DoorBlock.FACING, bcq.getHorizontalDirection())).setValue(DoorBlock.HINGE, this.getHinge(bcq))).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 bjw, final BlockPos fk, final BlockState byj, final LivingEntity akz, final ItemStack ben) {
        bjw.setBlock(fk.above(), ((AbstractStateHolder<O, BlockState>)byj).<DoubleBlockHalf, DoubleBlockHalf>setValue(DoorBlock.HALF, DoubleBlockHalf.UPPER), 3);
    }
    
    private DoorHingeSide getHinge(final BlockPlaceContext bcq) {
        final BlockGetter bjg3 = bcq.getLevel();
        final BlockPos fk4 = bcq.getClickedPos();
        final Direction fp5 = bcq.getHorizontalDirection();
        final BlockPos fk5 = fk4.above();
        final Direction fp6 = fp5.getCounterClockWise();
        final BlockPos fk6 = fk4.relative(fp6);
        final BlockState byj9 = bjg3.getBlockState(fk6);
        final BlockPos fk7 = fk5.relative(fp6);
        final BlockState byj10 = bjg3.getBlockState(fk7);
        final Direction fp7 = fp5.getClockWise();
        final BlockPos fk8 = fk4.relative(fp7);
        final BlockState byj11 = bjg3.getBlockState(fk8);
        final BlockPos fk9 = fk5.relative(fp7);
        final BlockState byj12 = bjg3.getBlockState(fk9);
        final int integer17 = (byj9.isCollisionShapeFullBlock(bjg3, fk6) ? -1 : 0) + (byj10.isCollisionShapeFullBlock(bjg3, fk7) ? -1 : 0) + (byj11.isCollisionShapeFullBlock(bjg3, fk8) ? 1 : 0) + (byj12.isCollisionShapeFullBlock(bjg3, fk9) ? 1 : 0);
        final boolean boolean18 = byj9.getBlock() == this && byj9.<DoubleBlockHalf>getValue(DoorBlock.HALF) == DoubleBlockHalf.LOWER;
        final boolean boolean19 = byj11.getBlock() == this && byj11.<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 = fp5.getStepX();
        final int integer19 = fp5.getStepZ();
        final Vec3 cvl22 = bcq.getClickLocation();
        final double double23 = cvl22.x - fk4.getX();
        final double double24 = cvl22.z - fk4.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 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>)DoorBlock.OPEN);
        bjw.setBlock(fk, byj, 10);
        bjw.levelEvent(ayj, ((boolean)byj.<Boolean>getValue((Property<Boolean>)DoorBlock.OPEN)) ? this.getOpenSound() : this.getCloseSound(), fk, 0);
        return InteractionResult.SUCCESS;
    }
    
    public void setOpen(final Level bjw, final BlockPos fk, final boolean boolean3) {
        final BlockState byj5 = bjw.getBlockState(fk);
        if (byj5.getBlock() != this || byj5.<Boolean>getValue((Property<Boolean>)DoorBlock.OPEN) == boolean3) {
            return;
        }
        bjw.setBlock(fk, ((AbstractStateHolder<O, BlockState>)byj5).<Comparable, Boolean>setValue((Property<Comparable>)DoorBlock.OPEN, boolean3), 10);
        this.playSound(bjw, fk, boolean3);
    }
    
    @Override
    public void neighborChanged(final BlockState byj, final Level bjw, final BlockPos fk3, final Block bph, final BlockPos fk5, final boolean boolean6) {
        final boolean boolean7 = bjw.hasNeighborSignal(fk3) || bjw.hasNeighborSignal(fk3.relative((byj.<DoubleBlockHalf>getValue(DoorBlock.HALF) == DoubleBlockHalf.LOWER) ? Direction.UP : Direction.DOWN));
        if (bph != this && boolean7 != byj.<Boolean>getValue((Property<Boolean>)DoorBlock.POWERED)) {
            if (boolean7 != byj.<Boolean>getValue((Property<Boolean>)DoorBlock.OPEN)) {
                this.playSound(bjw, fk3, boolean7);
            }
            bjw.setBlock(fk3, (((AbstractStateHolder<O, BlockState>)byj).setValue((Property<Comparable>)DoorBlock.POWERED, boolean7)).<Comparable, Boolean>setValue((Property<Comparable>)DoorBlock.OPEN, boolean7), 2);
        }
    }
    
    @Override
    public boolean canSurvive(final BlockState byj, final LevelReader bjz, final BlockPos fk) {
        final BlockPos fk2 = fk.below();
        final BlockState byj2 = bjz.getBlockState(fk2);
        if (byj.<DoubleBlockHalf>getValue(DoorBlock.HALF) == DoubleBlockHalf.LOWER) {
            return byj2.isFaceSturdy(bjz, fk2, Direction.UP);
        }
        return byj2.getBlock() == this;
    }
    
    private void playSound(final Level bjw, final BlockPos fk, final boolean boolean3) {
        bjw.levelEvent(null, boolean3 ? this.getOpenSound() : this.getCloseSound(), fk, 0);
    }
    
    @Override
    public PushReaction getPistonPushReaction(final BlockState byj) {
        return PushReaction.DESTROY;
    }
    
    @Override
    public BlockState rotate(final BlockState byj, final Rotation btu) {
        return ((AbstractStateHolder<O, BlockState>)byj).<Comparable, Direction>setValue((Property<Comparable>)DoorBlock.FACING, btu.rotate(byj.<Direction>getValue((Property<Direction>)DoorBlock.FACING)));
    }
    
    @Override
    public BlockState mirror(final BlockState byj, final Mirror bsu) {
        if (bsu == Mirror.NONE) {
            return byj;
        }
        return ((AbstractStateHolder<O, BlockState>)byj.rotate(bsu.getRotation(byj.<Direction>getValue((Property<Direction>)DoorBlock.FACING)))).<DoorHingeSide>cycle(DoorBlock.HINGE);
    }
    
    @Override
    public long getSeed(final BlockState byj, final BlockPos fk) {
        return Mth.getSeed(fk.getX(), fk.below((int)((byj.<DoubleBlockHalf>getValue(DoorBlock.HALF) != DoubleBlockHalf.LOWER) ? 1 : 0)).getY(), fk.getZ());
    }
    
    @Override
    protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> a) {
        a.add(DoorBlock.HALF, DoorBlock.FACING, DoorBlock.OPEN, DoorBlock.HINGE, DoorBlock.POWERED);
    }
    
    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);
    }
}
