package com.dairymoose.modernlife.blocks;

import com.dairymoose.modernlife.core.CustomBlocks;
import com.dairymoose.modernlife.tileentities.MicrowaveBlockEntity;
import com.dairymoose.modernlife.util.ModernLifeUtil;
import java.util.Optional;
import java.util.stream.Stream;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.Containers;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.SimpleContainer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.item.crafting.CampfireCookingRecipe;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
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.BooleanProperty;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.BooleanOp;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/MicrowaveBlock.class */
public class MicrowaveBlock extends StandardHorizontalBlock implements EntityBlock {
    protected static final VoxelShape SHAPE_NORTH = (VoxelShape) Stream.of(Block.box(1.0d, 0.0d, 2.0d, 15.0d, 10.0d, 14.0d)).reduce((v1, v2) -> {
        return Shapes.join(v1, v2, BooleanOp.OR);
    }).get();
    protected static final VoxelShape SHAPE_EAST = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_NORTH);
    protected static final VoxelShape SHAPE_SOUTH = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_EAST);
    protected static final VoxelShape SHAPE_WEST = ModernLifeUtil.RotateVoxelShapeClockwise(SHAPE_SOUTH);
    public static final BooleanProperty OPEN_DOOR = BooleanProperty.create("open_door");

    public MicrowaveBlock(Properties p_i48377_1_) {
        super(p_i48377_1_);
        registerDefaultState((BlockState) super.defaultBlockState().setValue(OPEN_DOOR, false));
    }

    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
        return ModernLifeUtil.createTickerHelper(type, MicrowaveBlockEntity.MICROWAVE, MicrowaveBlockEntity::tick);
    }

    public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
        MicrowaveBlockEntity toReturn = (MicrowaveBlockEntity) MicrowaveBlockEntity.MICROWAVE.create(pos, state);
        return toReturn;
    }

    public void onRemove(BlockState newBlockState, Level world, BlockPos pos, BlockState oldBlockState, boolean b) {
        if (!newBlockState.is(oldBlockState.getBlock())) {
            BlockEntity tileEntity = world.getBlockEntity(pos);
            if (tileEntity instanceof MicrowaveBlockEntity) {
                Containers.dropContents(world, pos, (MicrowaveBlockEntity) tileEntity);
                world.updateNeighbourForOutputSignal(pos, this);
            }
            super.onRemove(newBlockState, world, pos, oldBlockState, b);
        }
    }

    public static int calculateTime(Optional<CampfireCookingRecipe> optionalCookingRecipe) {
        if (optionalCookingRecipe.isPresent()) {
            return optionalCookingRecipe.get().m_43753_() / 9;
        }
        return 0;
    }

    public InteractionResult use(BlockState blockState, Level world, BlockPos blockPos, Player player, InteractionHand hand, BlockHitResult rayTrace) {
        if (!world.isClientSide) {
            if (blockState.is(CustomBlocks.BLOCK_MICROWAVE.get()) && !((Boolean) blockState.getValue(OPEN_DOOR)).booleanValue()) {
                world.setBlockAndUpdate(blockPos, (BlockState) blockState.setValue(OPEN_DOOR, true));
            } else {
                boolean tookItem = false;
                ItemStack itemStack = player.getItemInHand(hand);
                if (!itemStack.isEmpty()) {
                    Optional<CampfireCookingRecipe> optional = world.getRecipeManager().getRecipeFor(RecipeType.CAMPFIRE_COOKING, new SimpleContainer(new ItemStack[]{itemStack}), world);
                    if (optional.isPresent()) {
                        BlockEntity tileEntity = world.getBlockEntity(blockPos);
                        if (tileEntity instanceof MicrowaveBlockEntity) {
                            MicrowaveBlockEntity microwaveBlockEntity = (MicrowaveBlockEntity) tileEntity;
                            if (microwaveBlockEntity.isEmpty()) {
                                ItemStack oneItem = itemStack.split(1);
                                microwaveBlockEntity.cookItem(oneItem, calculateTime(optional));
                            }
                            microwaveBlockEntity.setChanged();
                        }
                    }
                } else {
                    BlockEntity tileEntity2 = world.getBlockEntity(blockPos);
                    if (tileEntity2 instanceof MicrowaveBlockEntity) {
                        MicrowaveBlockEntity microwaveBlockEntity2 = (MicrowaveBlockEntity) tileEntity2;
                        if (!microwaveBlockEntity2.isEmpty() && player.isSteppingCarefully()) {
                            tookItem = true;
                            player.addItem(microwaveBlockEntity2.getItem(0).copy());
                            microwaveBlockEntity2.setItem(0, ItemStack.EMPTY);
                        }
                        microwaveBlockEntity2.setChanged();
                    }
                }
                if (!tookItem) {
                    world.setBlockAndUpdate(blockPos, (BlockState) blockState.setValue(OPEN_DOOR, false));
                }
                world.sendBlockUpdated(blockPos, blockState, blockState, 2);
            }
        }
        return InteractionResult.CONSUME;
    }

    @Override // com.dairymoose.modernlife.blocks.StandardHorizontalBlock
    protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
        builder.add(new Property[]{HorizontalDirectionalBlock.FACING, OPEN_DOOR});
    }

    /* renamed from: com.dairymoose.modernlife.blocks.MicrowaveBlock$1 */
    /* loaded from: outputsrg.jar:com/dairymoose/modernlife/blocks/MicrowaveBlock$1.class */
    static /* synthetic */ class C00351 {
        static final /* synthetic */ int[] $SwitchMap$net$minecraft$core$Direction = new int[Direction.values().length];

        static {
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.NORTH.ordinal()] = 1;
            } catch (NoSuchFieldError e) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.SOUTH.ordinal()] = 2;
            } catch (NoSuchFieldError e2) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.EAST.ordinal()] = 3;
            } catch (NoSuchFieldError e3) {
            }
            try {
                $SwitchMap$net$minecraft$core$Direction[Direction.WEST.ordinal()] = 4;
            } catch (NoSuchFieldError e4) {
            }
        }
    }

    public VoxelShape getShape(BlockState bs, BlockGetter reader, BlockPos pos, CollisionContext sel) {
        switch (C00351.$SwitchMap$net$minecraft$core$Direction[bs.getValue(HorizontalDirectionalBlock.FACING).ordinal()]) {
            case 1:
                return SHAPE_NORTH;
            case 2:
                return SHAPE_SOUTH;
            case 3:
                return SHAPE_EAST;
            case 4:
                return SHAPE_WEST;
            default:
                return SHAPE_NORTH;
        }
    }

    @Override // com.dairymoose.modernlife.blocks.StandardHorizontalBlock
    public BlockState getStateForPlacement(BlockPlaceContext context) {
        return (BlockState) defaultBlockState().setValue(HorizontalDirectionalBlock.FACING, context.getHorizontalDirection());
    }
}
