using System.Diagnostics;

namespace Hawkeye.VisionBuilder.Workflow.DataTransfer;

public class PythonScriptRunner
{
    private Process process;
    private readonly string _pythonPath;
    private string scriptPath;
    private readonly string _workingDirectory;

    public PythonScriptRunner(string pythonPath, string scriptPath,string workingDirectory)
    {
        _pythonPath = pythonPath;
        this.scriptPath = scriptPath;
        _workingDirectory = workingDirectory;
    }

    public void Start()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();


        startInfo.FileName = _pythonPath; // set the path to the Python executable if not in PATH
        startInfo.Arguments = "\""+scriptPath+ "\"";
        startInfo.WorkingDirectory = _workingDirectory;
        
        // enable running in the background
        startInfo.UseShellExecute = false;
        //startInfo.CreateNoWindow = true;
        // create and start the process
        process = new Process();
        

        process.StartInfo = startInfo;
        process.Start();
        // on windows only
        if(Environment.OSVersion.Platform == PlatformID.Win32NT)
            ChildProcessTracker.AddProcess(process);


    }

    public void Stop()
    {
        if (process != null && !process.HasExited)
        {
            process.Kill();
            process.Dispose();
            process = null;
        }
    }
}