package org.jetbrains.haskell.config;

import com.intellij.openapi.components.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.haskell.util.OsUtil;
import org.jetbrains.haskell.util.UtilPackage;

import java.io.File;

@State(
        name = "HaskellConfiguration",
        storages = {
                @Storage(id = "default", file = StoragePathMacros.APP_CONFIG + "/haskell.xml")
        }
)
public class HaskellSettings implements PersistentStateComponent<HaskellSettings.State> {

    @NotNull
    public static HaskellSettings getInstance() {
        HaskellSettings persisted = ServiceManager.getService(HaskellSettings.class);
        if (persisted == null) {
            persisted = new HaskellSettings();
        }

        if (persisted.getState().cabalPath == null) {
            persisted.getState().cabalPath = "cabal";
        }

        if (persisted.getState().cabalDataPath == null) {
            OsUtil os = UtilPackage.getOS();
            persisted.getState().cabalDataPath = os.getCabalData();
        }

        if (persisted.getState().ghcModPath == null) {
            OsUtil os = UtilPackage.getOS();
            persisted.getState().ghcModPath = os.getDefaultCabalBin() + File.separator + "ghc-mod" + os.getExe();
        }

        if (persisted.getState().ghcModiPath == null) {
            OsUtil os = UtilPackage.getOS();
            persisted.getState().ghcModiPath = os.getDefaultCabalBin() + File.separator + "ghc-modi" + os.getExe();
        }

        if (persisted.getState().buildWrapperPath == null) {
            OsUtil os = UtilPackage.getOS();
            persisted.getState().buildWrapperPath = os.getDefaultCabalBin() + File.separator + "buildwrapper" + os.getExe();
        }

        if (persisted.getState().scionBrowserPath == null) {
            OsUtil os = UtilPackage.getOS();
            persisted.getState().scionBrowserPath = os.getDefaultCabalBin() + File.separator + "scion-browser" + os.getExe();
        }


        return persisted;
    }

    public static class State {
        public String ghcModPath;
        public String ghcModiPath;
        public String buildWrapperPath;
        public String scionBrowserPath;
        public String cabalPath;
        public String cabalDataPath;
    }

    State myState = new State();

    @NotNull
    public State getState() {
        return myState;
    }

    public void loadState(State state) {
        myState = state;
    }


}

@@ -746,32 +746,393 @@
     
-if (persisted.getState()
+persisted.update();%0A%0A        return persisted;%0A    %7D%0A%0A    public static class State %7B%0A        public String ghcModPath;%0A        public String ghcModiPath;%0A        public String buildWrapperPath;%0A        public String scionBrowserPath;%0A        public String cabalPath;%0A        public String cabalDataPath;%0A    %7D%0A%0A    State myState = new State();%0A%0A    void update() %7B%0A        if (myState
 .cab
@@ -1161,36 +1161,23 @@
         
-persisted.get
+my
 State
-()
 .cabalPa
@@ -1213,36 +1213,23 @@
     if (
-persisted.get
+my
 State
-()
 .cabalDa
@@ -1303,36 +1303,23 @@
         
-persisted.get
+my
 State
-()
 .cabalDa
@@ -1369,36 +1369,23 @@
     if (
-persisted.get
+my
 State
-()
 .ghcModP
@@ -1456,36 +1456,23 @@
         
-persisted.get
+my
 State
-()
 .ghcModP
@@ -1568,36 +1568,23 @@
     if (
-persisted.get
+my
 State
-()
 .ghcModi
@@ -1656,36 +1656,23 @@
         
-persisted.get
+my
 State
-()
 .ghcModi
@@ -1770,36 +1770,23 @@
     if (
-persisted.get
+my
 State
-()
 .buildWr
@@ -1863,36 +1863,23 @@
         
-persisted.get
+my
 State
-()
 .buildWr
@@ -1986,36 +1986,23 @@
     if (
-persisted.get
+my
 State
-()
 .scionBr
@@ -2083,28 +2083,15 @@
     
-persisted.get
+my
 State
-()
 .sci
@@ -2195,332 +2195,13 @@
  %7D%0A%0A
-%0A        return persisted;%0A    %7D%0A%0A    public static class State %7B%0A        public String ghcModPath;%0A        public String ghcModiPath;%0A        public String buildWrapperPath;%0A        public String scionBrowserPath;%0A        public String cabalPath;%0A        public String cabalDataPath;%0A    %7D%0A%0A    State myState = new State();
+    %7D
 %0A%0A  
@@ -2342,14 +2342,31 @@
 te;%0A    
+    update();%0A    
 %7D
-%0A
 %0A%0A%7D%0A

Load Haskell settings right. Fix NPE