package com.reason.build.dune;

import com.intellij.execution.ExecutionException;
import com.intellij.execution.configurations.GeneralCommandLine;
import com.intellij.execution.process.KillableColoredProcessHandler;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.process.ProcessListener;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.reason.build.CompilerLifecycle;
import com.reason.build.bs.ModuleConfiguration;
import com.reason.build.bs.compiler.CliType;
import com.reason.ide.RmlNotification;
import org.jetbrains.annotations.Nullable;

import java.util.concurrent.atomic.AtomicBoolean;

public final class DuneCompiler implements CompilerLifecycle {

    private final ModuleConfiguration m_moduleConfiguration;

    private KillableColoredProcessHandler m_processHangler;
    private ProcessListener m_outputListener;
    private final AtomicBoolean m_started = new AtomicBoolean(false);
    private final AtomicBoolean m_restartNeeded = new AtomicBoolean(false);

    public DuneCompiler(ModuleConfiguration moduleConfiguration) {
        m_moduleConfiguration = moduleConfiguration;
        m_outputListener = new DuneOutputListener(moduleConfiguration.getProject(), this);

        recreate(CliType.make);
    }

    @Nullable
    public ProcessHandler getHandler() {
        return m_processHangler;
    }

    // Wait for the tool window to be ready before starting the process
    public void startNotify() {
        if (m_processHangler != null && !m_processHangler.isStartNotified()) {
            try {
                m_processHangler.startNotify();
            } catch (Throwable e) {
                // already done ?
            }
        }
    }

    @Nullable
    public ProcessHandler recreate(CliType cliType) {
        try {
            killIt();
            GeneralCommandLine cli = getGeneralCommandLine(cliType);
            if (cli != null) {
                m_processHangler = new KillableColoredProcessHandler(cli);
                if (m_outputListener != null) {
                    m_processHangler.addProcessListener(m_outputListener);
                }
            }
            return m_processHangler;
        } catch (ExecutionException e) {
            Notifications.Bus.notify(new RmlNotification("Dune", "Can't run sdk\n" + e.getMessage(), NotificationType.ERROR));
        }

        return null;
    }

    public void killIt() {
        if (m_processHangler != null) {
            m_processHangler.killProcess();
            m_processHangler = null;
        }
    }

    public void addListener(ProcessListener outputListener) {
        m_outputListener = outputListener;
        if (m_processHangler != null) {
            m_processHangler.addProcessListener(outputListener);
        }
    }

    @Nullable
    private GeneralCommandLine getGeneralCommandLine(CliType cliType) {
//        String bsbPath = m_moduleConfiguration.getBsbPath();

//        if (bsbPath == null) {
//            Notifications.Bus.notify(new RmlNotification("Dune",
//                    "<html>Can't find sdk.\n"
//                            + "Working directory is '" + m_moduleConfiguration.getWorkingDir() + "'.\n"
//                            + "Be sure that sdk is installed and reachable from that directory, "
//                            + "see <a href=\"https://github.com/reasonml-editor/reasonml-idea-plugin#dune\">github</a>.</html>",
//                    ERROR, URL_OPENING_LISTENER));
//            return null;
//        }

        GeneralCommandLine cli;
        switch (cliType) {
//            case make:
//                cli = new GeneralCommandLine(bsbPath, "-make-world");
//                break;
//            case cleanMake:
//                cli = new GeneralCommandLine(bsbPath, "-clean-world", "-make-world");
//                break;
            default:
                cli = new GeneralCommandLine("/home/herve/.opam/4.02.3/bin/jbuilder", "build", "rincewind.exe");
        }

        cli.withWorkDirectory(m_moduleConfiguration.getWorkingDir());
        return cli;
    }

    public boolean start() {
        boolean success = m_started.compareAndSet(false, true);
        if (!success) {
            m_restartNeeded.compareAndSet(false, true);
        }
        return success;
    }

    public void terminated() {
        m_started.set(false);
    }

}

@@ -363,20 +363,17 @@
 fication
-Type
+s
 ;%0Aimport
@@ -390,34 +390,32 @@
 lij.
-notification.Notifications
+openapi.projectRoots.Sdk
 ;%0Aim
@@ -583,24 +583,60 @@
 tification;%0A
+import com.reason.ide.sdk.OCamlSDK;%0A
 import org.j
@@ -718,16 +718,164 @@
 olean;%0A%0A
+import static com.intellij.notification.NotificationListener.URL_OPENING_LISTENER;%0Aimport static com.intellij.notification.NotificationType.ERROR;%0A%0A
 public f
@@ -2554,33 +2554,16 @@
 sage(), 
-NotificationType.
 ERROR));
@@ -3072,34 +3072,32 @@
 Type cliType) %7B%0A
-//
         String b
@@ -3093,24 +3093,33 @@
    S
-tring bsbPath = 
+dk sdk = OCamlSDK.getSDK(
 m_mo
@@ -3143,22 +3143,20 @@
 .get
-BsbPath();%0A%0A//
+Project());%0A
     
@@ -3163,23 +3163,19 @@
     if (
-bsbPath
+sdk
  == null
@@ -3174,26 +3174,24 @@
  == null) %7B%0A
-//
             
@@ -3243,18 +3243,16 @@
 %22Dune%22,%0A
-//
         
@@ -3285,26 +3285,24 @@
 ind sdk.%5Cn%22%0A
-//
             
@@ -3393,18 +3393,16 @@
  %22'.%5Cn%22%0A
-//
         
@@ -3491,18 +3491,16 @@
 tory, %22%0A
-//
         
@@ -3620,18 +3620,16 @@
 html%3E%22,%0A
-//
         
@@ -3671,18 +3671,16 @@
 ENER));%0A
-//
         
@@ -3696,18 +3696,16 @@
 n null;%0A
-//
         
@@ -3741,393 +3741,55 @@
  cli
-;%0A        switch (cliType) %7B%0A//            case make:%0A//                cli = new GeneralCommandLine(bsbPath, %22-make-world%22);%0A//                break;%0A//            case cleanMake:%0A//                cli = new GeneralCommandLine(bsbPath, %22-clean-world%22, %22-make-world%22);%0A//                break;%0A            default:%0A                cli = new GeneralCommandLine(%22/home/herve/.opam/4.02.3
+ = new GeneralCommandLine(sdk.getHomePath() + %22
 /bin
@@ -3827,27 +3827,16 @@
 .exe%22);%0A
-        %7D%0A%0A
         
@@ -3893,24 +3893,25 @@
 kingDir());%0A
+%0A
         retu

use sdk home path