package net.minecraft.world.item.alchemy;

import java.util.AbstractList;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.network.chat.MutableComponent;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.effect.MobEffectUtil;
import com.mojang.datafixers.util.Pair;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.ai.attributes.Attribute;
import java.util.Map;
import net.minecraft.ChatFormatting;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.network.chat.Component;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.Registry;
import java.util.Iterator;
import net.minecraft.nbt.ListTag;
import javax.annotation.Nullable;
import net.minecraft.nbt.CompoundTag;
import com.google.common.collect.Lists;
import java.util.Collection;
import net.minecraft.world.effect.MobEffectInstance;
import java.util.List;
import net.minecraft.world.item.ItemStack;

public class PotionUtils {
    public static List<MobEffectInstance> getMobEffects(final ItemStack bki) {
        return getAllEffects(bki.getTag());
    }
    
    public static List<MobEffectInstance> getAllEffects(final Potion bmb, final Collection<MobEffectInstance> collection) {
        final List<MobEffectInstance> list3 = Lists.newArrayList();
        list3.addAll(bmb.getEffects());
        list3.addAll(collection);
        return list3;
    }
    
    public static List<MobEffectInstance> getAllEffects(@Nullable final CompoundTag le) {
        final List<MobEffectInstance> list2 = Lists.newArrayList();
        list2.addAll(getPotion(le).getEffects());
        getCustomEffects(le, list2);
        return list2;
    }
    
    public static List<MobEffectInstance> getCustomEffects(final ItemStack bki) {
        return getCustomEffects(bki.getTag());
    }
    
    public static List<MobEffectInstance> getCustomEffects(@Nullable final CompoundTag le) {
        final List<MobEffectInstance> list2 = Lists.newArrayList();
        getCustomEffects(le, list2);
        return list2;
    }
    
    public static void getCustomEffects(@Nullable final CompoundTag le, final List<MobEffectInstance> list) {
        if (le != null && le.contains("CustomPotionEffects", 9)) {
            final ListTag lk3 = le.getList("CustomPotionEffects", 10);
            for (int integer4 = 0; integer4 < lk3.size(); ++integer4) {
                final CompoundTag le2 = lk3.getCompound(integer4);
                final MobEffectInstance aog6 = MobEffectInstance.load(le2);
                if (aog6 != null) {
                    list.add(aog6);
                }
            }
        }
    }
    
    public static int getColor(final ItemStack bki) {
        final CompoundTag le2 = bki.getTag();
        if (le2 != null && le2.contains("CustomPotionColor", 99)) {
            return le2.getInt("CustomPotionColor");
        }
        return (getPotion(bki) == Potions.EMPTY) ? 16253176 : getColor(getMobEffects(bki));
    }
    
    public static int getColor(final Potion bmb) {
        return (bmb == Potions.EMPTY) ? 16253176 : getColor(bmb.getEffects());
    }
    
    public static int getColor(final Collection<MobEffectInstance> collection) {
        final int integer2 = 3694022;
        if (collection.isEmpty()) {
            return 3694022;
        }
        float float3 = 0.0f;
        float float4 = 0.0f;
        float float5 = 0.0f;
        int integer3 = 0;
        for (final MobEffectInstance aog8 : collection) {
            if (!aog8.isVisible()) {
                continue;
            }
            final int integer4 = aog8.getEffect().getColor();
            final int integer5 = aog8.getAmplifier() + 1;
            float3 += integer5 * (integer4 >> 16 & 0xFF) / 255.0f;
            float4 += integer5 * (integer4 >> 8 & 0xFF) / 255.0f;
            float5 += integer5 * (integer4 >> 0 & 0xFF) / 255.0f;
            integer3 += integer5;
        }
        if (integer3 == 0) {
            return 0;
        }
        float3 = float3 / integer3 * 255.0f;
        float4 = float4 / integer3 * 255.0f;
        float5 = float5 / integer3 * 255.0f;
        return (int)float3 << 16 | (int)float4 << 8 | (int)float5;
    }
    
    public static Potion getPotion(final ItemStack bki) {
        return getPotion(bki.getTag());
    }
    
    public static Potion getPotion(@Nullable final CompoundTag le) {
        if (le == null) {
            return Potions.EMPTY;
        }
        return Potion.byName(le.getString("Potion"));
    }
    
    public static ItemStack setPotion(final ItemStack bki, final Potion bmb) {
        final ResourceLocation uh3 = Registry.POTION.getKey(bmb);
        if (bmb == Potions.EMPTY) {
            bki.removeTagKey("Potion");
        }
        else {
            bki.getOrCreateTag().putString("Potion", uh3.toString());
        }
        return bki;
    }
    
    public static ItemStack setCustomEffects(final ItemStack bki, final Collection<MobEffectInstance> collection) {
        if (collection.isEmpty()) {
            return bki;
        }
        final CompoundTag le3 = bki.getOrCreateTag();
        final ListTag lk4 = le3.getList("CustomPotionEffects", 9);
        for (final MobEffectInstance aog6 : collection) {
            ((AbstractList<CompoundTag>)lk4).add(aog6.save(new CompoundTag()));
        }
        le3.put("CustomPotionEffects", lk4);
        return bki;
    }
    
    public static void addPotionTooltip(final ItemStack bki, final List<Component> list, final float float3) {
        final List<MobEffectInstance> list2 = getMobEffects(bki);
        final List<Pair<Attribute, AttributeModifier>> list3 = Lists.newArrayList();
        if (list2.isEmpty()) {
            list.add(new TranslatableComponent("effect.none").withStyle(ChatFormatting.GRAY));
        }
        else {
            for (final MobEffectInstance aog7 : list2) {
                final MutableComponent mx8 = new TranslatableComponent(aog7.getDescriptionId());
                final MobEffect aoe9 = aog7.getEffect();
                final Map<Attribute, AttributeModifier> map10 = aoe9.getAttributeModifiers();
                if (!map10.isEmpty()) {
                    for (final Map.Entry<Attribute, AttributeModifier> entry12 : map10.entrySet()) {
                        final AttributeModifier apv13 = entry12.getValue();
                        final AttributeModifier apv14 = new AttributeModifier(apv13.getName(), aoe9.getAttributeModifierValue(aog7.getAmplifier(), apv13), apv13.getOperation());
                        list3.add((Pair<Attribute, AttributeModifier>)new Pair(entry12.getKey(), apv14));
                    }
                }
                if (aog7.getAmplifier() > 0) {
                    mx8.append(" ").append(new TranslatableComponent("potion.potency." + aog7.getAmplifier()));
                }
                if (aog7.getDuration() > 20) {
                    mx8.append(" (").append(MobEffectUtil.formatDuration(aog7, float3)).append(")");
                }
                list.add(mx8.withStyle(aoe9.getCategory().getTooltipFormatting()));
            }
        }
        if (!list3.isEmpty()) {
            list.add(TextComponent.EMPTY);
            list.add(new TranslatableComponent("potion.whenDrank").withStyle(ChatFormatting.DARK_PURPLE));
            for (final Pair<Attribute, AttributeModifier> pair7 : list3) {
                final AttributeModifier apv15 = (AttributeModifier)pair7.getSecond();
                final double double9 = apv15.getAmount();
                double double10;
                if (apv15.getOperation() == AttributeModifier.Operation.MULTIPLY_BASE || apv15.getOperation() == AttributeModifier.Operation.MULTIPLY_TOTAL) {
                    double10 = apv15.getAmount() * 100.0;
                }
                else {
                    double10 = apv15.getAmount();
                }
                if (double9 > 0.0) {
                    list.add(new TranslatableComponent("attribute.modifier.plus." + apv15.getOperation().toValue(), new Object[] { ItemStack.ATTRIBUTE_MODIFIER_FORMAT.format(double10), new TranslatableComponent(((Attribute)pair7.getFirst()).getDescriptionId()) }).withStyle(ChatFormatting.BLUE));
                }
                else {
                    if (double9 >= 0.0) {
                        continue;
                    }
                    double10 *= -1.0;
                    list.add(new TranslatableComponent("attribute.modifier.take." + apv15.getOperation().toValue(), new Object[] { ItemStack.ATTRIBUTE_MODIFIER_FORMAT.format(double10), new TranslatableComponent(((Attribute)pair7.getFirst()).getDescriptionId()) }).withStyle(ChatFormatting.RED));
                }
            }
        }
    }
}
