package jetbrains.mps.debugger.java.runtime.ui.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 org.jetbrains.annotations.NotNull;
import com.intellij.openapi.actionSystem.AnActionEvent;
import java.util.Map;
import jetbrains.mps.debug.api.programState.IValue;
import jetbrains.mps.debugger.api.ui.tree.VariablesTree;
import jetbrains.mps.debugger.java.api.state.proxy.JavaValue;
import jetbrains.mps.debugger.api.ui.DebugActionsUtil;
import jetbrains.mps.debug.api.AbstractUiState;
import jetbrains.mps.debugger.java.api.state.JavaUiState;
import com.sun.jdi.ThreadReference;
import jetbrains.mps.debugger.java.api.evaluation.EvaluationUtils;
import jetbrains.mps.ide.datatransfer.CopyPasteUtil;
import jetbrains.mps.debug.api.AbstractDebugSession;
import jetbrains.mps.debugger.java.api.state.proxy.JavaThread;

@GeneratedClass(node = "r:a3396333-01e8-4d8f-ac7d-92203e663cdb(jetbrains.mps.debugger.java.runtime.ui.actions)/8894158296385402494", model = "r:a3396333-01e8-4d8f-ac7d-92203e663cdb(jetbrains.mps.debugger.java.runtime.ui.actions)")
public class CopyValueAction_Action extends BaseAction {
  private static final Icon ICON = null;

  public CopyValueAction_Action() {
    super("Copy Value", "", ICON);
    this.setIsAlwaysVisible(false);
    this.setActionAccess(ActionAccess.UNDO_PROJECT);
    updateInBackground(true);
  }
  @Override
  public boolean isDumbAware() {
    return true;
  }
  @Override
  public void doUpdate(@NotNull AnActionEvent event, final Map<String, Object> _params) {
    IValue value = VariablesTree.MPS_DEBUGGER_VALUE.getData(event.getDataContext());
    event.getPresentation().setVisible(value != null && value instanceof JavaValue && DebugActionsUtil.getEvaluationProvider(event) != null);
  }
  @Override
  public void doExecute(@NotNull final AnActionEvent event, final Map<String, Object> _params) {
    final IValue value = VariablesTree.MPS_DEBUGGER_VALUE.getData(event.getDataContext());
    if (value == null || !(value instanceof JavaValue)) {
      return;
    }

    AbstractUiState uiState = check_d54g7t_a0d0a(DebugActionsUtil.getDebugSession(event));
    if (uiState == null || !(uiState instanceof JavaUiState)) {
      return;
    }
    JavaUiState javaUiState = (JavaUiState) uiState;

    final ThreadReference thread = check_d54g7t_a0h0a(javaUiState.getThread());
    if (thread == null) {
      return;
    }

    String result = javaUiState.invokeEvaluationSynchronously(() -> EvaluationUtils.getInstance().getStringPresentation(((JavaValue) value).getValue().getJDIValue(), thread));
    if ((result != null && result.length() > 0)) {
      CopyPasteUtil.copyTextToClipboard(result);
    }
  }
  private static AbstractUiState check_d54g7t_a0d0a(AbstractDebugSession checkedDotOperand) {
    if (null != checkedDotOperand) {
      return checkedDotOperand.getUiState();
    }
    return null;
  }
  private static ThreadReference check_d54g7t_a0h0a(JavaThread checkedDotOperand) {
    if (null != checkedDotOperand) {
      return checkedDotOperand.getThread();
    }
    return null;
  }
}
