package _VisualDVM.Passes;
import Common.CommonConstants;
import Common.Passes.Pass;
import Common.Passes.PassException;
import Common.Utils.Utils_;
import _VisualDVM.Utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.Vector;
public abstract class ProcessPass<T> extends Pass<T> {
    public Process process = null;
    public int exit_code = CommonConstants.Nan;
    public LinkedHashMap<String, String> envs = new LinkedHashMap<>();
    public Vector<String> output = new Vector<>();
    protected String process_path = "";
    public void CreateProcess(String exec_path_in, File workspace, String... command) throws Exception {
        output.clear();
        exit_code = CommonConstants.Nan;
        process_path = exec_path_in;
        ProcessBuilder procBuilder = new ProcessBuilder(process_path);
        //-
        if (workspace != null)
            procBuilder.directory(workspace);
        //-
        procBuilder.redirectErrorStream(true);
        for (String name : envs.keySet())
            procBuilder.environment().put(name, envs.get(name));
        envs.clear();
        // запуск программы
        process = procBuilder.start();
    }
    public void CreateProcess(String exec_path_in) throws Exception {
        CreateProcess(exec_path_in, null);
    }
    //ожидать завершения читая весь его поток в процессе выполнения.
    public void WaitForProcess() throws Exception {
        exit_code = process.waitFor();
        process = null;
        if (exit_code != 0)
            throw new PassException("Процесс завершился с кодом " + exit_code);
    }
    public void ReadAllOutput() {
        try {
            InputStream stdout = process.getInputStream();
            InputStreamReader isrStdout = new InputStreamReader(stdout);
            BufferedReader brStdout = new BufferedReader(isrStdout);
            String line;
            while ((line = brStdout.readLine()) != null) {
                output.add(line);
            }
        } catch (Exception ex) {
            Utils_.MainLog.PrintException(ex);
        }
    }
    public String ReadLine() throws Exception {
        InputStream stdout = process.getInputStream();
        InputStreamReader isrStdout = new InputStreamReader(stdout);
        BufferedReader brStdout = new BufferedReader(isrStdout);
        return brStdout.readLine();
    }
    public void PerformScript(String script_text) throws Exception {
        PerformScript(Utils.CreateTempFile("script", Utils_.isWindows() ? "bat" : "", script_text), true);
    }
    public void PerformScript(File script, boolean wait) throws Exception {
        if (!script.setExecutable(true)) throw new PassException("Не удалось создать исполняемый файл для скрипта");
        CreateProcess(script.getAbsolutePath());
        if (wait) {
            ReadAllOutput();
            WaitForProcess();
        }
    }
    @Override
    protected boolean needsAnimation() {
        return true;
    }
}
