package com.reason.bs.hints;

import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.reason.Platform;
import com.reason.Streams;
import com.reason.bs.BucklescriptProjectComponent;
import com.reason.ide.RmlNotification;

import javax.annotation.Nullable;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

// WARNING... THIS IS A BIG WIP...
public class BsQueryTypesServiceComponent implements BsQueryTypesService {

    private final Project m_project;
    private final VirtualFile m_baseDir;
    private final String m_bsbPath;

    public BsQueryTypesServiceComponent(Project project, VirtualFile baseDir, String bsbPath) {
        m_project = project;
        m_baseDir = baseDir;
        m_bsbPath = bsbPath;
    }

    @Nullable
    @Override
    public InferredTypes types(VirtualFile file) {
        InferredTypesImplementation result = null;

        // Find corresponding cmi file... wip
        String filePath = file.getCanonicalPath();
        if (filePath != null) {
            String namespace = BucklescriptProjectComponent.getInstance(m_project).getNamespace();
            String cmiFilename = Platform.removeProjectDir(m_project, filePath).replace(file.getPresentableName(), file.getNameWithoutExtension() + (namespace.isEmpty() ? "" : "-" + namespace) + ".cmi");
            VirtualFile cmiFile = m_baseDir.findFileByRelativePath("lib/bs" + cmiFilename);
            if (cmiFile != null) {
                ProcessBuilder m_bscProcessBuilder = new ProcessBuilder(m_bsbPath, cmiFile.getCanonicalPath());
                String basePath = m_baseDir.getCanonicalPath();
                if (basePath != null) {
                    m_bscProcessBuilder.directory(new File(basePath));

                    Process bsc = null;
                    try {
                        bsc = m_bscProcessBuilder.start();
                        BufferedReader reader = new BufferedReader(new InputStreamReader(bsc.getInputStream()));
                        BufferedReader errReader = new BufferedReader(new InputStreamReader(bsc.getErrorStream()));

                        Streams.waitUntilReady(reader, errReader);
                        StringBuilder msgBuffer = new StringBuilder();
                        if (errReader.ready()) {
                            errReader.lines().forEach(line -> msgBuffer.append(line).append(System.lineSeparator()));
                            Notifications.Bus.notify(new RmlNotification("Code lens", msgBuffer.toString(), NotificationType.ERROR));
                        } else {
                            reader.lines().forEach(line -> {
                                //System.out.println("»" + line + "«");
                                if (line.startsWith("type") || line.startsWith("val") || line.startsWith("module") || line.startsWith("external")) {
                                    msgBuffer.append('\n');
                                }
                                msgBuffer.append(line);
                            });

                            String newText = msgBuffer.toString();
                            //System.out.println("---\n" + newText + "\n---");

                            result = new InferredTypesImplementation();

                            String[] types = newText.split("\n");
                            for (String type : types) {
                                result.add(type);
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace(); // no ! nothing in fact
                    } finally {
                        if (bsc != null) {
                            bsc.destroy();
                        }
                    }
                }
            }
        }

        return result;
    }
}

@@ -122,16 +122,63 @@
 ations;%0A
+import com.intellij.openapi.diagnostic.Logger;%0A
 import c
@@ -214,16 +214,16 @@
 roject;%0A
-
 import c
@@ -637,16 +637,48 @@
 vice %7B%0A%0A
+    private final Logger m_log;%0A
     priv
@@ -876,24 +876,78 @@
  bsbPath) %7B%0A
+        m_log = Logger.getInstance(%22ReasonML.types%22);%0A
         m_pr
@@ -1707,17 +1707,17 @@
 cmiFile 
-!
+=
 = null) 
@@ -1710,32 +1710,153 @@
 File == null) %7B%0A
+                m_log.warn(%22can't read types for %22 + filePath + %22, cmi not found: %22 + cmiFilename);%0A            %7D else %7B%0A
                 
@@ -3939,52 +3939,63 @@
     
-e.printStackTrace(); // no ! nothing in fact
+m_log.error(%22An error occurred when reading types%22, e);
 %0A   

add log to type component