package com.haskforce;

import com.haskforce.utils.ExecUtil;
import com.intellij.openapi.projectRoots.*;
import com.intellij.openapi.util.SystemInfo;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.io.File;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HaskellSdkType extends SdkType {

    public static final String HASKELL_SDK_TYPE_ID = "Haskell SDK";

    public HaskellSdkType() {
        // TODO
        super(HASKELL_SDK_TYPE_ID);
    }

    @NotNull
    public static HaskellSdkType getInstance() {
        return SdkType.findInstance(HaskellSdkType.class);
    }

    @Override
    public Icon getIcon() {
        return HaskellIcons.FILE;
    }

    @Nullable
    @Override
    public AdditionalDataConfigurable createAdditionalDataConfigurable(SdkModel sdkModel,
                                                                       SdkModificator sdkModificator) {
        return null;
    }

    @Override
    public String getPresentableName() {
        return HASKELL_SDK_TYPE_ID;
    }

    @Override
    public void saveAdditionalData(@NotNull SdkAdditionalData additionalData, @NotNull Element additional) {
    }

    @Override
    public boolean isValidSdkHome(String path) {
        return getVersionString(path) != null;
    }

    @Override
    public String suggestSdkName(String currentSdkName, String sdkHome) {
        return "GHC";
    }

    @Nullable
    @Override
    public String getVersionString(@NotNull final String sdkHome) {
        File ghc = getExecutable(sdkHome);
        if (ghc.canExecute()) {
            final String versionText = ExecUtil.exec(String.format("\"%s\" --version", ghc.getPath()));
            if (versionText != null) {
                Pattern p = Pattern.compile(".*, version (\\d+\\.\\d+\\.\\d+)");
                Matcher m = p.matcher(versionText);
                if (m.matches()) {
                    return m.group(1);
                }
            }
        }
        return null;
    }

    @Nullable
    @Override
    public String suggestHomePath() {
        return SystemInfo.isWindows ? suggestHomePathForWindows() : suggestHomePathForUNIX();
    }

    @Nullable
    public static String suggestHomePathForWindows() {
        final String ghcPath = ExecUtil.exec("where ghc.exe");
        if (ghcPath != null) {
            File ghc = new File(ghcPath);
            if (ghc.canExecute()) {
                return ghc.getParent();
            }
        }
        return null;
    }

    @Nullable
    public static String suggestHomePathForUNIX() {
        return ExecUtil.run("ghc --print-libdir");
    }

    @NotNull
    public static File getExecutable(@NotNull final String path) {
        return new File(path, SystemInfo.isWindows ? "ghc.exe" : "ghc");
    }
}

@@ -300,72 +300,8 @@
 ile;
-%0Aimport java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;
 %0A%0Apu
@@ -1613,34 +1613,14 @@
     
-final String versionText =
+return
  Exe
@@ -1654,16 +1654,24 @@
 %22%25s%5C%22 --
+numeric-
 version%22
@@ -1692,286 +1692,8 @@
 )));
-%0A            if (versionText != null) %7B%0A                Pattern p = Pattern.compile(%22.*, version (%5C%5Cd+%5C%5C.%5C%5Cd+%5C%5C.%5C%5Cd+)%22);%0A                Matcher m = p.matcher(versionText);%0A                if (m.matches()) %7B%0A                    return m.group(1);%0A                %7D%0A            %7D
 %0A   

Use --numeric-version to find out the version of GHC.