/*
 * BatchFromStrippedGUI2.java
 *
 * Created on 20 January 2005, 17:02
 */

/**
 *
 * @author  mnaylor
 */
public class BatchFromStrippedGUI2 {
    
    public static LomnitzAutonome world;
    private static String fileNameRoot, loadingMechanism;
    private static int Nx,neighbourNumber, runtimeDuration;
    private static double beta, maxStrength, minStrength;
    private static double maxStressRelaxationFactor, percentageNoise, asymmetricFactor;
    private static boolean usePeriodicBC;
    private static ReadBatchInput2 batchInput;
    private static int autonomeThreadSleepDuration;
    private static boolean isFirstTime = true;
    
    /** Creates a new instance of BatchFromStrippedGUI2 */
    public BatchFromStrippedGUI2() {
        resetParameters();
        setWorldParameters();
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String batchFileName;
        if(args[0]!=null){batchFileName = args[0];}
        else{ batchFileName = "batchTemplate.txt";}
//        batchFileName = "batchTemplate.txt";
        batchInput = new ReadBatchInput2(batchFileName);
        setDefaultParameters();
        world = new LomnitzAutonome();
        runNextBatchEntry();
    }
    
    public static void runNextBatchEntry(){
        batchInput.readNextBatchEntry();
        resetParameters();
        setWorldParameters();
        world.printWorldParameters();
        startAutonome(isFirstTime);
    }
    
    
    private static void resetParameters(){
        fileNameRoot = batchInput.getFileNameRoot();
        Nx = batchInput.getNx();
        neighbourNumber = batchInput.getNeighbourNumber();
        beta = batchInput.getBeta();
        maxStrength = batchInput.getMaxStrength();
        minStrength = batchInput.getMinStrength();
        usePeriodicBC = batchInput.getUsePeriodicBC();
        loadingMechanism = batchInput.getLoadingMechanism();
        runtimeDuration = batchInput.getRuntimeDuration();
        maxStressRelaxationFactor = batchInput.getMaxStressRelaxationFactor();
        percentageNoise = batchInput.getPercentageNoise();
        asymmetricFactor = batchInput.getAsymmetricFactor();
    }
    
    private static void startAutonome(boolean test){
        System.out.println(test);
        if( test ){ world.startAutonome(); isFirstTime=false; }
        else{ world.resumeAutonome(); }
    }
    
    private void pauseAutonome(){ world.pauseAutonome(); }
    
    private void resumeAutonome(){ world.resumeAutonome(); }
    
    private void saveData(){ world.saveData(); }
    
    private static void setDefaultParameters() {
        System.out.println("Setting batch default parameters");
        beta = 0.1;
        Nx = 100;
        neighbourNumber = 4;
        maxStrength = 1;
        minStrength = 1;
        usePeriodicBC = false;
        runtimeDuration = 20000;
        autonomeThreadSleepDuration = 1;
        maxStressRelaxationFactor = 0;
        percentageNoise=0;
        loadingMechanism = "constantLoading"; // or randomLoading or loadToRupture
        asymmetricFactor = 1;
    }
    
    private static void setWorldParameters() {
        System.out.println("Setting world parameters for : " + fileNameRoot);
        world.setFileNameRoot(fileNameRoot);
        world.setBeta(beta);
        world.setNx(Nx);
        world.setNeighbourNumber(neighbourNumber);
        world.setMaxStrength(maxStrength);
        world.setMinStrength(minStrength);
        world.setRelaxationStressFactor(maxStressRelaxationFactor);
        world.setPeriodicBC(usePeriodicBC);
        world.setLoadingMechanism(loadingMechanism);
        world.setRuntimeDuration(runtimeDuration);
        world.setAutonomeThreadSleepDuration(autonomeThreadSleepDuration);
        world.setPercentageNoise(percentageNoise);
        world.setAsymmetricFactor(asymmetricFactor);
        world.resetAutonome();
    }
}
