package jetbrains.mps.vcs.platform.actions;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import jetbrains.mps.workbench.action.BaseAction;
import javax.swing.Icon;
import jetbrains.mps.workbench.action.ActionAccess;
import com.intellij.openapi.actionSystem.AnActionEvent;
import java.util.Map;
import jetbrains.mps.ide.actions.MPSCommonDataKeys;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.project.MPSProject;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.util.NameUtil;

@GeneratedClass(node = "r:c29f530b-f74d-4627-9da2-61138cfa6722(jetbrains.mps.vcs.platform.actions)/6771236331600998130", model = "r:c29f530b-f74d-4627-9da2-61138cfa6722(jetbrains.mps.vcs.platform.actions)")
public class ShowNodeHistory_Action extends BaseAction {
  private static final Icon ICON = null;

  public ShowNodeHistory_Action() {
    super("Show Node History", "", ICON);
    this.setIsAlwaysVisible(false);
    this.setActionAccess(ActionAccess.READ_PROJECT);
    updateInBackground(true);
  }
  @Override
  public boolean isDumbAware() {
    return true;
  }
  @Override
  public boolean isApplicable(AnActionEvent event, final Map<String, Object> _params) {
    NodeHistoryUtil nh = new NodeHistoryUtil(event.getData(MPSCommonDataKeys.MPS_PROJECT));
    nh.initFileAndVcs(event.getData(MPSCommonDataKeys.NODE));
    return nh.isHistoryTracked();
  }
  @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;
      }
    }
    {
      SNode node = event.getData(MPSCommonDataKeys.NODE);
      if (node == null) {
        return false;
      }
    }
    return true;
  }
  @Override
  public void doExecute(@NotNull final AnActionEvent event, final Map<String, Object> _params) {
    SNode containingRoot = SNodeOperations.getContainingRoot(event.getData(MPSCommonDataKeys.NODE));
    final String modelName = NameUtil.compactModelName(SNodeOperations.getModel(event.getData(MPSCommonDataKeys.NODE)).getReference());
    final String fmt = (event.getData(MPSCommonDataKeys.NODE) == containingRoot ? "%s/%s" : "%s/%s/%s");
    final String dialogTitle = String.format(fmt, modelName, SNodeOperations.present(containingRoot), SNodeOperations.present(event.getData(MPSCommonDataKeys.NODE)));
    NodeHistoryUtil nh = new NodeHistoryUtil(event.getData(MPSCommonDataKeys.MPS_PROJECT));
    nh.initFileAndVcs(event.getData(MPSCommonDataKeys.NODE));
    nh.showNodeHistory(event.getData(MPSCommonDataKeys.NODE).getNodeId(), dialogTitle, true);
  }
}
