package net.mkonrad.synes.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import net.mkonrad.synes.util.DbgTools;
import net.mkonrad.synes.util.ErrTools;

/**
 * //TODO: Rename as ConfigLoader and load properties into new static class Config
 * 
 * @author mako
 *
 */
public class Config extends Properties
{
	private static String CONFIG_RES = "./config/config.properties";
	private static String DEFAULTS_RES = "./config/config.defaults";
	
	private Properties _defaults = null;
	
	public Config()
	{
		loadDefaults();
		loadProperties();
	}
	
	public String getCfgValue(String key)
	{
		String defValue = _defaults.getProperty(key);
		
		if (defValue == null)
			ErrTools.die("Invalid property: " + key);
		
		return getProperty(key, defValue).toLowerCase();
	}
	
  private void loadProperties()
  {
  	loadAsResource(CONFIG_RES, this);
  }
  
  private void loadDefaults()
  {
  	_defaults = new Properties();
  	loadAsResource(DEFAULTS_RES, _defaults);
  }
  
  private void loadAsResource(String res, Properties toObj)
  {
  	try
  	{
  		toObj.load(new FileReader(res));
  		//in.close();
  		DbgTools.print("Loaded properties from " + res);
  	}
	  catch (IOException e)
	  {
	  	ErrTools.die("Error loading properties from " + res);
		}
  }
}
