/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.plugins.groovy.runner;

import com.intellij.execution.CantRunException;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.configurations.JavaParameters;
import com.intellij.execution.configurations.RunProfile;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.projectRoots.JavaSdkType;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.OrderRootType;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.util.PathUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.plugins.groovy.config.GroovyConfigUtils;
import org.jetbrains.plugins.groovy.util.LibrariesUtil;

import java.io.File;
import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.regex.Pattern;

/**
 * @author peter
 */
public abstract class GroovyScriptRunner {

  public abstract boolean isValidModule(@NotNull Module module);

  public abstract boolean ensureRunnerConfigured(@Nullable Module module, RunProfile profile, final Project project) throws ExecutionException;

  public abstract void configureCommandLine(JavaParameters params, @Nullable Module module, boolean tests, VirtualFile script,
                                            GroovyScriptRunConfiguration configuration) throws CantRunException;

  protected static String getConfPath(final String groovyHomePath) {
    String confpath = FileUtil.toSystemDependentName(groovyHomePath + "/conf/groovy-starter.conf");
    if (new File(confpath).exists()) {
      return confpath;
    }

    try {
      final String jarPath = PathUtil.getJarPathForClass(GroovyScriptRunner.class);
      if (new File(jarPath).isFile()) { //jar; distribution mode
        return new File(jarPath, "../groovy-starter.conf").getCanonicalPath();
      }

      //else, it's directory in out, development mode
      return new File(jarPath, "conf/groovy-starter.conf").getCanonicalPath();
    }
    catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  protected static void setGroovyHome(JavaParameters params, String groovyHome) {
    params.getVMParametersList().add("-Dgroovy.home=" + groovyHome);
    if (groovyHome.contains("grails")) { //a bit of a hack
      params.getVMParametersList().add("-Dgrails.home=" + groovyHome);
    }
    if (groovyHome.contains("griffon")) { //a bit of a hack
      params.getVMParametersList().add("-Dgriffon.home=" + groovyHome);
    }
  }

  protected static void setToolsJar(JavaParameters params) {
    Sdk jdk = params.getJdk();
    if (jdk != null && jdk.getSdkType() instanceof JavaSdkType) {
      String toolsPath = ((JavaSdkType)jdk.getSdkType()).getToolsPath(jdk);
      if (toolsPath != null) {
        params.getVMParametersList().add("-Dtools.jar=" + toolsPath);
      }
    }
  }

  @Nullable
  protected static VirtualFile findGroovyJar(@NotNull Module module) {
    final Pattern pattern = Pattern.compile(".*[\\\\/]groovy[^\\\\/]*jar");
    for (Library library : GroovyConfigUtils.getInstance().getSDKLibrariesByModule(module)) {
      for (VirtualFile root : library.getFiles(OrderRootType.CLASSES)) {
        if (pattern.matcher(root.getPresentableUrl()).matches()) {
          return root;
        }
      }
    }
    return null;
  }

  protected static void addClasspathFromRootModel(@Nullable Module module, boolean isTests, JavaParameters params) throws CantRunException {
    if (module == null) {
      return;
    }

    final JavaParameters tmp = new JavaParameters();
    tmp.configureByModule(module, isTests ? JavaParameters.CLASSES_AND_TESTS : JavaParameters.CLASSES_ONLY);
    if (tmp.getClassPath().getVirtualFiles().isEmpty()) {
      return;
    }


    final boolean embeddable =
      LibrariesUtil.isEmbeddableDistribution(ModuleRootManager.getInstance(module).getFiles(OrderRootType.CLASSES));
    if (embeddable) {
      params.getProgramParametersList().add("--classpath");
      params.getProgramParametersList().add(tmp.getClassPath().getPathsString());
      return;
    }

    final LinkedHashSet<String> pathList = new LinkedHashSet<String>(tmp.getClassPath().getPathList());
    for (Library library : GroovyConfigUtils.getInstance().getSDKLibrariesByModule(module)) {
      for (VirtualFile file : library.getFiles(OrderRootType.CLASSES)) {
        pathList.remove(file.getPresentableUrl());
      }
    }
    if (pathList.isEmpty()) {
      return;
    }

    params.getProgramParametersList().add("--classpath");
    params.getProgramParametersList().add(StringUtil.join(pathList, File.pathSeparator));
  }

}

@@ -1048,61 +1048,8 @@
 dk;%0A
-import com.intellij.openapi.roots.ModuleRootManager;%0A
 impo
@@ -1196,58 +1196,8 @@
 il;%0A
-import com.intellij.openapi.util.text.StringUtil;%0A
 impo
@@ -1422,64 +1422,8 @@
 ils;
-%0Aimport org.jetbrains.plugins.groovy.util.LibrariesUtil;
 %0A%0Aim
@@ -1473,40 +1473,8 @@
 on;%0A
-import java.util.LinkedHashSet;%0A
 impo
@@ -4402,181 +4402,8 @@
  %7D%0A%0A
-%0A    final boolean embeddable =%0A      LibrariesUtil.isEmbeddableDistribution(ModuleRootManager.getInstance(module).getFiles(OrderRootType.CLASSES));%0A    if (embeddable) %7B%0A  
     
@@ -4456,18 +4456,16 @@
 path%22);%0A
-  
     para
@@ -4540,564 +4540,8 @@
 ));%0A
-      return;%0A    %7D%0A%0A    final LinkedHashSet%3CString%3E pathList = new LinkedHashSet%3CString%3E(tmp.getClassPath().getPathList());%0A    for (Library library : GroovyConfigUtils.getInstance().getSDKLibrariesByModule(module)) %7B%0A      for (VirtualFile file : library.getFiles(OrderRootType.CLASSES)) %7B%0A        pathList.remove(file.getPresentableUrl());%0A      %7D%0A    %7D%0A    if (pathList.isEmpty()) %7B%0A      return;%0A    %7D%0A%0A    params.getProgramParametersList().add(%22--classpath%22);%0A    params.getProgramParametersList().add(StringUtil.join(pathList, File.pathSeparator));%0A
   %7D%0A

put all the module dependencies to groovy/gant script's --classpath (IDEA-53978)