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

import java.io.File;
import java.io.FileWriter;

import com.google.gson.JsonObject;
import com.google.gson.internal.Streams;
import com.google.gson.stream.JsonWriter;

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

public class Settings
{
	File file;
	ISetting<?>[] settings;
	
	public Settings(File path, ISetting<?>[] settings)
	{
		file = path;
		this.settings = settings;
	}
	
	public void load()
	{
		JsonObject obj = JsonUtil.loadFile(file);
		for(int i = 0,m=settings.length;i<m;i++)
		{
			settings[i].read(obj);
		}
	}
	
	public void save()
	{
		JsonObject obj = new JsonObject();
		for(int i = 0,m=settings.length;i<m;i++)
		{
			settings[i].write(obj);
		}
		try(JsonWriter writer = new JsonWriter(new FileWriter(file)))
		{
			writer.setIndent(" ");
			Streams.write(obj, writer);
			writer.flush();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
}
