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 ben, final Level bjw, final LivingEntity akz) {
        final Player ayj5 = (akz instanceof Player) ? ((Player)akz) : null;
        if (ayj5 instanceof ServerPlayer) {
            CriteriaTriggers.CONSUME_ITEM.trigger((ServerPlayer)ayj5, ben);
        }
        if (!bjw.isClientSide) {
            final List<MobEffectInstance> list6 = PotionUtils.getMobEffects(ben);
            for (final MobEffectInstance akk8 : list6) {
                if (akk8.getEffect().isInstantenous()) {
                    akk8.getEffect().applyInstantenousEffect(ayj5, ayj5, akz, akk8.getAmplifier(), 1.0);
                }
                else {
                    akz.addEffect(new MobEffectInstance(akk8));
                }
            }
        }
        if (ayj5 != null) {
            ayj5.awardStat(Stats.ITEM_USED.get(this));
            if (!ayj5.abilities.instabuild) {
                ben.shrink(1);
            }
        }
        if (ayj5 == null || !ayj5.abilities.instabuild) {
            if (ben.isEmpty()) {
                return new ItemStack(Items.GLASS_BOTTLE);
            }
            if (ayj5 != null) {
                ayj5.inventory.add(new ItemStack(Items.GLASS_BOTTLE));
            }
        }
        return ben;
    }
    
    @Override
    public int getUseDuration(final ItemStack ben) {
        return 32;
    }
    
    @Override
    public UseAnim getUseAnimation(final ItemStack ben) {
        return UseAnim.DRINK;
    }
    
    @Override
    public InteractionResultHolder<ItemStack> use(final Level bjw, final Player ayj, final InteractionHand ajk) {
        ayj.startUsingItem(ajk);
        return InteractionResultHolder.<ItemStack>success(ayj.getItemInHand(ajk));
    }
    
    @Override
    public String getDescriptionId(final ItemStack ben) {
        return PotionUtils.getPotion(ben).getName(this.getDescriptionId() + ".effect.");
    }
    
    @Override
    public void appendHoverText(final ItemStack ben, @Nullable final Level bjw, final List<Component> list, final TooltipFlag bfw) {
        PotionUtils.addPotionTooltip(ben, list, 1.0f);
    }
    
    @Override
    public boolean isFoil(final ItemStack ben) {
        return super.isFoil(ben) || !PotionUtils.getMobEffects(ben).isEmpty();
    }
    
    @Override
    public void fillItemCategory(final CreativeModeTab bdd, final NonNullList<ItemStack> fy) {
        if (this.allowdedIn(bdd)) {
            for (final Potion bgd5 : Registry.POTION) {
                if (bgd5 != Potions.EMPTY) {
                    fy.add(PotionUtils.setPotion(new ItemStack(this), bgd5));
                }
            }
        }
    }
}
