package jetbrains.mps.ide.httpsupport.nodeaccess.plugin;

/*Generated by MPS */

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 org.jetbrains.mps.openapi.model.SModel;
import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations;
import jetbrains.mps.ide.actions.MPSCommonDataKeys;
import jetbrains.mps.smodel.tempmodel.TemporaryModels;
import jetbrains.mps.extapi.model.TransientSModel;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.mps.openapi.model.SNode;
import jetbrains.mps.project.MPSProject;
import org.jetbrains.mps.openapi.model.SNodeReference;
import jetbrains.mps.ide.datatransfer.CopyPasteUtil;
import com.intellij.openapi.util.registry.Registry;
import jetbrains.mps.project.Project;
import io.netty.handler.codec.http.QueryStringEncoder;
import jetbrains.mps.ide.httpsupport.manager.plugin.MPSInternalPortManager;
import org.jetbrains.mps.openapi.persistence.PersistenceFacade;

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

  public NodeReferenceURL_Action() {
    super("Copy Node Reference as URL", "", ICON);
    this.setIsAlwaysVisible(false);
    this.setActionAccess(ActionAccess.UNDO_PROJECT);
    updateInBackground(true);
  }
  @Override
  public boolean isDumbAware() {
    return true;
  }
  @Override
  public boolean isApplicable(AnActionEvent event, final Map<String, Object> _params) {
    SModel model = SNodeOperations.getModel(event.getData(MPSCommonDataKeys.NODE));
    return !(TemporaryModels.isTemporary(model)) && !(model instanceof TransientSModel);
  }
  @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;
    }
    {
      SNode node = event.getData(MPSCommonDataKeys.NODE);
      if (node == null) {
        return false;
      }
    }
    {
      MPSProject p = event.getData(MPSCommonDataKeys.MPS_PROJECT);
    }
    return true;
  }
  @Override
  public void doExecute(@NotNull final AnActionEvent event, final Map<String, Object> _params) {
    SNodeReference pointer = SNodeOperations.getPointer(event.getData(MPSCommonDataKeys.NODE));
    CopyPasteUtil.copyTextToClipboard((NodeReferenceURL_Action.this.specifyProjectInURL(event) ? buildRequest_njdhnr_a0a0b0h(pointer, event.getData(MPSCommonDataKeys.MPS_PROJECT)) : buildRequest_njdhnr_a0a0b0h_0(pointer)));
  }
  private boolean specifyProjectInURL(final AnActionEvent event) {
    return Registry.is("ide.httpsupport.specifyProjectInNodeURL");
  }
  private static String buildRequest_njdhnr_a0a0b0h(SNodeReference ref, Project project) {
    QueryStringEncoder encoder = new QueryStringEncoder("http://127.0.0.1:" + MPSInternalPortManager.getCurrentPort() + "/node");

    encoder.addParam("ref", PersistenceFacade.getInstance().asString(ref));
    encoder.addParam("project", project.getName());

    return encoder.toString();
  }
  private static String buildRequest_njdhnr_a0a0b0h_0(SNodeReference ref) {
    QueryStringEncoder encoder = new QueryStringEncoder("http://127.0.0.1:" + MPSInternalPortManager.getCurrentPort() + "/node");

    encoder.addParam("ref", PersistenceFacade.getInstance().asString(ref));

    return encoder.toString();
  }
}
