package jetbrains.mps.lang.structure.pluginSolution.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 jetbrains.mps.ide.actions.MPSCommonDataKeys;
import com.intellij.ide.actions.ContextHelpAction;
import jetbrains.mps.util.NameUtil;
import jetbrains.mps.project.MPSProject;
import org.jetbrains.mps.openapi.module.SModule;
import org.jetbrains.mps.openapi.model.SModel;
import org.jetbrains.mps.openapi.model.SNode;

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

  public ShowDefaultHelp_Action() {
    super("Show Default Help", "", ICON);
    this.setIsAlwaysVisible(false);
    this.setExecuteOutsideCommand(true);
    updateInBackground(true);
    this.addPlace(null);
  }
  @Override
  public boolean isDumbAware() {
    return true;
  }
  @Override
  public void doUpdate(@NotNull AnActionEvent event, final Map<String, Object> _params) {
    HelpHelper.HelpType defaultHelp = HelpHelper.getDefaultHelpFor(event.getData(MPSCommonDataKeys.CONTEXT_MODULE), event.getData(MPSCommonDataKeys.CONTEXT_MODEL), event.getData(MPSCommonDataKeys.NODE));
    if (defaultHelp == null) {
      ContextHelpAction contextHelpAction = new ContextHelpAction();
      contextHelpAction.update(event);
      if (event.getPresentation().isEnabled()) {
        return;
      }
      ShowDefaultHelp_Action.this.setEnabledState(event.getPresentation(), false);
      return;
    }
    ShowDefaultHelp_Action.this.setEnabledState(event.getPresentation(), true);
    event.getPresentation().setText("Show Help for " + NameUtil.capitalize(defaultHelp.getName()));
  }
  @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;
      }
    }
    {
      SModule p = event.getData(MPSCommonDataKeys.CONTEXT_MODULE);
    }
    {
      SModel p = event.getData(MPSCommonDataKeys.CONTEXT_MODEL);
    }
    {
      SNode node = event.getData(MPSCommonDataKeys.NODE);
    }
    return true;
  }
  @Override
  public void doExecute(@NotNull final AnActionEvent event, final Map<String, Object> _params) {
    event.getData(MPSCommonDataKeys.MPS_PROJECT).getModelAccess().runReadAction(() -> {
      if (HelpHelper.getDefaultHelpFor(event.getData(MPSCommonDataKeys.CONTEXT_MODULE), event.getData(MPSCommonDataKeys.CONTEXT_MODEL), event.getData(MPSCommonDataKeys.NODE)) == null) {
        ContextHelpAction contextHelpAction = new ContextHelpAction();
        contextHelpAction.update(event);
        if (event.getPresentation().isEnabled()) {
          contextHelpAction.actionPerformed(event);
        }
        return;
      }
      HelpHelper.showHelpFor(event.getData(MPSCommonDataKeys.CONTEXT_MODULE), event.getData(MPSCommonDataKeys.CONTEXT_MODEL), event.getData(MPSCommonDataKeys.NODE));
    });
  }
}
