package ajcon.JavaML;

import ajcon.Log.Logger;
import ajcon.UI.MainWindow;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;


public class JavaMLHandler {
    private ArrayList<String> appletSourcePaths;
    private MainWindow vMainWindow;
    private org.apache.log4j.Logger logger;
    private String pathToJavaHome, rtJarPath;
    private String projectPath, jikesPath;

    public JavaMLHandler(ArrayList<String> appletSP, String projectPath, String jikesPath, MainWindow mw) {
        logger = Logger.getSingletonLogger();
        vMainWindow = mw;
        appletSourcePaths = appletSP;
        pathToJavaHome = getEnvironmentVariables();

        this.projectPath = projectPath;
        this.jikesPath = jikesPath;
    }


    private String getEnvironmentVariables(){
        String tmp = "";
        Map<String, String> envMap = System.getenv();
        tmp = System.getenv("JAVA_HOME");
       
        /*
        for (String envName : envMap.keySet()) {
            System.out.format("%s = %s%n", envName, envMap.get(envName));
        }
       */

        //System.out.println(tmp);
        return tmp;
    }

    private String getLibPath(){
        String libPath ="";

         try{
            BufferedReader cfgReader = new BufferedReader(new FileReader(projectPath + "\\" + "config.txt"));
            cfgReader.readLine();
            libPath = cfgReader.readLine();
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }

        return libPath;
    }

    public void runJavaML(){

        int size = appletSourcePaths.size();
        for(int i=0; i<size ; i++){

             //System.out.println(appletSourcePaths.get(i));
             initializeBatFile( vMainWindow.projectPath + "javaML.bat", appletSourcePaths.get(i));
             String command = "cmd /c "+ vMainWindow.projectPath + "javaML.bat";  // bat dosyası path i verilecek

		try {
			Process proc = null;
			proc = Runtime.getRuntime().exec(command);
                        proc.waitFor();

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
        }
       
         
    }

    private void initializeBatFile(String batFilePath, String javaMLXML) {
       FileOutputStream fileOut = null;

       getEnvironmentVariables();
       rtJarPath = pathToJavaHome + "\\lib\\rt.jar";
       String libPath = getLibPath();

        String eof = "\n";
        try {
                fileOut = new FileOutputStream(batFilePath, false);

                fileOut.write(jikesPath.substring(0, jikesPath.indexOf(":")+1).getBytes());
                fileOut.write(eof.getBytes(), 0, 1);
                fileOut.write(("cd " + jikesPath + " ").getBytes());
                fileOut.write(eof.getBytes(), 0, 1);
                fileOut.write(("jikes.exe +ux -classpath ;" + rtJarPath+ ";" + libPath + " " + javaMLXML).getBytes());
                fileOut.write(eof.getBytes(), 0, 1);

                fileOut.close();


        }
        catch (Exception e) {
                e.printStackTrace();
        }

       // System.out.println("dfvdfbgdf");
    }

    
}
