package jetbrains.mps.ide.refactoring;

/*Generated by MPS */

import jetbrains.mps.ide.platform.refactoring.RefactoringAccessEx;
import jetbrains.mps.core.platform.Platform;
import jetbrains.mps.ide.platform.refactoring.ModelElementTargetChooser;
import com.intellij.openapi.project.Project;
import org.jetbrains.mps.openapi.model.SModel;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.ide.platform.refactoring.RefactoringViewAction;
import jetbrains.mps.ide.findusages.model.SearchResults;
import jetbrains.mps.ide.findusages.model.SearchTask;
import jetbrains.mps.ide.refactoring.plugin.RefactoringView_Tool;
import jetbrains.mps.plugins.projectplugins.ProjectPluginManager;
import jetbrains.mps.refactoring.framework.RefactoringContext;
import jetbrains.mps.ide.project.ProjectHelper;
import com.intellij.openapi.ui.Messages;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import java.util.ArrayList;

public class RefactoringAccessImpl extends RefactoringAccessEx {
  private static final int MAX_SEARCH_RESULTS = 30000;

  public RefactoringAccessImpl(Platform mpsPlatform) {
    super(mpsPlatform);
    // MPS Workbench-specific Refactoring Access implementation
  }

  @Override
  public ModelElementTargetChooser createTargetChooser(Project project, SModel model) {
    ModelOrNodeChooser rv = new ModelOrNodeChooser(project);
    rv.select(model);
    return rv;
  }

  @Override
  public ModelElementTargetChooser createTargetChooser(Project project, SNode node) {
    ModelOrNodeChooser rv = new ModelOrNodeChooser(project);
    rv.select(node);
    return rv;
  }

  @Override
  public void showRefactoringView(Project project, RefactoringViewAction refactoringViewAction, Runnable disposeAction, SearchResults searchResults, SearchTask searchTask, String name) {
    RefactoringView_Tool refactoringView = project.getComponent(ProjectPluginManager.class).getTool(RefactoringView_Tool.class);
    refactoringView.showRefactoringView(project, refactoringViewAction, disposeAction, truncateSearchResults(project, searchResults), searchTask, name);
  }

  @Override
  public void showRefactoringView(RefactoringContext refactoringContext, RefactoringViewAction refactoringViewAction, Runnable disposeAction, SearchResults searchResults, SearchTask searchTask, String name) {
    jetbrains.mps.project.Project mpsProject = refactoringContext.getSelectedProject();
    Project ideaProject = ProjectHelper.toIdeaProject(mpsProject);
    RefactoringView_Tool refactoringView = ideaProject.getComponent(ProjectPluginManager.class).getTool(RefactoringView_Tool.class);
    refactoringView.showRefactoringView(refactoringContext, refactoringViewAction, disposeAction, truncateSearchResults(ideaProject, searchResults), searchTask);
  }

  private SearchResults truncateSearchResults(Project project, SearchResults searchResults) {
    if (searchResults.getSearchResults2().size() > MAX_SEARCH_RESULTS) {
      Messages.showWarningDialog(project, "More than " + MAX_SEARCH_RESULTS + " usages found. Only first " + MAX_SEARCH_RESULTS + " results will be displayed.", "Refactor");

      SearchResults truncatedSearchResults = new SearchResults<>(searchResults.getSearchedObjects(), ListSequence.fromList(ListSequence.fromListWithValues(new ArrayList<>(), searchResults.getSearchResults2())).take(MAX_SEARCH_RESULTS).toList());
      return truncatedSearchResults;
    }
    return searchResults;
  }
}
