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

import org.apache.logging.log4j.LogManager;
import javax.annotation.Nullable;
import net.minecraft.nbt.CompoundTag;
import java.util.Objects;
import java.util.Random;
import net.minecraft.util.Mth;
import io.netty.util.internal.ThreadLocalRandom;
import java.util.UUID;
import java.util.function.Supplier;
import org.apache.logging.log4j.Logger;

public class AttributeModifier {
    private static final Logger LOGGER;
    private final double amount;
    private final Operation operation;
    private final Supplier<String> nameGetter;
    private final UUID id;
    
    public AttributeModifier(final String string, final double double2, final Operation a) {
        this(Mth.createInsecureUUID((Random)ThreadLocalRandom.current()), () -> string, double2, a);
    }
    
    public AttributeModifier(final UUID uUID, final String string, final double double3, final Operation a) {
        this(uUID, () -> string, double3, a);
    }
    
    public AttributeModifier(final UUID uUID, final Supplier<String> supplier, final double double3, final Operation a) {
        this.id = uUID;
        this.nameGetter = supplier;
        this.amount = double3;
        this.operation = a;
    }
    
    public UUID getId() {
        return this.id;
    }
    
    public String getName() {
        return this.nameGetter.get();
    }
    
    public Operation getOperation() {
        return this.operation;
    }
    
    public double getAmount() {
        return this.amount;
    }
    
    @Override
    public boolean equals(final Object object) {
        if (this == object) {
            return true;
        }
        if (object == null || this.getClass() != object.getClass()) {
            return false;
        }
        final AttributeModifier apv3 = (AttributeModifier)object;
        return Objects.equals(this.id, apv3.id);
    }
    
    @Override
    public int hashCode() {
        return this.id.hashCode();
    }
    
    @Override
    public String toString() {
        return "AttributeModifier{amount=" + this.amount + ", operation=" + this.operation + ", name='" + this.nameGetter.get() + '\'' + ", id=" + this.id + '}';
    }
    
    public CompoundTag save() {
        final CompoundTag le2 = new CompoundTag();
        le2.putString("Name", this.getName());
        le2.putDouble("Amount", this.amount);
        le2.putInt("Operation", this.operation.toValue());
        le2.putUUID("UUID", this.id);
        return le2;
    }
    
    @Nullable
    public static AttributeModifier load(final CompoundTag le) {
        try {
            final UUID uUID2 = le.getUUID("UUID");
            final Operation a3 = Operation.fromValue(le.getInt("Operation"));
            return new AttributeModifier(uUID2, le.getString("Name"), le.getDouble("Amount"), a3);
        }
        catch (Exception exception2) {
            AttributeModifier.LOGGER.warn("Unable to create attribute: {}", exception2.getMessage());
            return null;
        }
    }
    
    static {
        LOGGER = LogManager.getLogger();
    }
    
    public enum Operation {
        ADDITION(0), 
        MULTIPLY_BASE(1), 
        MULTIPLY_TOTAL(2);
        
        private static final Operation[] OPERATIONS;
        private final int value;
        
        private Operation(final int integer3) {
            this.value = integer3;
        }
        
        public int toValue() {
            return this.value;
        }
        
        public static Operation fromValue(final int integer) {
            if (integer < 0 || integer >= Operation.OPERATIONS.length) {
                throw new IllegalArgumentException("No operation with value " + integer);
            }
            return Operation.OPERATIONS[integer];
        }
        
        static {
            OPERATIONS = new Operation[] { Operation.ADDITION, Operation.MULTIPLY_BASE, Operation.MULTIPLY_TOTAL };
        }
    }
}
