package ac.ed.lurg;

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

public class ModelConfig {
	
	private Properties configFile;
	private static ModelConfig modelConfig;
	public static final String CONFIG_FILE = System.getProperty("CONFIG_FILE");
	
	private ModelConfig() {
		configFile = new Properties();
		try {
			System.out.println("Config. file is " + CONFIG_FILE);
			if (CONFIG_FILE != null)
				configFile.load(new FileInputStream(CONFIG_FILE));
		}
		catch (IOException e) {
			System.err.println("Problems reading config file");
			System.err.println(e.getMessage());
		}
	}
	
	private static ModelConfig getModelConfig() {
		if (modelConfig == null)
			modelConfig = new ModelConfig();

		return modelConfig;
	}
	
	private static String getProperty(String prop) {
		return getModelConfig().getProp(prop);
	}
	private static String getProperty(String prop, String defaultString) {
		String propValue = getProperty(prop);
		return propValue == null ? defaultString : propValue;
	}
	private String getProp(String prop) {
		return configFile.getProperty(prop);
	}
	
	private static Integer getIntProperty(String prop, Integer defaultInt) {
		Integer propValue = getModelConfig().getIntProp(prop);
		return propValue == null ? defaultInt : propValue;
	}
	private Integer getIntProp(String prop) {
		String v = configFile.getProperty(prop);
		return v==null ? null : Integer.valueOf(v);
	}
	
	@SuppressWarnings("unused")
	private static Long getLongProperty(String prop, Long defaultLong) {
		Long propValue = getModelConfig().getLongProp(prop);
		return propValue == null ? defaultLong : propValue;
	}
	private Long getLongProp(String prop) {
		String v = configFile.getProperty(prop);
		return v==null ? null : Long.valueOf(v);
	}
	
	private static Double getDoubleProperty(String prop, Double defaultDouble) {
		Double propValue = getModelConfig().getDoubleProp(prop);
		return propValue == null ? defaultDouble : propValue;
	}
	private Double getDoubleProp(String prop) {
		String v = configFile.getProperty(prop);
		return v==null ? null :  Double.valueOf(v);
	}
	
	private static Boolean getBooleanProperty(String prop, Boolean defaultBoolean) {
		return getModelConfig().getBooleanProp(prop, defaultBoolean);
	}
	private boolean getBooleanProp(String prop, Boolean defaultBoolean) {
		String v = configFile.getProperty(prop);
		return v==null ? defaultBoolean :  Boolean.valueOf(v);
	}

	public static final boolean SUPPRESS_STD_OUTPUT = getBooleanProperty("SUPPRESS_STD_OUTPUT", Boolean.FALSE);

	public static final String BASE_DIR = getProperty("OUTPUT_DIR", "/Users/peteralexander/Documents/R_Workspace");
	
	public static final String TEMP_DIR = BASE_DIR + File.separator + "temp";
	public static final String OUTPUT_DIR = BASE_DIR + File.separator + "output";
	public static final String DATA_DIR = BASE_DIR + File.separator + "data";
	public static final String GAMS_MODEL = BASE_DIR + File.separator + "UNPLUM/GAMS/IntExtOpt.gms";
	
	// Country (non-gridded) data
	public static final String DEMAND_CURVES_FILE = DATA_DIR + File.separator + "com_curves.csv";
	public static final String SSP_FILE = DATA_DIR + File.separator + "ssp.csv";	
	public static final String BASELINE_CONSUMP_FILE = DATA_DIR + File.separator + "base_consump.csv";
	public static final String COUNTRY_CODES_FILE = DATA_DIR + File.separator + "country_codes3.csv";
	public static final String COUNTRY_DATA_FILE = DATA_DIR + File.separator + "country_data.csv";
	public static final String COMMODITY_DATA_FILE = DATA_DIR + File.separator + "con_prod_c_and_m.csv";

	// yield data
	public static final String YIELD_DIR = "/Users/peteralexander/Documents/LURG/LPJ/tom-April2015";

	// Spatial (gridded) data
	public static final String SPATIAL_DATA_DIR = DATA_DIR; // + File.separator + "tinyTest";
	public static final String INITAL_LAND_COVER_DIR = SPATIAL_DATA_DIR + File.separator + "initLandUse";
	public static final String COUNTRY_BOUNDARY_FILE = SPATIAL_DATA_DIR + File.separator + "country_boundaries.asc";
	public static final String IRRIGATION_COST_FILE = SPATIAL_DATA_DIR + File.separator + "irrigation_cost.asc";;

	
	public static final int START_TIMESTEP = getIntProperty("START_TIMESTEP", 0);
	public static final int END_TIMESTEP = getIntProperty("END_TIMESTEP", 1);
	public static final int BASE_YEAR = getIntProperty("BASE_YEAR", 2010);
	
	public static final double MAX_IMPORT_CHANGE = getDoubleProperty("MAX_IMPORT_CHANGE", 0.2);
	
	public static final String SSP_SCENARIO = getProperty("SSP_SCENARIO", "SSP2_v9_130325");
	
}