package com.redhat.ceylon.eclipse.core.launch;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.model.LaunchConfigurationDelegate;
import org.eclipse.jface.dialogs.MessageDialog;

import com.redhat.ceylon.compiler.js.Runner;
import com.redhat.ceylon.eclipse.code.editor.Util;

import static org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME;

public class JsLaunchDelegate extends LaunchConfigurationDelegate {

    @Override
    public void launch(ILaunchConfiguration configuration, String mode,
            ILaunch launch, IProgressMonitor monitor) throws CoreException {

        String qname = configuration.getAttribute(ATTR_MAIN_TYPE_NAME, "::run");
        String methname = qname.substring(qname.indexOf("::")+2);
        String modname = configuration.getAttribute(ICeylonLaunchConfigurationConstants.ATTR_CEYLON_MODULE, "default");
        System.out.println("Launching " + methname + " from " + modname);
        ArrayList<String> repos = new ArrayList<String>(4);
        //Add workspace repo
        //Add project repo?
        //Add user repo
        repos.add("modules");
        try {
            Runner.run(repos, modname, methname);
        } catch (FileNotFoundException ex) {
            //Install node.js
            System.err.println(ex.getMessage());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

@@ -99,32 +99,60 @@
 io.IOException;%0A
+import java.io.PrintStream;%0A
 import java.util
@@ -440,82 +440,167 @@
 pse.
-jface.dialogs.MessageDialog;%0A%0Aimport com.redhat.ceylon.compiler.js.Runner;
+ui.console.ConsolePlugin;%0Aimport org.eclipse.ui.console.IConsole;%0Aimport org.eclipse.ui.console.IConsoleManager;%0Aimport org.eclipse.ui.console.MessageConsole;%0A
 %0Aimp
@@ -625,32 +625,26 @@
 lon.
-eclipse.code.editor.Util
+compiler.js.Runner
 ;%0A%0Ai
@@ -803,24 +803,648 @@
 Delegate %7B%0A%0A
+    private MessageConsole findConsole() %7B%0A        ConsolePlugin plugin = ConsolePlugin.getDefault();%0A        IConsoleManager conman = plugin.getConsoleManager();%0A        MessageConsole cons = null;%0A        for (IConsole ccons : conman.getConsoles()) %7B%0A            if (%22com.redhat.ceylon%22.equals(ccons.getName())) %7B%0A                cons = (MessageConsole)ccons;%0A                break;%0A            %7D%0A        %7D%0A        if (cons == null) %7B%0A            cons = new MessageConsole(%22com.redhat.ceylon%22, null);%0A            conman.addConsoles(new IConsole%5B%5D%7Bcons%7D);%0A        %7D%0A        cons.clearConsole();%0A        return cons;%0A    %7D%0A%0A
     @Overrid
@@ -1591,24 +1591,75 @@
 xception %7B%0A%0A
+        //Check that JS is enabled for the project%0A
         Stri
@@ -2154,24 +2154,101 @@
 %22modules%22);%0A
+        PrintStream pout = new PrintStream(findConsole().newOutputStream());%0A
         try 
@@ -2292,24 +2292,30 @@
 me, methname
+, pout
 );%0A        %7D
@@ -2658,32 +2658,78 @@
 ntStackTrace();%0A
+        %7D finally %7B%0A            pout.close();%0A
         %7D%0A    %7D%0A

send output to the console