package net.minecraft.world.item;

import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.core.Registry;
import net.minecraft.core.NonNullList;
import net.minecraft.network.chat.Component;
import javax.annotation.Nullable;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.InteractionHand;
import java.util.Iterator;
import java.util.List;
import net.minecraft.world.level.ItemLike;
import net.minecraft.stats.Stat;
import net.minecraft.stats.Stats;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.advancements.CriteriaTriggers;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.level.Level;
import net.minecraft.world.item.alchemy.PotionUtils;
import net.minecraft.world.item.alchemy.Potions;

public class PotionItem extends Item {
    public PotionItem(final Properties a) {
        super(a);
    }
    
    @Override
    public ItemStack getDefaultInstance() {
        return PotionUtils.setPotion(super.getDefaultInstance(), Potions.WATER);
    }
    
    @Override
    public ItemStack finishUsingItem(final ItemStack bki, final Level bqb, final LivingEntity aoy) {
        final Player bec5 = (aoy instanceof Player) ? ((Player)aoy) : null;
        if (bec5 instanceof ServerPlayer) {
            CriteriaTriggers.CONSUME_ITEM.trigger((ServerPlayer)bec5, bki);
        }
        if (!bqb.isClientSide) {
            final List<MobEffectInstance> list6 = PotionUtils.getMobEffects(bki);
            for (final MobEffectInstance aog8 : list6) {
                if (aog8.getEffect().isInstantenous()) {
                    aog8.getEffect().applyInstantenousEffect(bec5, bec5, aoy, aog8.getAmplifier(), 1.0);
                }
                else {
                    aoy.addEffect(new MobEffectInstance(aog8));
                }
            }
        }
        if (bec5 != null) {
            bec5.awardStat(Stats.ITEM_USED.get(this));
            if (!bec5.abilities.instabuild) {
                bki.shrink(1);
            }
        }
        if (bec5 == null || !bec5.abilities.instabuild) {
            if (bki.isEmpty()) {
                return new ItemStack(Items.GLASS_BOTTLE);
            }
            if (bec5 != null) {
                bec5.inventory.add(new ItemStack(Items.GLASS_BOTTLE));
            }
        }
        return bki;
    }
    
    @Override
    public int getUseDuration(final ItemStack bki) {
        return 32;
    }
    
    @Override
    public UseAnim getUseAnimation(final ItemStack bki) {
        return UseAnim.DRINK;
    }
    
    @Override
    public InteractionResultHolder<ItemStack> use(final Level bqb, final Player bec, final InteractionHand anf) {
        return ItemUtils.useDrink(bqb, bec, anf);
    }
    
    @Override
    public String getDescriptionId(final ItemStack bki) {
        return PotionUtils.getPotion(bki).getName(this.getDescriptionId() + ".effect.");
    }
    
    @Override
    public void appendHoverText(final ItemStack bki, @Nullable final Level bqb, final List<Component> list, final TooltipFlag bls) {
        PotionUtils.addPotionTooltip(bki, list, 1.0f);
    }
    
    @Override
    public boolean isFoil(final ItemStack bki) {
        return super.isFoil(bki) || !PotionUtils.getMobEffects(bki).isEmpty();
    }
    
    @Override
    public void fillItemCategory(final CreativeModeTab biy, final NonNullList<ItemStack> gi) {
        if (this.allowdedIn(biy)) {
            for (final Potion bmb5 : Registry.POTION) {
                if (bmb5 != Potions.EMPTY) {
                    gi.add(PotionUtils.setPotion(new ItemStack(this), bmb5));
                }
            }
        }
    }
}
