package net.minecraft.world.entity.ai.attributes;

import java.util.Iterator;
import java.util.Collection;
import javax.annotation.Nullable;
import com.google.common.collect.HashMultimap;
import net.minecraft.util.InsensitiveStringMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import java.util.Map;

public abstract class BaseAttributeMap {
    protected final Map<Attribute, AttributeInstance> attributesByObject;
    protected final Map<String, AttributeInstance> attributesByName;
    protected final Multimap<Attribute, Attribute> descendantsByParent;
    
    public BaseAttributeMap() {
        this.attributesByObject = Maps.newHashMap();
        this.attributesByName = new InsensitiveStringMap<AttributeInstance>();
        this.descendantsByParent = HashMultimap.create();
    }
    
    @Nullable
    public AttributeInstance getInstance(final Attribute alq) {
        return this.attributesByObject.get(alq);
    }
    
    @Nullable
    public AttributeInstance getInstance(final String string) {
        return this.attributesByName.get(string);
    }
    
    public AttributeInstance registerAttribute(final Attribute alq) {
        if (this.attributesByName.containsKey(alq.getName())) {
            throw new IllegalArgumentException("Attribute is already registered!");
        }
        final AttributeInstance alr3 = this.createAttributeInstance(alq);
        this.attributesByName.put(alq.getName(), alr3);
        this.attributesByObject.put(alq, alr3);
        for (Attribute alq2 = alq.getParentAttribute(); alq2 != null; alq2 = alq2.getParentAttribute()) {
            this.descendantsByParent.put(alq2, alq);
        }
        return alr3;
    }
    
    protected abstract AttributeInstance createAttributeInstance(final Attribute alq);
    
    public Collection<AttributeInstance> getAttributes() {
        return this.attributesByName.values();
    }
    
    public void onAttributeModified(final AttributeInstance alr) {
    }
    
    public void removeAttributeModifiers(final Multimap<String, AttributeModifier> multimap) {
        for (final Map.Entry<String, AttributeModifier> entry4 : multimap.entries()) {
            final AttributeInstance alr5 = this.getInstance(entry4.getKey());
            if (alr5 != null) {
                alr5.removeModifier(entry4.getValue());
            }
        }
    }
    
    public void addAttributeModifiers(final Multimap<String, AttributeModifier> multimap) {
        for (final Map.Entry<String, AttributeModifier> entry4 : multimap.entries()) {
            final AttributeInstance alr5 = this.getInstance(entry4.getKey());
            if (alr5 != null) {
                alr5.removeModifier(entry4.getValue());
                alr5.addModifier(entry4.getValue());
            }
        }
    }
    
    public void assignValues(final BaseAttributeMap alu) {
        final AttributeInstance alr2;
        this.getAttributes().forEach(alr -> {
            alr2 = alu.getInstance(alr.getAttribute());
            if (alr2 != null) {
                alr.copyFrom(alr2);
            }
        });
    }
}
