package ee.ut.ga.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;

public class PropertyUtil {
  public static Logger log = Logger.getLogger(PropertyUtil.class);
  
  public final static String DATASET = "data";
  public final static String INITIAL_PWMS = "initial";
  public final static String LOGGING = "log";
  public final static String PAM_TO_ANALIZE = "pam";
  public final static String PAM_RESULT = "result";
  
  public final static String FITNESS_FUNCTION = "ff";
  
  public final static String GOOD_ALL = "good.all";
  public final static String POPULATION_SIZE = "population.size";
  public final static String BEST_TO_LEAVE = "parents.to.leave";
  public final static String PWM_MIN_LENGTH = "pwm.min.length";
  
  public final static String A = "a";
  public final static String T = "t";
  public final static String G = "g";
  public final static String C = "c";
  
  /**
   * Loads properties from file. 
   */
  public static Properties loadProperties(String path) {
    Properties properties = new Properties();
    try {
      properties.load(new FileInputStream(new File(path)));
    } catch (IOException ie) {
      log.debug("Error reading file:" + path);
    }
    return properties;
  }
  
  /**
   * Loads priors from properties. 
   */
  public static Map loadPriors(Properties properties) throws Exception {
    Map priors = new HashMap();
    String[] priorProperties = { A, T, G, C };
    
    for (int i = 0; i < priorProperties.length; i++) {
      String prior = properties.getProperty(priorProperties[i]);
      if (prior == null || prior.equals(""))
        throw new Exception("Property " + priorProperties[i] + " is not set!");
      
      priors.put(priorProperties[i].toUpperCase(), new BigDecimal(prior));
    }
    
    return priors;
  }
}
