package speiger.src.coreengine.utils.io.settings;

import com.google.gson.JsonObject;

import speiger.src.coreengine.utils.helpers.JsonUtil;

public class IntSetting extends BaseSetting<IntSetting>
{
	final int defaultValue;
	int value;
	
	public IntSetting(String id, int defaultValue)
	{
		super(id);
		this.defaultValue = defaultValue;
		value = defaultValue;
	}

	@Override
	public boolean isDefault()
	{
		return value == defaultValue;
	}
	
	@Override
	public void setDefault()
	{
		if(value != defaultValue)
		{
			value = defaultValue;
			notifyListeners();
		}
	}
	
	public int getValue()
	{
		return value;
	}
	
	public int getDefaultValue()
	{
		return defaultValue;
	}
	
	public IntSetting setValue(int value)
	{
		if(this.value != value)
		{
			this.value = value;
			notifyListeners();
		}
		return this;
	}
	
	@Override
	public void write(JsonObject object)
	{
		object.addProperty(id, value);
	}

	@Override
	public void read(JsonObject object)
	{
		value = JsonUtil.getOrDefault(object, id, defaultValue);
	}
}
