package jetbrains.mps.ide.refactoring.plugin;

/*Generated by MPS */

import jetbrains.mps.workbench.action.BaseAction;
import javax.swing.Icon;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.actionSystem.AnActionEvent;
import java.util.Map;
import org.jetbrains.mps.openapi.module.SModule;
import jetbrains.mps.internal.collections.runtime.MapSequence;
import jetbrains.mps.project.AbstractModule;
import jetbrains.mps.ide.actions.MPSCommonDataKeys;
import jetbrains.mps.project.MPSProject;
import jetbrains.mps.ide.refactoring.RenameModuleDialog;
import jetbrains.mps.project.Solution;
import jetbrains.mps.ide.IdeBundle;
import jetbrains.mps.smodel.Language;
import jetbrains.mps.project.DevKit;
import jetbrains.mps.smodel.Generator;

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

  public RenameModule_Action() {
    super("Rename Module", "", ICON);
    this.setIsAlwaysVisible(false);
    this.setExecuteOutsideCommand(true);
    updateInBackground(true);
  }
  @Override
  public boolean isDumbAware() {
    return true;
  }
  @Override
  public void doUpdate(@NotNull AnActionEvent event, final Map<String, Object> _params) {
    boolean isApplicable = ((SModule) MapSequence.fromMap(_params).get("module")) instanceof AbstractModule && !(((SModule) MapSequence.fromMap(_params).get("module")).isReadOnly());
    event.getPresentation().setText(RenameModule_Action.this.getActionText(((SModule) MapSequence.fromMap(_params).get("module")), _params));
    event.getPresentation().setEnabledAndVisible(isApplicable);
  }
  @Override
  protected boolean collectActionData(AnActionEvent event, final Map<String, Object> _params) {
    if (!(super.collectActionData(event, _params))) {
      return false;
    }
    {
      SModule p = event.getData(MPSCommonDataKeys.MODULE);
      MapSequence.fromMap(_params).put("module", p);
      if (p == null) {
        return false;
      }
    }
    {
      MPSProject p = event.getData(MPSCommonDataKeys.MPS_PROJECT);
      MapSequence.fromMap(_params).put("project", p);
      if (p == null) {
        return false;
      }
    }
    return true;
  }
  @Override
  public void doExecute(@NotNull final AnActionEvent event, final Map<String, Object> _params) {
    new RenameModuleDialog(((MPSProject) MapSequence.fromMap(_params).get("project")), ((AbstractModule) ((SModule) MapSequence.fromMap(_params).get("module")))).show();
  }
  public String getActionText(SModule module, final Map<String, Object> _params) {
    if (module instanceof Solution) {
      return IdeBundle.message("actions.module.rename.text.solution");
    }
    if (module instanceof Language) {
      return IdeBundle.message("actions.module.rename.text.language");
    }
    if (module instanceof DevKit) {
      return IdeBundle.message("actions.module.rename.text.devkit");
    }
    if (module instanceof Generator) {
      return IdeBundle.message("actions.module.rename.text.generator");
    }

    return IdeBundle.message("actions.module.rename.text.module");
  }
}
