package com.andresdominguez.ngsort;

import com.intellij.lang.javascript.psi.JSBlockStatement;
import com.intellij.lang.javascript.psi.JSFunctionExpression;
import com.intellij.lang.javascript.psi.JSParameterList;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.TextRange;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.util.PsiTreeUtil;

public class ToArrowFunctionAction extends AnAction {

  @Override
  public void actionPerformed(AnActionEvent e) {
    final Editor editor = e.getData(PlatformDataKeys.EDITOR);
    PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
    if (editor == null || psiFile == null) {
      return;
    }

    PsiElement element = psiFile.findElementAt(editor.getCaretModel().getOffset());
    final JSFunctionExpression functionExpr = PsiTreeUtil.getParentOfType(element, JSFunctionExpression.class);
    if (functionExpr == null) {
      return;
    }

    final JSParameterList paramList = PsiTreeUtil.findChildOfType(functionExpr, JSParameterList.class);
    if (paramList == null) {
      return;
    }

    final JSBlockStatement fnBlock = PsiTreeUtil.findChildOfType(functionExpr, JSBlockStatement.class);
    if (fnBlock == null) {
      return;
    }

    final TextRange fnBlockRange = fnBlock.getTextRange();
    final TextRange paramListRange = paramList.getTextRange();

    final Document document = editor.getDocument();
    CommandProcessor.getInstance().executeCommand(getEventProject(e), new Runnable() {
      @Override
      public void run() {
        // Add =>
        document.replaceString(fnBlockRange.getStartOffset(), fnBlockRange.getEndOffset(),
            String.format("=> %s", fnBlock.getText()));

        // Migrate function() to ()
        boolean shouldDropParens = paramList.getParameters().length == 1;
        if (shouldDropParens) {
          int startOffset = paramListRange.getStartOffset();
          int endOffset = paramListRange.getEndOffset();
          String replacement = paramList.getText().replace("(", "").replace(")", "");
          document.replaceString(startOffset, endOffset, replacement);
        }

        int startOffset = functionExpr.getTextRange().getStartOffset();
        int endOffset = paramListRange.getStartOffset();
        document.replaceString(startOffset, endOffset, "");
      }
    }, "ng sort", null);

  }
}

@@ -30,16 +30,66 @@
 gsort;%0A%0A
+import com.intellij.codeInsight.hint.HintManager;%0A
 import c
@@ -134,24 +134,24 @@
 kStatement;%0A
-
 import com.i
@@ -484,62 +484,8 @@
 ys;%0A
-import com.intellij.openapi.command.CommandProcessor;%0A
 impo
@@ -1246,32 +1246,110 @@
 Expr == null) %7B%0A
+      HintManager.getInstance().showErrorHint(editor, %22Can't find function%22);%0A
       return;%0A  
@@ -1478,32 +1478,122 @@
 List == null) %7B%0A
+      HintManager.getInstance().showErrorHint(editor, %22Can't find functions parameters%22);%0A
       return;%0A  
@@ -1720,32 +1720,116 @@
 lock == null) %7B%0A
+      HintManager.getInstance().showErrorHint(editor, %22Can't find function block%22);%0A
       return;%0A  
@@ -2023,39 +2023,18 @@
 mand
-Processor.getInstance().execute
+Runner.run
 Comm
@@ -2916,29 +2916,11 @@
    %7D
-, %22ng sort%22, null
 );%0A
-%0A
   %7D%0A

add error, use runCommand