package edu.clemson.resolve.jetbrains.completion;

import com.intellij.codeInsight.completion.BasicInsertHandler;
import com.intellij.codeInsight.completion.InsertHandler;
import com.intellij.codeInsight.completion.InsertionContext;
import com.intellij.codeInsight.completion.util.ParenthesesInsertHandler;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.openapi.editor.Editor;
import com.intellij.psi.PsiElement;
import edu.clemson.resolve.jetbrains.psi.*;

import java.util.List;

//TODO: Needs to handle math infix, outfix, and prefix symbol... prefix will depend on context as well.
public class MathSymbolInsertHandler extends BasicInsertHandler<LookupElement> {

    private final boolean mathWildcardQuery;

    MathSymbolInsertHandler(boolean mathWildcardQuery) {
        this.mathWildcardQuery = mathWildcardQuery;
    }

    @Override
    //TODO: I think its probably better we don't do a  (live-template) style completion for 'infix' definitions,
    //better to type the first argument go to the op, then type this, find the operator in the menu,
    //and insert it as usual.
    public void handleInsert(InsertionContext context, LookupElement item) {
        //this is 4 because that's the length of the current wildcard query keyword: 'this', we want to
        //erase that.. and it comes directly before the symbol (or name) we just inserted.
        if (mathWildcardQuery) {
            context.getDocument().deleteString(context.getStartOffset() - 4, context.getStartOffset());
        }

        Editor editor = context.getEditor();
        PsiElement element = item.getPsiElement();
        if (!(element instanceof ResMathDefnSig)) return;
        ResMathDefnSig signature = (ResMathDefnSig) element;
        int paramsCount = signature.getParameters().size();
        //we don't want empty parens for nullary function applications or infix (or outfix)
        //TODO: Actually, we could define some nice insertion handlers for outfix defns.

        InsertHandler<LookupElement> handler = new BasicInsertHandler<LookupElement>();
        if (signature instanceof ResMathPrefixDefnSig && paramsCount != 0) {
            handler = ParenthesesInsertHandler.WITH_PARAMETERS;
        }
        else if (signature instanceof ResMathOutfixDefnSig) {
            List<ResMathSymbolName> pieces = ((ResMathOutfixDefnSig) signature).getMathSymbolNameList();
            context.getDocument().insertString(context.getStartOffset(), "`");
            context.getDocument().insertString(context.getTailOffset(), pieces.get(1).getText() + "`");
        }
        handler.handleInsert(context, item);
    }
}

@@ -506,112 +506,8 @@
 t;%0A%0A
-//TODO: Needs to handle math infix, outfix, and prefix symbol... prefix will depend on context as well.%0A
 publ
@@ -764,252 +764,8 @@
 ide%0A
-    //TODO: I think its probably better we don't do a  (live-template) style completion for 'infix' definitions,%0A    //better to type the first argument go to the op, then type this, find the operator in the menu,%0A    //and insert it as usual.%0A
     
@@ -1458,189 +1458,8 @@
 e();
-%0A        //we don't want empty parens for nullary function applications or infix (or outfix)%0A        //TODO: Actually, we could define some nice insertion handlers for outfix defns.
 %0A%0A  
@@ -1858,24 +1858,107 @@
 NameList();%0A
+            if (pieces.size() == 2) %7B //if we actually have a left and right..%0A    
             
@@ -2020,24 +2020,28 @@
 et(), %22%60%22);%0A
+    
             
@@ -2128,24 +2128,38 @@
 t() + %22%60%22);%0A
+            %7D%0A
         %7D%0A  

remove some old comments