package com.dairymoose.modernlife.items;

import com.dairymoose.entity.MotorboatEntity;
import java.util.List;
import java.util.function.Predicate;
import javax.annotation.Nullable;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntitySelector;
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.ClipContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.gameevent.GameEvent;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/* loaded from: outputsrg.jar:com/dairymoose/modernlife/items/MotorboatItem.class */
public class MotorboatItem extends Item {
    private static final Logger LOGGER = LogManager.getLogger();
    private static final Predicate<Entity> ENTITY_PREDICATE = EntitySelector.NO_SPECTATORS.and((v0) -> {
        return v0.isPickable();
    });

    public MotorboatItem(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 deploy"));
        float fuelLevel = 1.0f;
        if (itemStack.getTag() != null && itemStack.getTag().contains("FuelLevel")) {
            fuelLevel = itemStack.getTag().getFloat("FuelLevel");
        }
        String formattedFuel = String.format("%.1f", Float.valueOf(fuelLevel * 100.0f));
        text.add(new TextComponent("Remaining fuel: " + formattedFuel + "%"));
        text.add(new TextComponent("Refuel by using a gas can on the deployed boat"));
    }

    public InteractionResultHolder<ItemStack> use(Level p_40622_, Player p_40623_, InteractionHand p_40624_) {
        ItemStack itemStack = p_40623_.getItemInHand(p_40624_);
        BlockHitResult playerPOVHitResult = Item.getPlayerPOVHitResult(p_40622_, p_40623_, ClipContext.Fluid.ANY);
        if (playerPOVHitResult.getType() == HitResult.Type.MISS) {
            return InteractionResultHolder.pass(itemStack);
        }
        Vec3 vec3 = p_40623_.getViewVector(1.0f);
        List<Entity> list = p_40622_.getEntities(p_40623_, p_40623_.getBoundingBox().expandTowards(vec3.scale(5.0d)).inflate(1.0d), ENTITY_PREDICATE);
        if (!list.isEmpty()) {
            Vec3 vec31 = p_40623_.getEyePosition();
            for (Entity entity : list) {
                AABB aabb = entity.getBoundingBox().inflate(entity.getPickRadius());
                if (aabb.contains(vec31)) {
                    return InteractionResultHolder.pass(itemStack);
                }
            }
        }
        if (playerPOVHitResult.getType() == HitResult.Type.BLOCK) {
            Vec3 loc = playerPOVHitResult.getLocation();
            MotorboatEntity boat = new MotorboatEntity(p_40622_, loc.x, loc.y, loc.z);
            boat.setYRot(p_40623_.getYRot());
            if (!p_40622_.noCollision(boat, boat.getBoundingBox())) {
                return InteractionResultHolder.fail(itemStack);
            }
            if (!p_40622_.isClientSide) {
                p_40622_.m_7967_(boat);
                p_40622_.gameEvent(p_40623_, GameEvent.ENTITY_PLACE, new BlockPos(loc));
                if (!p_40623_.getAbilities().instabuild) {
                    itemStack.shrink(1);
                }
            }
            p_40623_.awardStat(Stats.ITEM_USED.get(this));
            if (itemStack.getTag() != null && itemStack.getTag().contains("FuelLevel")) {
                float fuelLevel = itemStack.getTag().getFloat("FuelLevel");
                boat.setSyncedFuelLevel(fuelLevel);
            }
            return InteractionResultHolder.sidedSuccess(itemStack, p_40622_.isClientSide());
        }
        return InteractionResultHolder.pass(itemStack);
    }
}
