package seed.minerva;

import java.io.*;
import java.util.Properties;

/** Provides settings file lookup/storage.
 *

 */
public class MinervaSettings extends SettingsManager {

	private static MinervaSettings instance;
	
	protected MinervaSettings() {
		super("minerva");
	}	
	
	public static MinervaSettings instance() {
		if (instance == null) {
			instance = new MinervaSettings();
		}
		
		return instance;
	}

	/* Really common settings requests are below: */
	
	
	/** @return apth for tests outputs (typically want volatile storage)
	 * Default is $TEMP/minerva/tests/
	 */	
	public static String getTestsOutputPath(){
		String outPath = instance().getProperty(
				"Tests.OutputPath", 
				System.getProperty("java.io.tmpdir") + "/minerva/tests/");
		outPath += "/"; //just make sure
		(new File(outPath)).mkdirs();
		return outPath;
	}
	
	/** @return path for minera applications results (data, svgs etc)
	 * Default is $TEMP/minerva/results/
	 */
	public static String getAppsOutputPath(){
		String outPath = instance().getProperty(
				"minerva.apps.resultpath", 
				System.getProperty("java.io.tmpdir") + "/minera/results/");
		outPath += "/"; //just make sure
		(new File(outPath)).mkdirs();
		return outPath;
	}
	
	/** @return path for minera validation results (mostly metadata info for 
	 * different validation runs, data stored elsewhere)
	 * Default is $TEMP/minerva/validation-runs/
	 */
	public static String getValidationRunsRootPath(){
		String outPath = instance().getProperty(
				"minerva.validation.path", 
				System.getProperty("java.io.tmpdir") + "/minera/validation-runs/");
		outPath += "/"; //just make sure
		(new File(outPath)).mkdirs();
		return outPath;
	}

	/** @return path for data signals cache (JPFs PPFs etc)
	 * Default is $TEMP/DataSignalCache/|
	 */
	public static String getDataSignalsCachePath(){
		return instance().getProperty(
				"DataSignals.CachePath",
				System.getProperty("java.io.tmpdir") + "/minerva/DataSignalCache");		
	}
	
	/** @return path for magnetics cache (Reponses of B and A to currents)
	 *  Default is $TEMP/minerva-magneticscache/
	 */
	public static String getMagneticsCachePath(){
		String outPath = MinervaSettings.instance().getProperty("minerva.magnetics.cache",
				System.getProperty("java.io.tmpdir")+"/minerva-magneticscache/");
		(new File(outPath)).mkdirs();
		return outPath;
	}
	
	/** @return path for general data files stored in the file system
	 *  Default is $TEMP/minerva-data/
	 */
	public static String getDataPath(){
		String outPath = MinervaSettings.instance().getProperty("minerva.data.path",
				System.getProperty("java.io.tmpdir")+"/minerva-data/");
		(new File(outPath)).mkdirs();
		return outPath;
	}

	
	/** @return The debug log level: 0=quiet, 1=more verbose etc. */
	public static int getDbgLevel(){
		return Integer.parseInt(MinervaSettings.instance().getProperty("minerva.logLevel", "0"));
	}
	
	/** @return XBase URL override or 'default' to use the built-in one */
	public static String getXBaseURL(){
		return (MinervaSettings.instance().getProperty("minerva.xbase.url", "default"));
	}
	
	/** @return XBase Admin URL override or 'default' to use the build-in one */
	public static String getXBaseAdminURL(){
		return (MinervaSettings.instance().getProperty("minerva.xbase.admin.url", "default"));
	}

	
	/** @return path for XBase cache.
	 *  Default is $TEMP/minerva-magneticscache/
	 */
	public static String getXBaseCachePath(){
		String outPath = MinervaSettings.instance().getProperty("minerva.xbase.cache",
				System.getProperty("java.io.tmpdir")+"/minerva-xbasecache/");
		(new File(outPath)).mkdirs();
		return outPath;
	}
	
	/** @return Returns string setting for cache use ( 'always','never' or 'offline')
	 *  Default is 'offline'
	 */
	public static String getXBaseUseCache(){
		String out = MinervaSettings.instance().getProperty("minerva.xbase.useCache", "offline");

		if(!out.equalsIgnoreCase("always") && 
				!out.equalsIgnoreCase("never") && 
				!out.equalsIgnoreCase("offline"))
			throw new IllegalArgumentException("minerva.xbase.useCache in minerva-settings should be 'always','never' or 'offline'.");
		
		return out;
	}
	
	/** @return Returns whether minerva should test (and activate) the XBase connection when it sets up the XBase service and cache, or wait for
	 * the first real non-cached request. 
	 *  
	 *  Default is true
	 */
	public static boolean getXBaseTokenOnDemand(){
		return Boolean.parseBoolean(MinervaSettings.instance().getProperty("minerva.xbase.tokenOnDemand", "true"));
	}
	
	/** @return Mag3D URL override or 'default' to use the build-in one */
	public static String getMag3DURL(){
		return (MinervaSettings.instance().getProperty("minerva.mag3d.url", "default"));
	}	

	/** @return Mag3D URL override or 'default' to use the build-in one */
	public static String getMDSServer(){
		String defaultUTRL;
		/* If in windows, bounce off of ws-services
		 *  because windows' TCP stack is broken somehow
		 * and it can't connect to the real server quickly. 
		 */ //FIXME: Find out what this is really (also change in JetMDSFetcher() it self.		
		return MinervaSettings.instance().getProperty("data-signals.server", 
						System.getProperty("os.name").startsWith("Windows") 
							? "wsservices-1.jet.uk:8765" : "mdsplus.jet.efda.org:8000");
	}	
	
	/** @return Mag3D Maximum number of points that should be requested in one go from Mag3d server. */
	public static int getMag3DMaxRequestLength(){
		int maxReqLen = Integer.parseInt(MinervaSettings.instance().getProperty("minerva.mag3d.max-request-length", "100"));
		if(maxReqLen < 10){
			throw new RuntimeException("Maxiumum mag3D request length should not be < 10. " +
					"Making large numbers of requests like this make the MagneticModelAxiSym inefficient and " +
					"can cause serious problems for the server. If individual requests are really needed for a particular " +
					"diagnostic, this should be done in the diagnostic's code itself.");
		}
		return maxReqLen;
	}
	
	/** @return Whether or not to use native flush locally (rather than run it over MDS+) */
	public static boolean useLocalFlush(){
		return Boolean.parseBoolean(MinervaSettings.instance().getProperty("minerva.flush.uselocal", "false"));
	}
}

