/*
 * Copyright 2000-2014 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.config;

import com.intellij.openapi.application.PathManager;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.module.JavaModuleType;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.roots.LibraryOrderEntry;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.ui.Messages;
import icons.JetgroovyIcons;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
import org.jetbrains.plugins.groovy.GroovyBundle;
import org.jetbrains.plugins.groovy.util.LibrariesUtil;

import java.io.File;

public class GroovyFacetUtil {
  public static final String PLUGIN_MODULE_ID = "PLUGIN_MODULE";

  public static boolean tryToSetUpGroovyFacetOnTheFly(final Module module) {
    final Project project = module.getProject();
    GroovyConfigUtils utils = GroovyConfigUtils.getInstance();
    final Library[] libraries = utils.getAllSDKLibraries(project);
    if (libraries.length > 0) {
      final Library library = libraries[0];
      int result = Messages
        .showOkCancelDialog(
          GroovyBundle.message("groovy.like.library.found.text", module.getName(), library.getName(), utils.getSDKLibVersion(library)),
          GroovyBundle.message("groovy.like.library.found"), JetgroovyIcons.Groovy.Groovy_32x32);
      if (result == Messages.OK) {
        WriteAction.run(() -> {
          ModifiableRootModel model = ModuleRootManager.getInstance(module).getModifiableModel();
          LibraryOrderEntry entry = model.addLibraryEntry(libraries[0]);
          LibrariesUtil.placeEntryToCorrectPlace(model, entry);
          model.commit();
        });
        return true;
      }
    }
    return false;
  }

  public static boolean isSuitableModule(Module module) {
    if (module == null) return false;
    return isAcceptableModuleType(ModuleType.get(module));
  }

  public static boolean isAcceptableModuleType(ModuleType type) {
    return type instanceof JavaModuleType || PLUGIN_MODULE_ID.equals(type.getId()) || "ANDROID_MODULE".equals(type.getId());
  }

  public static File getBundledGroovyJar() {
    String jarPath = PathManager.getJarPathForClass(DefaultGroovyMethods.class);
    assert jarPath != null : "Cannot find JAR containing groovy classes";
    File jar = new File(jarPath);
    assert GroovyConfigUtils.GROOVY_ALL_JAR_PATTERN.matcher(jar.getName()).matches() : "Incorrect path to groovy JAR: " + jarPath;
    return jar;
  }
}

@@ -1198,84 +1198,60 @@
 ort 
-icons.JetgroovyIcons;%0Aimport org.codehaus.groovy.runtime.DefaultGroovyMethod
+groovy.lang.GroovyObject;%0Aimport icons.JetgroovyIcon
 s;%0Ai
@@ -2953,28 +2953,20 @@
 ass(
-DefaultGroovyMethods
+GroovyObject
 .cla

use GroovyObject class to locate groovy-all.jar (IDEA-CR-23991)