package net.minecraft.stats;

import java.util.Objects;
import javax.annotation.Nullable;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.core.Registry;
import net.minecraft.world.scores.criteria.ObjectiveCriteria;

public class Stat<T> extends ObjectiveCriteria {
    private final StatFormatter formatter;
    private final T value;
    private final StatType<T> type;
    
    protected Stat(final StatType<T> aaq, final T object, final StatFormatter aap) {
        super(Stat.<T>buildName(aaq, object));
        this.type = aaq;
        this.formatter = aap;
        this.value = object;
    }
    
    public static <T> String buildName(final StatType<T> aaq, final T object) {
        return Stat.locationToKey(Registry.STAT_TYPE.getKey(aaq)) + ":" + Stat.locationToKey(aaq.getRegistry().getKey(object));
    }
    
    private static <T> String locationToKey(@Nullable final ResourceLocation sm) {
        return sm.toString().replace(':', '.');
    }
    
    public StatType<T> getType() {
        return this.type;
    }
    
    public T getValue() {
        return this.value;
    }
    
    public String format(final int integer) {
        return this.formatter.format(integer);
    }
    
    @Override
    public boolean equals(final Object object) {
        return this == object || (object instanceof Stat && Objects.equals(this.getName(), ((Stat)object).getName()));
    }
    
    @Override
    public int hashCode() {
        return this.getName().hashCode();
    }
    
    @Override
    public String toString() {
        return "Stat{name=" + this.getName() + ", formatter=" + this.formatter + '}';
    }
}
