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

import static com.redhat.ceylon.eclipse.util.EditorUtil.getDocument;
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 org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
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 com.redhat.ceylon.compiler.typechecker.tree.Tree;
import com.redhat.ceylon.model.typechecker.model.Declaration;

class AssignToFieldProposal {

    static void addAssignToFieldProposal(IFile file, 
                Tree.Statement statement, 
                Tree.Declaration declaration, 
                Collection<ICompletionProposal> proposals) {
            if (declaration instanceof Tree.TypedDeclaration && 
                    statement instanceof Tree.Constructor) {
    //            Tree.TypedDeclaration td = 
    //                    (Tree.TypedDeclaration) declaration;
                Tree.Constructor constructor = 
                        (Tree.Constructor) statement;
                Declaration model = 
                        declaration.getDeclarationModel();
                String name = model.getName();
                if (constructor.getConstructor()
                        .getExtendedType()
                        .getDeclaration()
                        .getMember(name, null, false)
                            == null) {
                    TextFileChange change = 
                            new TextFileChange("Assign to Field", 
                                    file);
                    change.setEdit(new MultiTextEdit());
                    IDocument document = getDocument(change);
                    String indent = 
                            getDefaultLineDelimiter(document) +
                            getIndent(constructor, document);
                    String def;
                    try {
                        def = document.get(
                                declaration.getStartIndex(), 
                                declaration.getDistance());
                    }
                    catch (BadLocationException e) {
                        return;
                    }
                    //TODO: strip off the default argument!!!
                    def += ";" + indent;
                    int loc = statement.getStartIndex();
                    int offset = 
                            constructor.getBlock()
                                .getStartIndex() + 1;
                    String text = 
                            indent +
                            getDefaultIndent() +
                            "this." + name + 
                            " = " + name + ";";
                    change.addEdit(new InsertEdit(loc, def));
                    change.addEdit(new InsertEdit(offset, text));
                    String desc = 
                            "Assign parameter '" + name + 
                            "' to new field";
                    proposals.add(new CorrectionProposal(
                            desc, change, null));
                }
            }
        }

}

@@ -755,16 +755,78 @@
 e.Tree;%0A
+import com.redhat.ceylon.model.typechecker.model.Constructor;%0A
 import c
@@ -1246,117 +1246,8 @@
 ) %7B%0A
-    //            Tree.TypedDeclaration td = %0A    //                    (Tree.TypedDeclaration) declaration;%0A
     
@@ -1503,20 +1503,37 @@
         
-if (
+Constructor cmodel = 
 construc
@@ -1552,16 +1552,17 @@
 ructor()
+;
 %0A       
@@ -1566,32 +1566,140 @@
             
-        
+if (!model.getContainer().equals(cmodel)) %7B%0A                    return;%0A                %7D%0A                if (cmodel
 .getExtended
@@ -2301,89 +2301,454 @@
     
-String def;%0A                    try %7B%0A                        def = document.get(
+int start = declaration.getStartIndex();%0A                    int end;%0A                    Tree.SpecifierOrInitializerExpression sie;%0A                    if (declaration instanceof Tree.AttributeDeclaration) %7B%0A                        Tree.AttributeDeclaration ad = %0A                                (Tree.AttributeDeclaration) %0A                                    declaration;%0A                        sie = ad.getSpecifierOrInitializerExpression();
 %0A   
@@ -2755,32 +2755,35 @@
                 
+ %7D%0A
              dec
@@ -2783,95 +2783,701 @@
     
-declaration.getStartIndex(), %0A                                declaration.getDistance()
+       else if (declaration instanceof Tree.MethodDeclaration) %7B%0A                        Tree.MethodDeclaration ad = %0A                                (Tree.MethodDeclaration) %0A                                    declaration;%0A                        sie = ad.getSpecifierExpression();%0A                    %7D%0A                    else %7B%0A                        sie = null;%0A                    %7D%0A                    end = sie==null ? %0A                            declaration.getEndIndex() : %0A                            sie.getStartIndex();%0A                    String def;%0A                    try %7B%0A                        def = document.get(start, end-start)%0A                                .trim(
 );%0A 

handle default args for #1506