package com.jetbrains.python.actions;

import com.intellij.codeInsight.CodeInsightUtilBase;
import com.intellij.codeInsight.template.TemplateBuilder;
import com.intellij.codeInsight.template.TemplateBuilderFactory;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil;
import com.jetbrains.python.PyBundle;
import com.jetbrains.python.psi.*;
import com.jetbrains.python.psi.impl.ParamHelper;
import com.jetbrains.python.psi.impl.PyFunctionBuilder;
import org.jetbrains.annotations.NotNull;

/**
 * User: catherine
 *
 * QuickFix to create function to unresolved unqualified reference
 */
public class UnresolvedRefCreateFunctionQuickFix implements LocalQuickFix {
  private PyCallExpression myElement;
  private PyReferenceExpression myReference;
  public UnresolvedRefCreateFunctionQuickFix(PyCallExpression element, PyReferenceExpression reference) {
    myElement = element;
    myReference = reference;
  }

  @NotNull
  public String getName() {
    return PyBundle.message("QFIX.unresolved.reference.create.function");
  }

  @NotNull
  public String getFamilyName() {
    return PyBundle.message("INSP.GROUP.python");
  }

  public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    if (!CodeInsightUtilBase.preparePsiElementForWrite(myElement)) return;

    PyFunctionBuilder functionBuilder = new PyFunctionBuilder(myReference.getText());

    for (PyExpression param : myElement.getArgumentList().getArguments()) {
      if (param instanceof PyKeywordArgument) {
        functionBuilder.parameter(((PyKeywordArgument)param).getKeyword());
      }
      else if (param instanceof PyReferenceExpression) {
        PyReferenceExpression refex = (PyReferenceExpression)param;
        functionBuilder.parameter(refex.getReferencedName());
      }
      else {
        functionBuilder.parameter("param");
      }
    }
    PyFunction function = functionBuilder.buildFunction(project, LanguageLevel.getDefault());
    PyFunction parentFunction = PsiTreeUtil.getTopmostParentOfType(myElement, PyFunction.class);
    if (parentFunction != null ) {
      PyClass parentClass = PsiTreeUtil.getTopmostParentOfType(parentFunction, PyClass.class);
      if (parentClass != null) {
        PsiElement parent = parentClass.getParent();
        function = (PyFunction)parent.addBefore(function, parentClass);
      } else {
        PsiElement parent = parentFunction.getParent();
        function = (PyFunction)parent.addBefore(function, parentFunction);
      }
    } else {
      PyStatement statement = PsiTreeUtil.getTopmostParentOfType(myElement,
                                                                 PyStatement.class);
      if (statement != null) {
        PsiElement parent = statement.getParent();
        if (parent != null)
          function = (PyFunction)parent.addBefore(function, statement);
      }
    }
    function = CodeInsightUtilBase.forcePsiPostprocessAndRestoreElement(function);
    final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(function);
    ParamHelper.walkDownParamArray(
      function.getParameterList().getParameters(),
      new ParamHelper.ParamVisitor() {
        public void visitNamedParameter(PyNamedParameter param, boolean first, boolean last) {
          builder.replaceElement(param, param.getName());
        }
      }
    );
    builder.replaceElement(function.getStatementList(), "pass");
    builder.run();
  }
}

@@ -913,16 +913,17 @@
 erence;%0A
+%0A
   public
@@ -1558,61 +1558,343 @@
     
-for (PyExpression param : myElement.getArgumentList()
+// if function is actually an argument of a call, don't use other arguments of the call to create parameter list of new function%0A    final PyArgumentList argumentList = myElement.getArgumentList();%0A    if (argumentList != null && !PsiTreeUtil.isAncestor(argumentList, myReference, false)) %7B%0A      for (PyExpression param : argumentList
 .get
@@ -1904,24 +1904,26 @@
 uments()) %7B%0A
+  
       if (pa
@@ -1958,32 +1958,34 @@
 ment) %7B%0A        
+ 
+ 
 functionBuilder.
@@ -2034,34 +2034,38 @@
 yword());%0A      
-%7D%0A
+  %7D%0A  
       else if (p
@@ -2113,16 +2113,18 @@
         
+ 
+ 
 PyRefere
@@ -2175,16 +2175,18 @@
 )param;%0A
+  
         
@@ -2245,24 +2245,26 @@
 ;%0A      
+  
 %7D%0A      
 else %7B%0A 
@@ -2255,23 +2255,27 @@
 %7D%0A      
+ 
+ 
 else %7B%0A
+  
         
@@ -2312,31 +2312,100 @@
 am%22);%0A      
-%7D%0A    %7D
+  %7D%0A      %7D%0A    %7D%0A    else %7B%0A      functionBuilder.parameter(%22args%22);%0A    %7D%0A
 %0A    PyFunct

create function quickfix doesn't take arguments from call expression if function being created is itself an argument