package jetbrains.mps.ide.actions;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import jetbrains.mps.workbench.action.BaseAction;
import javax.swing.Icon;
import com.intellij.openapi.actionSystem.AnActionEvent;
import java.util.Map;
import jetbrains.mps.internal.collections.runtime.MapSequence;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.diagnostic.IdeErrorsDialog;
import com.intellij.unscramble.AnalyzeStacktraceUtil;
import java.io.StringWriter;
import java.io.PrintWriter;
import com.intellij.unscramble.AnalyzeStacktraceAction;

@GeneratedClass(node = "r:00000000-0000-4000-0000-011c895904a4(jetbrains.mps.ide.actions)/4221956679900513437", model = "r:00000000-0000-4000-0000-011c895904a4(jetbrains.mps.ide.actions)")
public class AnalyzeStacktrace_Action extends BaseAction {
  private static final Icon ICON = null;

  public AnalyzeStacktrace_Action() {
    super("Analyze Stacktrace...", "Open console with the navigation stacktrace", ICON);
    this.setIsAlwaysVisible(false);
    this.setExecuteOutsideCommand(true);
    this.setMnemonic("S".charAt(0));
    updateInBackground(true);
  }
  @Override
  public boolean isDumbAware() {
    return true;
  }
  @Override
  public boolean isApplicable(AnActionEvent event, final Map<String, Object> _params) {
    // In messages tool window show action only on lines with exception
    if (MPSActionPlaces.MPS_MESSAGES_POPUP.equals(event.getPlace())) {
      return ((Throwable) MapSequence.fromMap(_params).get("exception")) != null;
    }
    // project is required for platform AnalyzeStacktraceAction action,
    // so we leave it in parameters and use it to avoid warning
    return ((Project) MapSequence.fromMap(_params).get("project")) != null;
  }
  @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;
      }
    }
    {
      Throwable p = event.getData(MPSCommonDataKeys.EXCEPTION);
      MapSequence.fromMap(_params).put("exception", p);
    }
    return true;
  }
  @Override
  public void doExecute(@NotNull final AnActionEvent event, final Map<String, Object> _params) {
    String message = event.getData(IdeErrorsDialog.CURRENT_TRACE_KEY);
    if (message != null) {
      // In case we are in IDE Fatal Errors dialog just show trace from dialog in console.
      // Copy/Paste from com.intellij.unscramble.UnscrambleAction#actionPerformed
      AnalyzeStacktraceUtil.addConsole(((Project) MapSequence.fromMap(_params).get("project")), null, "<Stacktrace>", message);
    } else if (((Throwable) MapSequence.fromMap(_params).get("exception")) != null) {
      StringWriter writer = new StringWriter();
      ((Throwable) MapSequence.fromMap(_params).get("exception")).printStackTrace(new PrintWriter(writer));
      // In case Throwable was provided just show it in console
      AnalyzeStacktraceUtil.addConsole(((Project) MapSequence.fromMap(_params).get("project")), null, "<Stacktrace>", writer.toString());
    } else {
      // Reuse platform action
      new AnalyzeStacktraceAction().actionPerformed(event);
    }
  }
  @NotNull
  public String getActionId() {
    StringBuilder res = new StringBuilder();
    res.append("Unscramble");
    return res.toString();
  }
}
