package com.redhat.ceylon.eclipse.code.correct;

import static com.redhat.ceylon.eclipse.util.Indents.getDefaultIndent;
import static com.redhat.ceylon.eclipse.util.Indents.getDefaultLineDelimiter;
import static com.redhat.ceylon.eclipse.util.Indents.getIndent;

import java.util.Collection;
import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.ltk.core.refactoring.TextFileChange;
import org.eclipse.text.edits.InsertEdit;
import org.eclipse.text.edits.MultiTextEdit;
import org.eclipse.text.edits.ReplaceEdit;

import com.redhat.ceylon.compiler.typechecker.tree.Tree;
import com.redhat.ceylon.compiler.typechecker.tree.Visitor;

public class ChangeToIfProposal {

    static void addChangeToIfProposal(Collection<ICompletionProposal> proposals,
            IDocument doc, IFile file, Tree.CompilationUnit rootNode,
            final Tree.Statement statement) {
        if (statement instanceof Tree.Assertion) {
            Tree.ConditionList conditionList = 
                    ((Tree.Assertion) statement).getConditionList();
            if (conditionList!=null) {
                class FindBodyVisitor extends Visitor {
                    Tree.Body result;
                    @Override
                    public void visit(Tree.Body that) {
                        if (that.getStatements().contains(statement)) {
                            result = that;
                        }
                        else {
                            super.visit(that);
                        }
                    }
                }
                FindBodyVisitor fbv = new FindBodyVisitor();
                fbv.visit(rootNode);
                List<Tree.Statement> statements = fbv.result.getStatements();
                Tree.Statement last = statements.get(statements.size()-1);
                boolean isLast = statement==last;
                TextFileChange change = 
                        new TextFileChange("Change Assert To If", file);
                change.setEdit(new MultiTextEdit());
                int begin = statement.getStartIndex();
                int end = conditionList.getStartIndex();
                change.addEdit(new ReplaceEdit(begin, end-begin, "if "));
                change.addEdit(new ReplaceEdit(statement.getStopIndex(), 1, 
                        isLast ? " {}" : " {"));
                for (int i=statements.indexOf(statement)+1; i<statements.size(); i++) {
                    change.addEdit(new InsertEdit(statements.get(i).getStartIndex(), 
                            getDefaultIndent()));
                }
                if (!isLast) {
                    change.addEdit(new InsertEdit(last.getStopIndex()+1, 
                            getDefaultLineDelimiter(doc) + getIndent(last, doc) + "}"));
                }
                proposals.add(new CorrectionProposal("Change 'assert' to 'if'", change, 
                        new Region(statement.getStopIndex()-2, 0)));
            }
        }
    }

}

@@ -2167,24 +2167,141 @@
 extEdit());%0A
+                String newline = getDefaultLineDelimiter(doc);%0A                String indent = getIndent(last, doc);%0A
             
@@ -2335,32 +2335,32 @@
 etStartIndex();%0A
-
                 
@@ -2596,24 +2596,103 @@
 %22 : %22 %7B%22));%0A
+                //TODO: this is wrong, need to look for lines, not statements!%0A
             
@@ -3058,67 +3058,249 @@
     
-getDefaultLineDelimiter(doc) + getIndent(last, doc)
+newline + indent + %22%7D%22));%0A                %7D%0A                String elseBlock = newline + indent +%0A                        %22else %7B%22 + newline + indent + getDefaultIndent() + %0A                        %22assert (false);%22 + newline + indent
  + %22%7D%22
-))
+ 
 ;%0A  
@@ -3305,33 +3305,97 @@
                 
-%7D
+change.addEdit(new InsertEdit(last.getStopIndex()+1, elseBlock));
 %0A               

improve assert->if quickfix