package com.dairymoose.modernlife.blocks;

import com.dairymoose.modernlife.core.CustomBlocks;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.npc.Villager;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Explosion;
import net.minecraft.world.level.ExplosionDamageCalculator;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.block.BedBlock;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.HorizontalDirectionalBlock;
import net.minecraft.world.level.block.Mirror;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.Rotation;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BedPart;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.material.PushReaction;
import net.minecraft.world.level.pathfinder.PathComputationType;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/DeluxeBedBlock.class */
public class DeluxeBedBlock extends BedBlock {
    public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING;
    public static final EnumProperty<CornerType> CORNER = EnumProperty.create("corner", CornerType.class);
    public static final EnumProperty<BedPart> PART = BlockStateProperties.BED_PART;
    public static final BooleanProperty OCCUPIED = BlockStateProperties.OCCUPIED;
    protected static final VoxelShape SOUTH_AABB = Block.box(0.01d, 0.0d, 0.01d, 15.99d, 11.0d, 15.99d);
    protected static final VoxelShape NORTH_AABB = Block.box(0.01d, 0.0d, 0.01d, 15.99d, 11.0d, 15.99d);
    protected static final VoxelShape WEST_AABB = Block.box(0.01d, 0.0d, 0.01d, 15.99d, 11.0d, 15.99d);
    protected static final VoxelShape EAST_AABB = Block.box(0.01d, 0.0d, 0.01d, 15.99d, 11.0d, 15.99d);

    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/DeluxeBedBlock$CornerType.class */
    public enum CornerType implements StringRepresentable {
        bottom_left,
        bottom_right,
        top_left,
        top_right;

        public String getSerializedName() {
            return name().toLowerCase();
        }
    }

    public DeluxeBedBlock(Properties p_i48413_1_) {
        super(DyeColor.GRAY, p_i48413_1_);
        registerDefaultState((BlockState) ((BlockState) ((BlockState) ((BlockState) this.stateDefinition.any().setValue(FACING, Direction.NORTH)).setValue(CORNER, CornerType.bottom_left)).setValue(PART, BedPart.FOOT)).setValue(OCCUPIED, false));
    }

    public int getFlammability(BlockState state, BlockGetter world, BlockPos pos, Direction face) {
        if (this.material.isFlammable()) {
            return 20;
        }
        return 0;
    }

    public int getFireSpreadSpeed(BlockState state, BlockGetter world, BlockPos pos, Direction face) {
        if (this.material.isFlammable()) {
            return 5;
        }
        return 0;
    }

    public RenderShape getRenderShape(BlockState p_149645_1_) {
        return RenderShape.MODEL;
    }

    public VoxelShape getShape(BlockState p_220053_1_, BlockGetter p_220053_2_, BlockPos p_220053_3_, CollisionContext p_220053_4_) {
        Direction lvt_5_1_ = p_220053_1_.getValue(FACING);
        switch (lvt_5_1_.ordinal()) {
            case 1:
            default:
                return EAST_AABB;
            case 2:
                return SOUTH_AABB;
            case 3:
                return WEST_AABB;
            case 4:
                return NORTH_AABB;
        }
    }

    public BlockState updateShape(BlockState p_196271_1_, Direction p_196271_2_, BlockState p_196271_3_, LevelAccessor p_196271_4_, BlockPos p_196271_5_, BlockPos p_196271_6_) {
        Direction checkDir;
        Direction checkDirLateral;
        CornerType lvt_7_1_ = (CornerType) p_196271_1_.getValue(CORNER);
        if (p_196271_1_.getValue(FACING) == Direction.EAST) {
            checkDir = Direction.EAST;
        } else if (p_196271_1_.getValue(FACING) == Direction.WEST) {
            checkDir = Direction.WEST;
        } else if (p_196271_1_.getValue(FACING) == Direction.NORTH) {
            checkDir = Direction.NORTH;
        } else {
            checkDir = Direction.SOUTH;
        }
        if (p_196271_1_.getValue(FACING) == Direction.EAST) {
            checkDirLateral = Direction.SOUTH;
        } else if (p_196271_1_.getValue(FACING) == Direction.WEST) {
            checkDirLateral = Direction.NORTH;
        } else if (p_196271_1_.getValue(FACING) == Direction.NORTH) {
            checkDirLateral = Direction.EAST;
        } else {
            checkDirLateral = Direction.WEST;
        }
        if ((lvt_7_1_ == CornerType.bottom_left || lvt_7_1_ == CornerType.top_left) && p_196271_5_.relative(checkDirLateral).equals(p_196271_6_) && !p_196271_3_.is(CustomBlocks.BLOCK_DELUXE_BED.get())) {
            return Blocks.AIR.defaultBlockState();
        }
        if ((lvt_7_1_ == CornerType.bottom_right || lvt_7_1_ == CornerType.top_right) && p_196271_5_.relative(checkDirLateral.getOpposite()).equals(p_196271_6_) && !p_196271_3_.is(CustomBlocks.BLOCK_DELUXE_BED.get())) {
            return Blocks.AIR.defaultBlockState();
        }
        if ((lvt_7_1_ == CornerType.bottom_left || lvt_7_1_ == CornerType.bottom_right) && p_196271_5_.relative(checkDir).equals(p_196271_6_) && !p_196271_3_.is(CustomBlocks.BLOCK_DELUXE_BED.get())) {
            return Blocks.AIR.defaultBlockState();
        }
        if ((lvt_7_1_ == CornerType.top_left || lvt_7_1_ == CornerType.top_right) && p_196271_5_.relative(checkDir.getOpposite()).equals(p_196271_6_) && !p_196271_3_.is(CustomBlocks.BLOCK_DELUXE_BED.get())) {
            return Blocks.AIR.defaultBlockState();
        }
        return p_196271_1_;
    }

    protected static void preventCreativeDropFromBottomPart(Level p_241471_0_, BlockPos p_241471_1_, BlockState p_241471_2_, Player p_241471_3_) {
        CornerType doubleblockhalf = (CornerType) p_241471_2_.getValue(CORNER);
        if (doubleblockhalf == CornerType.bottom_left) {
            BlockPos blockpos = p_241471_1_.below();
            BlockState blockstate = p_241471_0_.getBlockState(blockpos);
            if (blockstate.getBlock() == p_241471_2_.getBlock() && blockstate.getValue(CORNER) == CornerType.bottom_left) {
                p_241471_0_.setBlock(blockpos, Blocks.AIR.defaultBlockState(), 35);
                p_241471_0_.levelEvent(p_241471_3_, 2001, blockpos, Block.getId(blockstate));
            }
        }
    }

    public void playerWillDestroy(Level p_176208_1_, BlockPos p_176208_2_, BlockState p_176208_3_, Player p_176208_4_) {
        if (!p_176208_1_.isClientSide && p_176208_4_.isCreative()) {
            preventCreativeDropFromBottomPart(p_176208_1_, p_176208_2_, p_176208_3_, p_176208_4_);
        }
        super.playerWillDestroy(p_176208_1_, p_176208_2_, p_176208_3_, p_176208_4_);
    }

    @Nullable
    public BlockState getStateForPlacement(BlockPlaceContext p_196258_1_) {
        BlockPos lvt_2_1_ = p_196258_1_.getClickedPos();
        if (lvt_2_1_.getY() < p_196258_1_.getLevel().getMaxBuildHeight() && p_196258_1_.getLevel().getBlockState(lvt_2_1_.relative(p_196258_1_.getHorizontalDirection())).canBeReplaced(p_196258_1_) && p_196258_1_.getLevel().getBlockState(lvt_2_1_.relative(p_196258_1_.getHorizontalDirection()).relative(p_196258_1_.getHorizontalDirection().getClockWise())).canBeReplaced(p_196258_1_) && p_196258_1_.getLevel().getBlockState(lvt_2_1_.relative(p_196258_1_.getHorizontalDirection().getClockWise())).canBeReplaced(p_196258_1_)) {
            Level lvt_3_1_ = p_196258_1_.getLevel();
            boolean z = lvt_3_1_.hasNeighborSignal(lvt_2_1_) || lvt_3_1_.hasNeighborSignal(lvt_2_1_.above());
            return (BlockState) ((BlockState) defaultBlockState().setValue(FACING, p_196258_1_.getHorizontalDirection())).setValue(CORNER, CornerType.bottom_left);
        }
        return null;
    }

    public BlockState rotate(BlockState p_52790_, Rotation p_52791_) {
        return (BlockState) p_52790_.setValue(FACING, p_52791_.rotate(p_52790_.getValue(FACING)));
    }

    public BlockState mirror(BlockState p_52787_, Mirror p_52788_) {
        return p_52788_ == Mirror.NONE ? p_52787_ : p_52787_.rotate(p_52788_.getRotation(p_52787_.getValue(FACING)));
    }

    public void setPlacedBy(Level p_180633_1_, BlockPos p_180633_2_, BlockState p_180633_3_, LivingEntity p_180633_4_, ItemStack p_180633_5_) {
        p_180633_1_.setBlock(p_180633_2_.relative(p_180633_4_.getDirection()), (BlockState) p_180633_3_.setValue(CORNER, CornerType.top_left), 3);
        p_180633_1_.setBlock(p_180633_2_.relative(p_180633_4_.getDirection()).relative(p_180633_4_.getDirection().getClockWise()), (BlockState) p_180633_3_.setValue(CORNER, CornerType.top_right), 3);
        p_180633_1_.setBlock(p_180633_2_.relative(p_180633_4_.getDirection().getClockWise()), (BlockState) p_180633_3_.setValue(CORNER, CornerType.bottom_right), 3);
    }

    public boolean canSurvive(BlockState p_196260_1_, LevelReader p_196260_2_, BlockPos p_196260_3_) {
        BlockPos lvt_4_1_ = p_196260_3_.below();
        p_196260_2_.getBlockState(lvt_4_1_);
        return true;
    }

    public PushReaction getPistonPushReaction(BlockState p_149656_1_) {
        return PushReaction.DESTROY;
    }

    @OnlyIn(Dist.CLIENT)
    public long getSeed(BlockState p_209900_1_, BlockPos p_209900_2_) {
        return 0L;
    }

    public InteractionResult use(BlockState p_225533_1_, Level p_225533_2_, BlockPos p_225533_3_, Player p_225533_4_, InteractionHand p_225533_5_, BlockHitResult p_225533_6_) {
        if (p_225533_2_.isClientSide) {
            return InteractionResult.CONSUME;
        }
        if (p_225533_1_.getValue(CORNER) != CornerType.top_left && p_225533_1_.getValue(CORNER) != CornerType.top_right) {
            p_225533_3_ = p_225533_3_.relative(p_225533_1_.getValue(FACING));
            p_225533_1_ = p_225533_2_.getBlockState(p_225533_3_);
            if (!p_225533_1_.is(this)) {
                return InteractionResult.CONSUME;
            }
        }
        if (((Boolean) p_225533_1_.getValue(OCCUPIED)).booleanValue()) {
            if (p_225533_1_.getValue(CORNER) == CornerType.top_left) {
                p_225533_3_ = p_225533_3_.relative(p_225533_1_.getValue(FACING).getClockWise());
                p_225533_1_ = p_225533_2_.getBlockState(p_225533_3_);
                if (!p_225533_1_.is(this)) {
                    return InteractionResult.CONSUME;
                }
            } else if (p_225533_1_.getValue(CORNER) == CornerType.top_right) {
                p_225533_3_ = p_225533_3_.relative(p_225533_1_.getValue(FACING).getCounterClockWise());
                p_225533_1_ = p_225533_2_.getBlockState(p_225533_3_);
                if (!p_225533_1_.is(this)) {
                    return InteractionResult.CONSUME;
                }
            }
        }
        if (!canSetSpawn(p_225533_2_)) {
            p_225533_2_.removeBlock(p_225533_3_, false);
            BlockPos lvt_7_1_ = p_225533_3_.relative(p_225533_1_.getValue(FACING).getOpposite());
            if (p_225533_2_.getBlockState(lvt_7_1_).is(this)) {
                p_225533_2_.removeBlock(lvt_7_1_, false);
            }
            p_225533_2_.explode((Entity) null, DamageSource.badRespawnPointExplosion(), (ExplosionDamageCalculator) null, p_225533_3_.getX() + 0.5d, p_225533_3_.getY() + 0.5d, p_225533_3_.getZ() + 0.5d, 5.0f, true, Explosion.BlockInteraction.DESTROY);
            return InteractionResult.SUCCESS;
        }
        if (((Boolean) p_225533_1_.getValue(OCCUPIED)).booleanValue()) {
            if (!kickVillagerOutOfBed(p_225533_2_, p_225533_3_)) {
                p_225533_4_.displayClientMessage(new TranslatableComponent("block.minecraft.bed.occupied"), true);
            }
            return InteractionResult.SUCCESS;
        }
        p_225533_4_.startSleepInBed(p_225533_3_).ifLeft(p_220173_1_ -> {
            if (p_220173_1_ != null) {
                p_225533_4_.displayClientMessage(p_220173_1_.getMessage(), true);
            }
        });
        return InteractionResult.SUCCESS;
    }

    public boolean isPathfindable(BlockState p_196266_1_, BlockGetter p_196266_2_, BlockPos p_196266_3_, PathComputationType p_196266_4_) {
        return false;
    }

    public static boolean canSetSpawn(Level p_235330_0_) {
        return p_235330_0_.dimensionType().bedWorks();
    }

    private boolean kickVillagerOutOfBed(Level p_226861_1_, BlockPos p_226861_2_) {
        List<Villager> lvt_3_1_ = p_226861_1_.m_6443_(Villager.class, new AABB(p_226861_2_), (v0) -> {
            return v0.isSleeping();
        });
        if (lvt_3_1_.isEmpty()) {
            return false;
        }
        lvt_3_1_.get(0).stopSleeping();
        return true;
    }

    public void fallOn(Level p_180658_1_, BlockState state, BlockPos p_180658_2_, Entity p_180658_3_, float p_180658_4_) {
        super.fallOn(p_180658_1_, state, p_180658_2_, p_180658_3_, p_180658_4_ * 0.5f);
    }

    public void updateEntityAfterFallOn(BlockGetter p_176216_1_, Entity p_176216_2_) {
        if (p_176216_2_.isSuppressingBounce()) {
            super.updateEntityAfterFallOn(p_176216_1_, p_176216_2_);
        } else {
            bounceUp(p_176216_2_);
        }
    }

    private void bounceUp(Entity p_226860_1_) {
        Vec3 lvt_2_1_ = p_226860_1_.getDeltaMovement();
        if (lvt_2_1_.y < 0.0d) {
            double lvt_3_1_ = p_226860_1_ instanceof LivingEntity ? 1.0d : 0.8d;
            p_226860_1_.setDeltaMovement(lvt_2_1_.x, (-lvt_2_1_.y) * 0.6600000262260437d * lvt_3_1_, lvt_2_1_.z);
        }
    }

    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> p_206840_1_) {
        p_206840_1_.add(new Property[]{CORNER, FACING, PART, OCCUPIED});
    }
}
