package jetbrains.mps.ide.modelchecker.actions;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import jetbrains.mps.workbench.action.BaseAction;
import javax.swing.Icon;
import jetbrains.mps.icons.MPSIcons;
import com.intellij.openapi.actionSystem.AnActionEvent;
import java.util.Map;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import jetbrains.mps.internal.collections.runtime.MapSequence;
import java.util.List;
import javax.swing.tree.TreeNode;
import jetbrains.mps.ide.actions.MPSCommonDataKeys;
import org.jetbrains.mps.openapi.module.SModule;
import jetbrains.mps.ide.modelchecker.platform.actions.ModelCheckerTool;
import java.util.ArrayList;
import jetbrains.mps.ide.ui.tree.module.NamespaceTextNode;

@GeneratedClass(node = "r:e2c8c94a-404b-4b97-a3a4-c76946bd1913(jetbrains.mps.ide.modelchecker.actions)/7502735747194136940", model = "r:e2c8c94a-404b-4b97-a3a4-c76946bd1913(jetbrains.mps.ide.modelchecker.actions)")
public class CheckNamespace_Action extends BaseAction {
  private static final Icon ICON = MPSIcons.General.ModelChecker;

  public CheckNamespace_Action() {
    super("Check Selected Modules", "Check modules for errors", ICON);
    this.setIsAlwaysVisible(false);
    this.setExecuteOutsideCommand(true);
  }
  @Override
  public boolean isDumbAware() {
    return true;
  }
  @Override
  public boolean isApplicable(AnActionEvent event, final Map<String, Object> _params) {
    return ListSequence.fromList(CheckNamespace_Action.this.modules2check(_params)).isNotEmpty();
  }
  @Override
  public void doUpdate(@NotNull AnActionEvent event, final Map<String, Object> _params) {
    this.setEnabledState(event.getPresentation(), this.isApplicable(event, _params));
  }
  @Override
  protected boolean collectActionData(AnActionEvent event, final Map<String, Object> _params) {
    if (!(super.collectActionData(event, _params))) {
      return false;
    }
    {
      Project p = event.getData(CommonDataKeys.PROJECT);
      MapSequence.fromMap(_params).put("project", p);
      if (p == null) {
        return false;
      }
    }
    {
      List<TreeNode> p = event.getData(MPSCommonDataKeys.TREE_NODES);
      MapSequence.fromMap(_params).put("treeNodes", p);
      if (p == null) {
        return false;
      }
      if (p.isEmpty()) {
        return false;
      }
    }
    return true;
  }
  @Override
  public void doExecute(@NotNull final AnActionEvent event, final Map<String, Object> _params) {
    List<SModule> modules = CheckNamespace_Action.this.modules2check(_params);
    ModelCheckerTool.getInstance(((Project) MapSequence.fromMap(_params).get("project"))).checkModulesAndShowResult(modules);
  }
  private List<SModule> modules2check(final Map<String, Object> _params) {
    List<SModule> modules = ListSequence.fromList(new ArrayList<SModule>());
    for (TreeNode node : ListSequence.fromList(((List<TreeNode>) MapSequence.fromMap(_params).get("treeNodes")))) {
      if (!(node instanceof NamespaceTextNode)) {
        return ListSequence.fromList(new ArrayList<SModule>());
      }
      ListSequence.fromList(modules).addSequence(ListSequence.fromList(((NamespaceTextNode) node).getModulesUnder()));
    }
    return modules;
  }
}
