package jetbrains.mps.lang.script.pluginSolution.plugin;

/*Generated by MPS */

import jetbrains.mps.ide.tools.BaseTabbedProjectTool;
import javax.swing.Icon;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindowAnchor;
import java.util.Collection;
import jetbrains.mps.lang.script.runtime.RefactoringScript;
import org.jetbrains.mps.openapi.module.SearchScope;
import jetbrains.mps.ide.ThreadUtils;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.progress.Task;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.progress.ProgressIndicator;
import jetbrains.mps.ide.script.migrationtool.MigrationScriptFinder;
import jetbrains.mps.ide.findusages.model.IResultProvider;
import jetbrains.mps.ide.findusages.view.FindUtils;
import jetbrains.mps.ide.findusages.model.SearchQuery;
import jetbrains.mps.ide.findusages.model.SearchResults;
import jetbrains.mps.smodel.ModelAccessHelper;
import jetbrains.mps.ide.project.ProjectHelper;
import jetbrains.mps.util.Computable;
import jetbrains.mps.progress.ProgressMonitorAdapter;
import javax.swing.JOptionPane;
import jetbrains.mps.ide.findusages.view.icons.IconManager;
import javax.swing.JComponent;

public class MigrationScriptsTool_Tool extends BaseTabbedProjectTool {
  private static final Icon ICON = null;
  public MigrationScriptsTool_Tool(Project project) {
    super(project, "Migration", null, ICON, ToolWindowAnchor.BOTTOM, true);
  }
  public void startMigration(final Collection<RefactoringScript> scripts, final SearchScope scope) {
    final Project ideaProject = super.getProject();
    ThreadUtils.assertEDT();
    ApplicationManager.getApplication().invokeLater(new Runnable() {
      @Override
      public void run() {
        ProgressManager.getInstance().run(new Task.Modal(ideaProject, "Searching", true) {
          @Override
          public void run(@NotNull final ProgressIndicator indicator) {
            indicator.setIndeterminate(true);
            final MigrationScriptFinder finder = new MigrationScriptFinder(scripts);
            final IResultProvider provider = FindUtils.makeProvider(finder);
            final SearchQuery query = new SearchQuery(scope);
            final SearchResults results = new ModelAccessHelper(ProjectHelper.getModelAccess(getProject())).runReadAction(new Computable<SearchResults>() {
              public SearchResults compute() {
                return FindUtils.getSearchResults(new ProgressMonitorAdapter(indicator), query, provider);
              }
            });
            ApplicationManager.getApplication().invokeLater(new Runnable() {
              @Override
              public void run() {
                if (results.getSearchResults2().isEmpty()) {
                  JOptionPane.showMessageDialog(MigrationScriptsTool_Tool.this.getToolWindow().getComponent(), "No applicable nodes found", "Migration Scripts", JOptionPane.INFORMATION_MESSAGE);
                } else {
                  MigrationScriptsTool_Tool.this.getToolWindow().getContentManager().removeAllContents(true);
                  MigrationScriptsTool_Tool.this.addTab(finder, provider, query);
                }
              }
            });
          }
        });
      }
    });
  }
  /*package*/ void addTab(final MigrationScriptFinder finder, final IResultProvider provider, final SearchQuery query) {
    ThreadUtils.assertEDT();
    final MigrationScriptsView view = new MigrationScriptsView(finder, provider, query, MigrationScriptsTool_Tool.this, super.getProject()) {
      @Override
      public void close() {
        MigrationScriptsTool_Tool.this.closeTab(this.getComponent());
      }
    };
    String tabName = "    ";
    MigrationScriptsTool_Tool.this.addTab(view.getComponent(), tabName, IconManager.getIconForIHolder(query.getObjectHolder()), (JComponent c) -> view.getUsagesView().dispose(), true);
  }
}
