package net.sf.jabref.gui.desktop.os;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.StringJoiner;

import net.sf.jabref.gui.externalfiletype.ExternalFileType;
import net.sf.jabref.gui.externalfiletype.ExternalFileTypes;
import net.sf.jabref.preferences.JabRefPreferences;

import static net.sf.jabref.preferences.JabRefPreferences.ADOBE_ACROBAT_COMMAND;
import static net.sf.jabref.preferences.JabRefPreferences.USE_PDF_READER;

public class Windows implements NativeDesktop {
    private static String DEFAULT_EXECUTABLE_EXTENSION = ".exe";

    @Override
    public void openFile(String filePath, String fileType) throws IOException {
        Optional<ExternalFileType> type = ExternalFileTypes.getInstance().getExternalFileTypeByExt(fileType);

        if (type.isPresent() && !type.get().getOpenWithApplication().isEmpty()) {
            openFileWithApplication(filePath, type.get().getOpenWithApplication());
        } else {
            // quote String so explorer handles URL query strings correctly
            String quotePath = "\"" + filePath +"\"";
            new ProcessBuilder("explorer.exe", quotePath).start();
        }
    }

    @Override
    public String detectProgramPath(String programName, String directoryName) {
        String progFiles = System.getenv("ProgramFiles(x86)");
        if (progFiles == null) {
            progFiles = System.getenv("ProgramFiles");
        }
        if ((directoryName != null) && !directoryName.isEmpty()) {
            return Paths.get(progFiles, directoryName, programName + DEFAULT_EXECUTABLE_EXTENSION).toString();
        }
        return Paths.get(progFiles, programName + DEFAULT_EXECUTABLE_EXTENSION).toString();
    }

    @Override
    public void openFileWithApplication(String filePath, String application) throws IOException {
        new ProcessBuilder(Paths.get(application).toString(), Paths.get(filePath).toString()).start();
    }

    @Override
    public void openFolderAndSelectFile(String filePath) throws IOException {
        new ProcessBuilder("explorer.exe", "/select,", filePath).start();
    }

    @Override
    public void openConsole(String absolutePath) throws IOException {
        ProcessBuilder process = new ProcessBuilder("cmd.exe", "/c", "start");
        process.directory(new File(absolutePath));
        process.start();
    }

    @Override
    public void openPdfWithParameters(String filePath, List<String> parameters) throws IOException {

        StringJoiner paramJoiner = new StringJoiner(" ");
        if(JabRefPreferences.getInstance().get(USE_PDF_READER).equals(ADOBE_ACROBAT_COMMAND)) {
            paramJoiner.add(JabRefPreferences.getInstance().get(USE_PDF_READER));
            parameters.forEach((param) -> paramJoiner.add(param));
        } else {
            paramJoiner.add(parameters.get(0));
        }

        new ProcessBuilder(Paths.get(USE_PDF_READER).toString(), paramJoiner.toString());
    }
}

@@ -162,39 +162,8 @@
 nal;
-%0Aimport java.util.StringJoiner;
 %0A%0Aim
@@ -285,141 +285,8 @@
 es;%0A
-import net.sf.jabref.preferences.JabRefPreferences;%0A%0Aimport static net.sf.jabref.preferences.JabRefPreferences.ADOBE_ACROBAT_COMMAND;
 %0Aimp
@@ -2371,17 +2371,16 @@
 ption %7B%0A
-%0A
         
@@ -2389,207 +2389,141 @@
 ring
-Joiner paramJoiner = new StringJoiner(%22 %22);%0A        if(JabRefPreferences.getInstance().get(USE_PDF_READER).equals(ADOBE_ACROBAT_COMMAND)) %7B%0A            paramJoiner.add(JabRefPreferences.getInstance()
+%5B%5D params = (String%5B%5D) parameters.toArray();%0A        String%5B%5D command = new String%5Bparameters.size() + 1%5D;%0A        command%5B0%5D = Paths
 .get
@@ -2538,16 +2538,26 @@
 _READER)
+.toString(
 );%0A     
@@ -2563,112 +2563,76 @@
     
-    parameters.forEach((param) -%3E paramJoiner.add(param));%0A        %7D else %7B%0A            paramJoiner.add(
+for( int i = 1; i %3C command.length; i++) %7B%0A            command%5Bi%5D = 
 para
@@ -2646,10 +2646,11 @@
 get(
-0)
+i-1
 );%0A 
@@ -2654,25 +2654,24 @@
 ;%0A        %7D%0A
-%0A
         new 
@@ -2689,68 +2689,15 @@
 der(
-Paths.get(USE_PDF_READER).toString(), paramJoiner.toString()
+command
 );%0A 

Use single array to open pdf on windows