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

import com.google.gson.JsonObject;

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

public class BooleanSetting extends BaseSetting<BooleanSetting>
{
	final boolean defaultValue;
	boolean value;
	
	public BooleanSetting(String id, boolean 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 BooleanSetting setValue(boolean value)
	{
		if(this.value != value)
		{
			this.value = value;
			notifyListeners();
		}
		return this;
	}
	
	public boolean getDefaultValue()
	{
		return defaultValue;
	}
	
	public boolean getValue()
	{
		return value;
	}
	
	@Override
	public void write(JsonObject object)
	{
		object.addProperty(id, value);
	}
	
	@Override
	public void read(JsonObject object)
	{
		value = JsonUtil.getOrDefault(object, id, defaultValue);
	}
}
