package common.attributes;

import java.util.Map;

import common.collect.Maps;
import common.util.ExtMath;

public class Attribute
{
	private static final Map<String, Attribute> ATTRIBUTES = Maps.newHashMap();
	
    private final String name;
    private final String display;
    private final double defValue;
    private final double minValue;
    private final double maxValue;
    private final boolean shouldWatch;
    
    public static Attribute getAttribute(String name) {
    	return ATTRIBUTES.get(name);
    }

    public Attribute(String name, String display, double def, double min, double max, boolean watch)
    {
        this.name = name;
        this.display = display;
        this.defValue = def;
        if(name == null)
            throw new IllegalArgumentException("Name cannot be null!");
        this.minValue = min;
        this.maxValue = max;
        this.shouldWatch = watch;

        if (min > max)
        {
            throw new IllegalArgumentException("Minimum value cannot be bigger than maximum value!");
        }
        else if (def < min)
        {
            throw new IllegalArgumentException("Default value cannot be lower than minimum value!");
        }
        else if (def > max)
        {
            throw new IllegalArgumentException("Default value cannot be bigger than maximum value!");
        }
        
        ATTRIBUTES.put(name, this);
    }

    public String getUnlocalizedName()
    {
        return this.name;
    }

    public String getDisplayName()
    {
        return this.display;
    }

    public double getDefaultValue()
    {
        return this.defValue;
    }

    public boolean getShouldWatch()
    {
        return this.shouldWatch;
    }

    public double clampValue(double value)
    {
        return ExtMath.clampd(value, this.minValue, this.maxValue);
    }

    public int hashCode()
    {
        return this.name.hashCode();
    }

    public boolean equals(Object obj)
    {
        return obj instanceof Attribute && this.name.equals(((Attribute)obj).getUnlocalizedName());
    }
}
