/*
 * Copyright 2000-2015 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 com.intellij.ide.util.frameworkSupport;

import com.intellij.CommonBundle;
import com.intellij.facet.impl.DefaultFacetsProvider;
import com.intellij.framework.addSupport.FrameworkSupportInModuleProvider;
import com.intellij.ide.util.newProjectWizard.AddSupportForFrameworksPanel;
import com.intellij.ide.util.newProjectWizard.impl.FrameworkSupportModelBase;
import com.intellij.openapi.application.Result;
import com.intellij.openapi.application.WriteAction;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.ProjectBundle;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainer;
import com.intellij.openapi.roots.ui.configuration.projectRoot.LibrariesContainerFactory;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.util.List;

/**
 * @author nik
 */
public class AddFrameworkSupportDialog extends DialogWrapper {
  private final AddSupportForFrameworksPanel myAddSupportPanel;
  private final Module myModule;

  private AddFrameworkSupportDialog(@NotNull Module module, final @NotNull String contentRootPath, final List<FrameworkSupportInModuleProvider> providers) {
    super(module.getProject(), true);
    setTitle(ProjectBundle.message("dialog.title.add.frameworks.support"));
    myModule = module;
    final LibrariesContainer container = LibrariesContainerFactory.createContainer(module.getProject());
    final FrameworkSupportModelBase model = new FrameworkSupportModelImpl(module.getProject(), contentRootPath, container);
    myAddSupportPanel = new AddSupportForFrameworksPanel(providers, model, false, null) {
      @Override
      protected void onFrameworkStateChanged() {
        setOKActionEnabled(isOKActionEnabled());
      }
    };
    
    setOKActionEnabled(isOKActionEnabled());
    Disposer.register(myDisposable, myAddSupportPanel);
    init();
  }

  @Nullable
  public static AddFrameworkSupportDialog createDialog(@NotNull Module module) {
    VirtualFile[] roots = ModuleRootManager.getInstance(module).getContentRoots();
    if (roots.length == 0) return null;

    List<FrameworkSupportInModuleProvider> providers = FrameworkSupportUtil.getProviders(module, DefaultFacetsProvider.INSTANCE);
    if (providers.isEmpty()) return null;

    return new AddFrameworkSupportDialog(module, roots[0].getPath(), providers);
  }

  public static boolean isAvailable(@NotNull Module module) {
    VirtualFile[] roots = ModuleRootManager.getInstance(module).getContentRoots();
    return roots.length != 0 && FrameworkSupportUtil.hasProviders(module, DefaultFacetsProvider.INSTANCE);
  }

  @Override
  public boolean isOKActionEnabled() {
    return myAddSupportPanel.hasSelectedFrameworks();
  }

  protected void doOKAction() {
    if (myAddSupportPanel.hasSelectedFrameworks()) {
      if (!myAddSupportPanel.downloadLibraries()) {
        int answer = Messages.showYesNoDialog(myAddSupportPanel.getMainPanel(),
                                              ProjectBundle.message("warning.message.some.required.libraries.wasn.t.downloaded"),
                                              CommonBundle.getWarningTitle(), Messages.getWarningIcon());
        if (answer != Messages.YES) {
          return;
        }
      }

      new WriteAction() {
        protected void run(@NotNull final Result result) {
          ModifiableRootModel model = ModuleRootManager.getInstance(myModule).getModifiableModel();
          myAddSupportPanel.addSupport(myModule, model);
          model.commit();
        }
      }.execute();
    }
    super.doOKAction();
  }

  @Override
  protected String getDimensionServiceKey() {
    return "#com.intellij.ide.util.frameworkSupport.AddFrameworkSupportDialog";
  }

  @Override
  protected String getHelpId() {
    return "reference.frameworks.support.dialog";
  }

  protected JComponent createCenterPanel() {
    return myAddSupportPanel.getMainPanel();
  }

  @Override
  public JComponent getPreferredFocusedComponent() {
    return myAddSupportPanel.getFrameworksTree();
  }
}

@@ -1100,24 +1100,129 @@
 ule.Module;%0A
+import com.intellij.openapi.project.DumbModePermission;%0Aimport com.intellij.openapi.project.DumbService;%0A
 import com.i
@@ -4268,16 +4268,170 @@
      %7D%0A%0A
+      DumbService.allowStartingDumbModeInside(DumbModePermission.MAY_START_BACKGROUND, new Runnable() %7B%0A        @Override%0A        public void run() %7B%0A    
       ne
@@ -4444,24 +4444,28 @@
 eAction() %7B%0A
+    
         prot
@@ -4507,24 +4507,28 @@
 t result) %7B%0A
+    
           Mo
@@ -4621,24 +4621,28 @@
 ;%0A          
+    
 myAddSupport
@@ -4686,16 +4686,20 @@
         
+    
 model.co
@@ -4702,24 +4702,28 @@
 l.commit();%0A
+    
         %7D%0A  
@@ -4726,16 +4726,20 @@
 %7D%0A      
+    
 %7D.execut
@@ -4739,24 +4739,44 @@
 .execute();%0A
+        %7D%0A      %7D);%0A
     %7D%0A    su

enable bg dumb mode in add framework support (EA-71345 - Throwable: DumbServiceImpl.scheduleCacheUpdate)