from spatialsoul.shell import commandline_exec


def call_headless_fiji(fiji_path, script_path, script_args):
    """
    Executes a Fiji (ImageJ) script.

    Runs a Fiji script by running an external Fiji (ImageJ) in headless mode,
    and passing it the script to be executed.

    Args: 
        fiji_path: The full path (string) to the Fiji executable.
        script_path: The full path (string) to the script to be executed by Fiji.
        script_args: A string with the arguments for the Fiji script. For example, for a script expecting
          an integer argument called my_int_arg and a string argument called my_string_arg, the script_args
          string would look like this: "my_int_arg='42',my_string_arg='to be or not to be'"

    Returns:
        A tuple consisting of the return code, the standard output, and the standard error output
        generated by Fiji.  
    """
    retcode, out, err = commandline_exec(
            [fiji_path, "-Dpython.console.encoding=UTF-8", "--ij2", "--headless", "--console", "--run",
             script_path, script_args])

    return retcode, out, err
