package net.minecraft.world.item.alchemy;

import java.util.AbstractList;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.world.effect.MobEffectUtil;
import net.minecraft.util.Tuple;
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 ben) {
        return getAllEffects(ben.getTag());
    }
    
    public static List<MobEffectInstance> getAllEffects(final Potion bgd, final Collection<MobEffectInstance> collection) {
        final List<MobEffectInstance> list3 = Lists.newArrayList();
        list3.addAll(bgd.getEffects());
        list3.addAll(collection);
        return list3;
    }
    
    public static List<MobEffectInstance> getAllEffects(@Nullable final CompoundTag jt) {
        final List<MobEffectInstance> list2 = Lists.newArrayList();
        list2.addAll(getPotion(jt).getEffects());
        getCustomEffects(jt, list2);
        return list2;
    }
    
    public static List<MobEffectInstance> getCustomEffects(final ItemStack ben) {
        return getCustomEffects(ben.getTag());
    }
    
    public static List<MobEffectInstance> getCustomEffects(@Nullable final CompoundTag jt) {
        final List<MobEffectInstance> list2 = Lists.newArrayList();
        getCustomEffects(jt, list2);
        return list2;
    }
    
    public static void getCustomEffects(@Nullable final CompoundTag jt, final List<MobEffectInstance> list) {
        if (jt != null && jt.contains("CustomPotionEffects", 9)) {
            final ListTag jz3 = jt.getList("CustomPotionEffects", 10);
            for (int integer4 = 0; integer4 < jz3.size(); ++integer4) {
                final CompoundTag jt2 = jz3.getCompound(integer4);
                final MobEffectInstance akk6 = MobEffectInstance.load(jt2);
                if (akk6 != null) {
                    list.add(akk6);
                }
            }
        }
    }
    
    public static int getColor(final ItemStack ben) {
        final CompoundTag jt2 = ben.getTag();
        if (jt2 != null && jt2.contains("CustomPotionColor", 99)) {
            return jt2.getInt("CustomPotionColor");
        }
        return (getPotion(ben) == Potions.EMPTY) ? 16253176 : getColor(getMobEffects(ben));
    }
    
    public static int getColor(final Potion bgd) {
        return (bgd == Potions.EMPTY) ? 16253176 : getColor(bgd.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 akk8 : collection) {
            if (!akk8.isVisible()) {
                continue;
            }
            final int integer4 = akk8.getEffect().getColor();
            final int integer5 = akk8.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 ben) {
        return getPotion(ben.getTag());
    }
    
    public static Potion getPotion(@Nullable final CompoundTag jt) {
        if (jt == null) {
            return Potions.EMPTY;
        }
        return Potion.byName(jt.getString("Potion"));
    }
    
    public static ItemStack setPotion(final ItemStack ben, final Potion bgd) {
        final ResourceLocation sm3 = Registry.POTION.getKey(bgd);
        if (bgd == Potions.EMPTY) {
            ben.removeTagKey("Potion");
        }
        else {
            ben.getOrCreateTag().putString("Potion", sm3.toString());
        }
        return ben;
    }
    
    public static ItemStack setCustomEffects(final ItemStack ben, final Collection<MobEffectInstance> collection) {
        if (collection.isEmpty()) {
            return ben;
        }
        final CompoundTag jt3 = ben.getOrCreateTag();
        final ListTag jz4 = jt3.getList("CustomPotionEffects", 9);
        for (final MobEffectInstance akk6 : collection) {
            ((AbstractList<CompoundTag>)jz4).add(akk6.save(new CompoundTag()));
        }
        jt3.put("CustomPotionEffects", jz4);
        return ben;
    }
    
    public static void addPotionTooltip(final ItemStack ben, final List<Component> list, final float float3) {
        final List<MobEffectInstance> list2 = getMobEffects(ben);
        final List<Tuple<String, AttributeModifier>> list3 = Lists.newArrayList();
        if (list2.isEmpty()) {
            list.add(new TranslatableComponent("effect.none", new Object[0]).withStyle(ChatFormatting.GRAY));
        }
        else {
            for (final MobEffectInstance akk7 : list2) {
                final Component lf8 = new TranslatableComponent(akk7.getDescriptionId(), new Object[0]);
                final MobEffect aki9 = akk7.getEffect();
                final Map<Attribute, AttributeModifier> map10 = aki9.getAttributeModifiers();
                if (!map10.isEmpty()) {
                    for (final Map.Entry<Attribute, AttributeModifier> entry12 : map10.entrySet()) {
                        final AttributeModifier als13 = entry12.getValue();
                        final AttributeModifier als14 = new AttributeModifier(als13.getName(), aki9.getAttributeModifierValue(akk7.getAmplifier(), als13), als13.getOperation());
                        list3.add(new Tuple<String, AttributeModifier>(entry12.getKey().getName(), als14));
                    }
                }
                if (akk7.getAmplifier() > 0) {
                    lf8.append(" ").append(new TranslatableComponent("potion.potency." + akk7.getAmplifier(), new Object[0]));
                }
                if (akk7.getDuration() > 20) {
                    lf8.append(" (").append(MobEffectUtil.formatDuration(akk7, float3)).append(")");
                }
                list.add(lf8.withStyle(aki9.getCategory().getTooltipFormatting()));
            }
        }
        if (!list3.isEmpty()) {
            list.add(new TextComponent(""));
            list.add(new TranslatableComponent("potion.whenDrank", new Object[0]).withStyle(ChatFormatting.DARK_PURPLE));
            for (final Tuple<String, AttributeModifier> abz7 : list3) {
                final AttributeModifier als15 = abz7.getB();
                final double double9 = als15.getAmount();
                double double10;
                if (als15.getOperation() == AttributeModifier.Operation.MULTIPLY_BASE || als15.getOperation() == AttributeModifier.Operation.MULTIPLY_TOTAL) {
                    double10 = als15.getAmount() * 100.0;
                }
                else {
                    double10 = als15.getAmount();
                }
                if (double9 > 0.0) {
                    list.add(new TranslatableComponent("attribute.modifier.plus." + als15.getOperation().toValue(), new Object[] { ItemStack.ATTRIBUTE_MODIFIER_FORMAT.format(double10), new TranslatableComponent("attribute.name." + abz7.getA(), new Object[0]) }).withStyle(ChatFormatting.BLUE));
                }
                else {
                    if (double9 >= 0.0) {
                        continue;
                    }
                    double10 *= -1.0;
                    list.add(new TranslatableComponent("attribute.modifier.take." + als15.getOperation().toValue(), new Object[] { ItemStack.ATTRIBUTE_MODIFIER_FORMAT.format(double10), new TranslatableComponent("attribute.name." + abz7.getA(), new Object[0]) }).withStyle(ChatFormatting.RED));
                }
            }
        }
    }
}
