package com.decker.clientupdate.core;

import com.decker.clientupdate.Loader;
import com.decker.clientupdate.core.client.ClientProxy;
import com.decker.clientupdate.core.client.ClientType;
import com.decker.clientupdate.interactiveUI.ConfigFrame;
import com.decker.clientupdate.interactiveUI.ProgressFrame;
import com.decker.clientupdate.interactiveUI.ReportFrame;
import com.decker.clientupdate.util.GUILauncher;
import java.io.File;
import java.io.IOException;
import javax.swing.JFrame;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.exception.ExceptionUtils;

/**
 *
 * @author decker
 */
public final class UpdateCore {

    public static UpdateCore instance;

    public static UpdateCore getInstance() throws IOException {
        if (instance == null) {
            instance = new UpdateCore();
        }
        return instance;
    }

    private final File tempFolder;
    private final File currentFolder;
    private final String currentFolderPath;
    private ProgressFrame progressFrame;

    public File getTempFolder() {
        return this.tempFolder;
    }

    public String getCurrentFolderPath() {
        return this.currentFolderPath;
    }

    private UpdateCore() throws IOException {
        //Get current work folder
        this.currentFolder = new File(new File(UpdateCore.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getParentFile().getCanonicalPath());
        this.currentFolderPath = currentFolder.getCanonicalPath() + "/";
        //Generate temp folder
        tempFolder = new File(this.currentFolderPath + "temp");
        if (tempFolder.exists()) {
            this.removeTempFolder();
            tempFolder.mkdir();
        } else {
            tempFolder.mkdir();

        }
    }

    public void process() throws Exception {
        String updateResultReport = null;
        try {
            //Receive config if ilist is empty 
            if (Config.haveSuchConfig("ilist")) {
                ConfigFrame configFrame = new ConfigFrame();
                GUILauncher.Run(configFrame);
                System.out.println("Form finished! Result is" + configFrame.getIListUrl());
                Config.setConfig("ilist", configFrame.getIListUrl());
                Config.saveAllConfigToFile();
                configFrame.dispose();
            }
            progressFrame = new ProgressFrame();
            GUILauncher.Launch(progressFrame);
            String[] instructionList = new ClientProxy(ClientType.HttpClient).receiveToString(Config.getConfig("ilist")).split("\n");
            for (int i = 0; i < instructionList.length; i++) {
                String instruction = instructionList[i];
                String executeResult = this.ExecuteInstruction(instruction);
                this.progressFrame.setStatus(executeResult);
                this.progressFrame.setProgress(((double) (i + 1) / (double) instructionList.length));
            }
            this.removeTempFolder();
            updateResultReport = String.format("All update finished! %s instructions executed.", String.valueOf(instructionList.length));
        } catch (Exception e) {
            updateResultReport = String.format("At least one deadly error encountered! Please connect to server manager to report this:%s", ExceptionUtils.getStackTrace(e));

        }
        this.progressFrame.dispose();
        ReportFrame reportFrame = new ReportFrame();
        reportFrame.setReport(updateResultReport);
        GUILauncher.Run(reportFrame);

    }

    public String ExecuteInstruction(String instruction) {

        try {
            return new Executor().execute(instruction);
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    public void removeTempFolder() throws IOException {
        if (this.tempFolder.exists()) {
            FileUtils.deleteQuietly(this.tempFolder);
        }
    }

}

@@ -36,47 +36,8 @@
 e;%0A%0A
-import com.decker.clientupdate.Loader;%0A
 impo
@@ -432,22 +432,22 @@
 java
-x.swing.JFrame
+.nio.file.Path
 ;%0Aim
@@ -900,52 +900,8 @@
 er;%0A
-    private final String currentFolderPath;%0A
     
@@ -1022,22 +1022,20 @@
  public 
-String
+Path
  getCurr
@@ -1085,20 +1085,25 @@
 ntFolder
+.to
 Path
+()
 ;%0A    %7D%0A
@@ -1351,81 +1351,8 @@
 ));%0A
-        this.currentFolderPath = currentFolder.getCanonicalPath() + %22/%22;%0A
     
@@ -1399,25 +1399,16 @@
 older = 
-new File(
 this.cur
@@ -1421,21 +1421,41 @@
 lder
+.to
 Path
- + %22temp%22
+().resolve(%22temp%22).toFile(
 );%0A 

FIx path bug.