/*
 * ReadBatchInput.java
 *
 * Created on 04 January 2005, 18:17
 */
import java.io.*;
import java.util.regex.*;
import java.util.*;
import java.util.Iterator.*;

/**
 *
 * @author  mnaylor
 */
public class ReadBatchInput2 {
    
    /**
     * HashMap containing all the default and values input from batch file
     */    
    public static HashMap parameterMap;
    /**
     * FileReader for the batch input file
     */    
    public static FileReader fr;
    private static File file;
    private static String path;
    private static BufferedReader in;
    private static String [] header;
    private static Object beta, minStrength, maxStrength, usePeriodicBC, runtimeDuration; 
    private static Object Nx, maxStressRelaxationFactor, loadingMechanism, percentageNoise;
    private static Object neighbourNumber, asymmetricFactor;
    private static String boundaryCondition;
    private static String fileNameRoot;
    
//    public static GarathsAutonome world;
    
    /**
     * Open batch file, read header and process
     */    
    public static void openAndInitiateBatchFile(){
        openBatchInputFile();
        readHeader();
    }
    
    private static void init(){
        Nx = "100";
        neighbourNumber = "4";
        fileNameRoot = "a";
        beta = "0.2";
        minStrength = "1.0";
        maxStrength = "1.0";
        usePeriodicBC = "false" ;
        loadingMechanism = "constantLoading";
        runtimeDuration = "20000";
        maxStressRelaxationFactor = "0";
        percentageNoise = "0";
        asymmetricFactor = "1";
        
        parameterMap.put("Nx", Nx);
        parameterMap.put("neighbourNumber", neighbourNumber);
        parameterMap.put("fileNameRoot", fileNameRoot);
        parameterMap.put("beta", beta);
        parameterMap.put("maxStrength", maxStrength);
        parameterMap.put("minStrength", minStrength);
        parameterMap.put("loadingMechanism", loadingMechanism);
        parameterMap.put("usePeriodicBC", usePeriodicBC);
        parameterMap.put("runtimeDuration", runtimeDuration);
        parameterMap.put("maxStressRelaxationFactor", maxStressRelaxationFactor);
        parameterMap.put("percentageNoise", percentageNoise);
        parameterMap.put("asymmetricFactor", asymmetricFactor);

    }
    
    /** Creates a new instance of ReadBatchInput */
    public ReadBatchInput2(String batchFileName) {
        parameterMap = new HashMap ();
        init();
        
//        path = "/home/mnaylor/Java/AutomatonViewer/Batch/";
        path = System.getProperty("user.dir");
        System.out.println(path);
        file = new File (path + "/" + batchFileName);
        
        if (!file.exists()){ System.out.println("File " + file + " does not exist"); }
        else {
            openBatchInputFile();
            readHeader();
        }
    }  
    
    private static void printRunDetails (){
        try{
           fileNameRoot = parameterMap.get("fileNameRoot").toString();
            
           File detailsFile = new File (fileNameRoot + "-details.out");
           FileWriter fwDetails = new FileWriter ( detailsFile );	
           
           Collection parameterMapValues = parameterMap.values();
           Set parameterMapKeys = parameterMap.keySet();
                      
           Iterator iterator = parameterMapKeys.iterator();
           Iterator collectionIterator = parameterMapValues.iterator();
           
           while (iterator.hasNext()){
               fwDetails.write(iterator.next() + " = " + collectionIterator.next() + "\n");
           }
           
           fwDetails.close();
        } catch ( IOException e ) { System.out.println("Cannot output run details to file"); }

    }
    
    /**
     * Reads next batch file entry and runs autonome accordingly
     */    
    public static void readNextBatchEntry (){
        
        try {            
           String nextLine;
           if ( (nextLine = in.readLine()) != null) {
               String [] fields = nextLine.split ( "," );
               trimStringEntries (fields);
               setParameterMap(fields);
               printRunDetails();
           }
           else{
               System.out.println("End of file.");
               System.exit(0);
           }
        } catch ( IOException e ) { System.out.println("Cannot read and print batchInputFile"); }
    
    }
    
    private static void setParameterMap(String [] fields){
        for (int n = 0; n<fields.length; n++){ parameterMap.put(header[n],fields[n]); }
    }
    
    private static void readHeader () {
        try {            
           String line = in.readLine();
           header = line.split ( "," );           
           trimStringEntries (header);            
        } catch ( IOException e ) { System.out.println("Cannot read file header"); }        
    }
    
    private static void trimStringEntries (String [] line) {
        String REGEX = "\"";
        String REPLACE = "";
        
        for(int n=0; n<line.length; n++){
            String INPUT = line[n];            
            Pattern p = Pattern.compile(REGEX);
            Matcher m = p.matcher(INPUT); // get a matcher object
            INPUT = m.replaceAll(REPLACE);            
            line[n] = INPUT;
        }
    }
    
    /**
     * get fileNameRoot from HashMap
     */    
    public static String getFileNameRoot () {
        String string = parameterMap.get("fileNameRoot").toString();
        return string;
    }
    
    public static String getLoadingMechanism () {
        String string = parameterMap.get("loadingMechanism").toString();
        return string;
    }
    
    /**
     * get beta from HashMap
     */    
    public static double getBeta () {
        String string = parameterMap.get("beta").toString();
        return Double.parseDouble(string);
    }
    
    /**
     * get maxStrength from HashMap
     */    
    public static double getMaxStrength () {
        String string = parameterMap.get("maxStrength").toString();
        return Double.parseDouble(string);
    }
    
    /**
     * get minStrength from HashMap
     * @return minStrength
     */    
    public static double getMinStrength () {
        String string = parameterMap.get("minStrength").toString();
        return Double.parseDouble(string);
    }

    public static double getAsymmetricFactor () {
        String string = parameterMap.get("asymmetricFactor").toString();
        return Double.parseDouble(string);
    }
    
    /**
     * get Nx from HashMap
     * @return
     */    
    public static int getNx() {
        String string = parameterMap.get("Nx").toString();
        return Integer.parseInt(string);
    }
   
    public static int getNeighbourNumber() {
        String string = parameterMap.get("neighbourNumber").toString();
        return Integer.parseInt(string);
    }
   
    public static double getMaxStressRelaxationFactor() {
        String string = parameterMap.get("maxStressRelaxationFactor").toString();
        return Double.parseDouble(string);
    }
   
    public static double getPercentageNoise() {
        String string = parameterMap.get("percentageNoise").toString();
        return Double.parseDouble(string);
    }
   
    /**
     * get runtimeDuration from HashMap
     */    
    public static int getRuntimeDuration() {
        String string = parameterMap.get("runtimeDuration").toString();
        return Integer.parseInt(string);
    }
    
    public static boolean getUsePeriodicBC (){
        String string = parameterMap.get("usePeriodicBC").toString();
        boolean b = new Boolean(string).booleanValue();
        return b;
    }
    
    private static void openBatchInputFile () {       
        try {            
            fr = new FileReader ( file );
            in = new BufferedReader( fr );
        } catch ( IOException e ) { System.out.println("Cannot open batchInputFile"); }   
   }
    
    private static void closeBatchInputFile () {
        try { fr.close();
        } catch ( IOException e ) { System.out.println("Cannot close batchInputFile"); }
    }
}
