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> act, final T object, final StatFormatter acs) {
        super(Stat.<T>buildName(act, object));
        this.type = act;
        this.formatter = acs;
        this.value = object;
    }
    
    public static <T> String buildName(final StatType<T> act, final T object) {
        return Stat.locationToKey(Registry.STAT_TYPE.getKey(act)) + ":" + Stat.locationToKey(act.getRegistry().getKey(object));
    }
    
    private static <T> String locationToKey(@Nullable final ResourceLocation uh) {
        return uh.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 + '}';
    }
}
