package jetbrains.mps.ide.make.actions;

/*Generated by MPS */

import jetbrains.mps.workbench.action.BaseAction;
import javax.swing.Icon;
import com.intellij.openapi.actionSystem.AnActionEvent;
import java.util.Map;
import jetbrains.mps.ide.actions.MPSCommonDataKeys;
import jetbrains.mps.make.MakeServiceComponent;
import javax.swing.tree.TreeNode;
import jetbrains.mps.ide.ui.tree.module.NamespaceTextNode;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.project.MPSProject;
import java.util.List;
import org.jetbrains.mps.openapi.module.SModule;
import java.util.ArrayList;
import jetbrains.mps.internal.collections.runtime.ListSequence;

public class MakeNamespace_Action extends BaseAction {
  private static final Icon ICON = null;

  private boolean cleanMake;
  public MakeNamespace_Action(boolean cleanMake_par) {
    super("Make", "", ICON);
    this.cleanMake = cleanMake_par;
    this.setIsAlwaysVisible(false);
    this.setExecuteOutsideCommand(true);
  }
  @Override
  public boolean isDumbAware() {
    return true;
  }
  @Override
  public boolean isApplicable(AnActionEvent event, final Map<String, Object> _params) {
    if (event.getData(MPSCommonDataKeys.MPS_PROJECT).getComponent(MakeServiceComponent.class).isSessionActive()) {
      return false;
    }
    for (TreeNode selectedNode : event.getData(MPSCommonDataKeys.TREE_NODES)) {
      if (!(selectedNode instanceof NamespaceTextNode)) {
        return false;
      }
    }
    String text = new MakeActionParameters(event.getData(MPSCommonDataKeys.MPS_PROJECT)).modules(MakeNamespace_Action.this.selectedModules(event)).cleanMake(MakeNamespace_Action.this.cleanMake).actionText();
    if (text != null) {
      event.getPresentation().setText(text);
      return true;
    }
    return false;
  }
  @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;
    }
    {
      MPSProject p = event.getData(MPSCommonDataKeys.MPS_PROJECT);
      if (p == null) {
        return false;
      }
    }
    {
      List<TreeNode> p = event.getData(MPSCommonDataKeys.TREE_NODES);
      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) {
    new MakeActionImpl(new MakeActionParameters(event.getData(MPSCommonDataKeys.MPS_PROJECT)).modules(MakeNamespace_Action.this.selectedModules(event)).cleanMake(MakeNamespace_Action.this.cleanMake)).executeAction();

  }
  @NotNull
  public String getActionId() {
    StringBuilder res = new StringBuilder();
    res.append(super.getActionId());
    res.append("#");
    res.append(((Object) this.cleanMake).toString());
    res.append("!");
    return res.toString();
  }
  private List<SModule> selectedModules(final AnActionEvent event) {
    List<SModule> modules = new ArrayList<SModule>();
    for (TreeNode ppNode : ListSequence.fromList(event.getData(MPSCommonDataKeys.TREE_NODES))) {
      for (SModule module : ListSequence.fromList(((NamespaceTextNode) ppNode).getModulesUnder())) {
        modules.add(module);
      }
    }
    return modules;
  }
}
