package com.reason.bs;

import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

class BsConfig {
    private static Pattern DEPS_REGEXP = Pattern.compile(".*\"bs-dependencies\":\\s*\\[(.*?)].*");
    private static String[] PERVASIVES = new String[]{
            // all files but the ones with _ ?
            "bs-platform/lib/ocaml/js.mli",
    };

    private final String[] m_deps;

    private BsConfig(@Nullable String[] deps) {
        if (deps == null) {
            m_deps = new String[PERVASIVES.length];
            System.arraycopy(PERVASIVES, 0, m_deps, 0, PERVASIVES.length);
        } else {
            m_deps = new String[deps.length + PERVASIVES.length];
            System.arraycopy(deps, 0, m_deps, 0, deps.length);
            System.arraycopy(PERVASIVES, 0, m_deps, deps.length, PERVASIVES.length);
        }
    }

    @NotNull
    static BsConfig read(@NotNull VirtualFile bsconfig) {
        try {
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(bsconfig.getInputStream()))) {
                // Quick and dirty code to read json values from bsconfig
                StringBuilder content = new StringBuilder();
                reader.lines().forEach(line -> content.append(line.trim()));
                // extract bs dependencies
                Matcher matcher = DEPS_REGEXP.matcher(content.toString());

                String[] deps = null;
                if (matcher.matches()) {
                    String[] tokens = matcher.group(1).split(",");
                    deps = new String[tokens.length];
                    for (int i = 0; i < tokens.length; i++) {
                        String token = tokens[i].trim();
                        deps[i] = token.substring(1, token.length() - 1) + "/lib";
                    }
                }

                return new BsConfig(deps);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    boolean accept(@Nullable String canonicalPath) {
        if (canonicalPath == null) {
            return false;
        }

        if (canonicalPath.contains("node_modules") && m_deps != null) {
            for (String dep : m_deps) {
                if (canonicalPath.contains(dep) || canonicalPath.contains("bs-platform/lib/ocaml/pervasives.ml")) {
                    return true;
                }
            }
            return false;
        }

        return true;
    }
}

@@ -323,16 +323,17 @@
 onfig %7B%0A
+%0A
     priv
@@ -419,24 +419,117 @@
 (.*?)%5D.*%22);%0A
+    private static Pattern NAME_REGEXP = Pattern.compile(%22.*%5C%22name%5C%22:%5C%5Cs*%5C%22(%5B%5E%5C%22%5D*?)%5C%22.*%22);%0A%0A
     private 
@@ -670,16 +670,49 @@
     %7D;%0A%0A
+    private final String m_name;%0A
     priv
@@ -760,16 +760,29 @@
 sConfig(
+String name, 
 @Nullabl
@@ -792,32 +792,56 @@
 tring%5B%5D deps) %7B%0A
+        m_name = name;%0A%0A
         if (deps
@@ -1637,16 +1637,17 @@
 im()));%0A
+%0A
         
@@ -1658,119 +1658,249 @@
     
-// extract bs dependencies%0A                Matcher matcher = DEPS_REGEXP.matcher(content.toString()
+String name = readName(content);%0A                String%5B%5D deps = readDependencies(content);%0A%0A                return new BsConfig(name, deps);%0A            %7D%0A        %7D catch (IOException e) %7B%0A            throw new RuntimeException(e
 );%0A
-%0A
         
     
@@ -1895,37 +1895,118 @@
         
+%7D%0A
     
+%7D%0A%0A
     
-String%5B%5D deps
+private static String%5B%5D readDependencies(StringBuilder content) %7B%0A        String%5B%5D result
  = null;
@@ -2006,24 +2006,25 @@
 = null;%0A
+%0A
         
         
@@ -2007,32 +2007,91 @@
  null;%0A%0A        
+Matcher matcher = DEPS_REGEXP.matcher(content.toString());%0A
         if (matc
@@ -2103,32 +2103,24 @@
 atches()) %7B%0A
-        
             
@@ -2178,28 +2178,22 @@
         
-        deps
+result
  = new S
@@ -2210,32 +2210,24 @@
 ns.length%5D;%0A
-        
             
@@ -2280,32 +2280,24 @@
             
-     
-   
 String token
@@ -2337,20 +2337,14 @@
     
-       
- deps
+result
 %5Bi%5D 
@@ -2398,32 +2398,34 @@
 b%22;%0A            
+%7D%0A
         %7D%0A      
@@ -2430,162 +2430,293 @@
     
- 
-       %7D%0A%0A                return new BsConfig(deps);%0A            %7D%0A        %7D catch (IOException e) %7B%0A            throw new RuntimeException(e);%0A        %7D
+return result;%0A    %7D%0A%0A    private static String readName(StringBuilder content) %7B%0A        String result = null;%0A%0A        Matcher matcher = NAME_REGEXP.matcher(content.toString());%0A        if (matcher.matches()) %7B%0A            result = matcher.group(1);%0A        %7D%0A%0A        return result;
 %0A   

Read namespace value