package jetbrains.mps.debugger.java.runtime.ui.actions;

/*Generated by MPS */

import jetbrains.mps.annotations.GeneratedClass;
import jetbrains.mps.plugins.actions.GeneratedActionGroup;
import java.util.Set;
import com.intellij.openapi.util.Pair;
import jetbrains.mps.workbench.ActionPlace;
import org.jetbrains.mps.util.Condition;
import jetbrains.mps.workbench.action.BaseAction;
import jetbrains.mps.internal.collections.runtime.SetSequence;
import java.util.HashSet;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.workbench.action.ApplicationPlugin;
import com.intellij.openapi.actionSystem.AnActionEvent;
import jetbrains.mps.debug.api.programState.IValue;
import jetbrains.mps.debugger.api.ui.tree.VariablesTree;
import jetbrains.mps.debugger.java.api.state.proxy.ValueWrapper;
import jetbrains.mps.debug.api.AbstractDebugSession;
import jetbrains.mps.debugger.api.ui.DebugActionsUtil;
import jetbrains.mps.debugger.java.api.state.JavaUiState;
import jetbrains.mps.debugger.java.api.state.proxy.ValueWrapperFactory;
import jetbrains.mps.debugger.java.runtime.state.customViewers.CustomViewersManagerImpl;
import org.jetbrains.annotations.Nullable;

@GeneratedClass(node = "r:a3396333-01e8-4d8f-ac7d-92203e663cdb(jetbrains.mps.debugger.java.runtime.ui.actions)/4225554808929802673", model = "r:a3396333-01e8-4d8f-ac7d-92203e663cdb(jetbrains.mps.debugger.java.runtime.ui.actions)")
public class ViewAs_Group_ActionGroup extends GeneratedActionGroup {
  public static final String ID = "jetbrains.mps.debugger.java.runtime.ui.actions.ViewAs_Group_ActionGroup";
  private final Set<Pair<ActionPlace, Condition<BaseAction>>> myPlaces = SetSequence.fromSet(new HashSet<Pair<ActionPlace, Condition<BaseAction>>>());

  public ViewAs_Group_ActionGroup(@NotNull ApplicationPlugin plugin) {
    super("View As", ID, plugin);
    setIsInternal(false);
    setPopup(true);
  }
  public void doUpdate(AnActionEvent event) {
    removeAll();
    final IValue value = VariablesTree.MPS_DEBUGGER_VALUE.getData(event.getDataContext());
    if (value == null || !(value instanceof ValueWrapper)) {
      event.getPresentation().setVisible(false);
      return;
    }

    AbstractDebugSession debugSession = DebugActionsUtil.getDebugSession(event);
    if (debugSession == null) {
      event.getPresentation().setVisible(false);
      return;
    }

    JavaUiState uiState = (JavaUiState) debugSession.getUiState();
    final Set<ValueWrapperFactory> factories = SetSequence.fromSet(new HashSet<ValueWrapperFactory>());
    uiState.invokeEvaluationSynchronously(() -> SetSequence.fromSet(factories).addSequence(SetSequence.fromSet(CustomViewersManagerImpl.getInstanceImpl().getValueWrapperFactories(((ValueWrapper) value).getValue()))));

    if (SetSequence.fromSet(factories).count() <= 1) {
      event.getPresentation().setVisible(false);
      return;
    }
    for (ValueWrapperFactory factory : SetSequence.fromSet(factories)) {
      ViewAs_Group_ActionGroup.this.addParameterizedAction(new ViewAs_Action(factory), factory);
    }
    for (Pair<ActionPlace, Condition<BaseAction>> p : this.myPlaces) {
      this.addPlace(p.first, p.second);
    }
  }
  public void addPlace(ActionPlace place, @Nullable Condition<BaseAction> cond) {
    SetSequence.fromSet(this.myPlaces).addElement(new Pair<ActionPlace, Condition<BaseAction>>(place, cond));
  }
  public boolean isStrict() {
    return false;
  }
}
