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

import com.google.common.collect.Maps;
import javax.annotation.Nullable;
import java.util.function.Consumer;
import java.util.UUID;
import net.minecraft.core.Registry;
import com.google.common.collect.ImmutableMap;
import java.util.Map;

public class AttributeSupplier {
    private final Map<Attribute, AttributeInstance> instances;
    
    public AttributeSupplier(final Map<Attribute, AttributeInstance> map) {
        this.instances = ImmutableMap.copyOf(map);
    }
    
    private AttributeInstance getAttributeInstance(final Attribute aps) {
        final AttributeInstance apt3 = this.instances.get(aps);
        if (apt3 == null) {
            throw new IllegalArgumentException("Can't find attribute " + Registry.ATTRIBUTE.getKey(aps));
        }
        return apt3;
    }
    
    public double getValue(final Attribute aps) {
        return this.getAttributeInstance(aps).getValue();
    }
    
    public double getBaseValue(final Attribute aps) {
        return this.getAttributeInstance(aps).getBaseValue();
    }
    
    public double getModifierValue(final Attribute aps, final UUID uUID) {
        final AttributeModifier apv4 = this.getAttributeInstance(aps).getModifier(uUID);
        if (apv4 == null) {
            throw new IllegalArgumentException("Can't find modifier " + uUID + " on attribute " + Registry.ATTRIBUTE.getKey(aps));
        }
        return apv4.getAmount();
    }
    
    @Nullable
    public AttributeInstance createInstance(final Consumer<AttributeInstance> consumer, final Attribute aps) {
        final AttributeInstance apt4 = this.instances.get(aps);
        if (apt4 == null) {
            return null;
        }
        final AttributeInstance apt5 = new AttributeInstance(aps, consumer);
        apt5.replaceFrom(apt4);
        return apt5;
    }
    
    public static Builder builder() {
        return new Builder();
    }
    
    public boolean hasAttribute(final Attribute aps) {
        return this.instances.containsKey(aps);
    }
    
    public boolean hasModifier(final Attribute aps, final UUID uUID) {
        final AttributeInstance apt4 = this.instances.get(aps);
        return apt4 != null && apt4.getModifier(uUID) != null;
    }
    
    public static class Builder {
        private final Map<Attribute, AttributeInstance> builder;
        private boolean instanceFrozen;
        
        public Builder() {
            this.builder = Maps.newHashMap();
        }
        
        private AttributeInstance create(final Attribute aps) {
            final UnsupportedOperationException ex;
            final AttributeInstance apt3 = new AttributeInstance(aps, apt -> {
                if (this.instanceFrozen) {
                    new UnsupportedOperationException("Tried to change value for default attribute instance: " + Registry.ATTRIBUTE.getKey(aps));
                    throw ex;
                }
                else {
                    return;
                }
            });
            this.builder.put(aps, apt3);
            return apt3;
        }
        
        public Builder add(final Attribute aps) {
            this.create(aps);
            return this;
        }
        
        public Builder add(final Attribute aps, final double double2) {
            final AttributeInstance apt5 = this.create(aps);
            apt5.setBaseValue(double2);
            return this;
        }
        
        public AttributeSupplier build() {
            this.instanceFrozen = true;
            return new AttributeSupplier(this.builder);
        }
    }
}
