package com.dairymoose.modernlife.items;

import com.dairymoose.modernlife.core.CustomBlocks;
import java.util.List;
import javax.annotation.Nullable;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.level.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/items/BatteryItem.class */
public class BatteryItem extends Item {
    private static final Logger LOGGER = LogManager.getLogger();

    public BatteryItem(Properties p_i48530_4_) {
        super(p_i48530_4_);
    }

    public void appendHoverText(ItemStack itemStack, @Nullable Level world, List<Component> text, TooltipFlag flag) {
        text.add(new TextComponent("Right click to recharge flashlight (battery will be consumed)"));
    }

    public InteractionResultHolder<ItemStack> use(Level world, Player player, InteractionHand hand) {
        ItemStack battery = player.getItemInHand(hand);
        if (!world.isClientSide && battery.is(CustomBlocks.ITEM_BATTERY.get()) && player != null && player.getInventory() != null) {
            Inventory inven = player.getInventory();
            ItemStack lowestDurabilityFlashlight = null;
            for (int i = 0; i < inven.getContainerSize(); i++) {
                ItemStack inventoryItem = inven.getItem(i);
                if (inventoryItem.is(CustomBlocks.ITEM_FLASHLIGHT.get())) {
                    if (lowestDurabilityFlashlight == null) {
                        lowestDurabilityFlashlight = inventoryItem;
                    } else if (inventoryItem.getDamageValue() > lowestDurabilityFlashlight.getDamageValue()) {
                        lowestDurabilityFlashlight = inventoryItem;
                    }
                }
            }
            if (lowestDurabilityFlashlight != null && lowestDurabilityFlashlight.isDamaged()) {
                battery.shrink(1);
                lowestDurabilityFlashlight.setDamageValue(0);
            }
        }
        return InteractionResultHolder.consume(battery);
    }
}
