package com.redhat.ceylon.compiler.typechecker.util;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import com.redhat.ceylon.common.Backend;
import com.redhat.ceylon.compiler.typechecker.tree.Tree;
import com.redhat.ceylon.model.typechecker.model.Declaration;
import com.redhat.ceylon.model.typechecker.model.FunctionOrValue;
import com.redhat.ceylon.model.typechecker.model.ModelUtil;

/** Certain convenience methods related to native types.
 * 
 * @author Enrique Zamudio
 */
public class NativeUtil {

    public static List<Tree.Statement> mergeStatements(Tree.ClassBody body, Tree.Declaration header) {
        // And if the header exists we go through the declarations in
        // its body and add them to our list of statements as if they
        // were part of the native implementation when a) it has a
        // default implementation and b) we can't find a matching
        // declaration in our (original) list of statements
        List<Tree.Statement> hdrstmts;
        if (header instanceof Tree.ClassDefinition) {
            hdrstmts = ((Tree.ClassDefinition)header).getClassBody().getStatements();
        } else if (header instanceof Tree.ObjectDefinition) {
            hdrstmts = ((Tree.ObjectDefinition)header).getClassBody().getStatements();
        } else {
            hdrstmts = null;
        }
        List<Tree.Statement> stmts = body.getStatements();
        if (hdrstmts != null && !hdrstmts.isEmpty()) {
            Set<String> names = getDeclarationNames(stmts);
            LinkedList<Tree.Statement> newstmts = new LinkedList<Tree.Statement>(stmts);
            for (Tree.Statement stmt : hdrstmts) {
                if (stmt instanceof Tree.Declaration) {
                    Tree.Declaration decl = (Tree.Declaration)stmt;
                    if (isImplemented(decl)
                            && !names.contains(decl.getDeclarationModel().getName())) {
                        newstmts.addFirst(decl);
                    }
                }
            }
            stmts = newstmts;
        }
        return stmts;
    }

    public static Set<String> getDeclarationNames(java.util.List<Tree.Statement> stmts) {
        java.util.HashSet<String> names = new java.util.HashSet<String>();
        for (Tree.Statement stmt : stmts) {
            if (stmt instanceof Tree.Declaration) {
                Tree.Declaration decl = (Tree.Declaration)stmt;
                String declName = decl.getDeclarationModel().getName();
                names.add(declName);
            }
        }
        return names;
    }

    public static boolean isImplemented(Tree.Declaration decl) {
        return isImplemented(decl.getDeclarationModel());
    }
    
    public static boolean isImplemented(Declaration decl) {
        if (decl instanceof FunctionOrValue) {
            return ((FunctionOrValue)decl).isImplemented();
        } else {
            return false;
        }
    }

    public static boolean isNative(Tree.Declaration decl) {
        return isNative(decl.getDeclarationModel());
    }
    
    public static boolean isNative(Declaration decl) {
        return decl.isNative();
    }
    
    public static boolean isNativeHeader(Tree.Declaration decl) {
        return isNativeHeader(decl.getDeclarationModel());
    }
    
    public static boolean isNativeHeader(Declaration decl) {
        return decl.isNativeHeader();
    }
    
    public static String getNative(Tree.Declaration decl) {
        return getNative(decl.getDeclarationModel());
    }
    
    public static String getNative(Declaration decl) {
        return decl.getNativeBackend();
    }
    
    public static boolean isForBackend(Tree.Declaration decl) {
        return isForBackend(decl.getDeclarationModel());
    }
    
    public static boolean isForBackend(Declaration decl) {
        String backend = getNative(decl);
        return backend == null || backend.equals(Backend.Java.nativeAnnotation);
    }
    
    public static boolean isHeaderWithoutBackend(Tree.Declaration decl) {
        return isHeaderWithoutBackend(decl.getDeclarationModel());
    }
    
    public static boolean isHeaderWithoutBackend(Declaration decl) {
        return decl.isNativeHeader()
                && (ModelUtil.getNativeDeclaration(decl, Backend.Java) == null);
    }
    
}

@@ -3629,32 +3629,173 @@
 d();%0A    %7D%0A    %0A
+    /**%0A     * Checks that the declaration is marked %22native%22 and has a Ceylon implementation%0A     * meant for the specified backend%0A     */%0A
     public stati
@@ -3830,32 +3830,49 @@
 Declaration decl
+, Backend backend
 ) %7B%0A        retu
@@ -3905,32 +3905,41 @@
 clarationModel()
+, backend
 );%0A    %7D%0A    %0A  
@@ -3928,32 +3928,173 @@
 nd);%0A    %7D%0A    %0A
+    /**%0A     * Checks that the declaration is marked %22native%22 and has a Ceylon implementation%0A     * meant for the specified backend%0A     */%0A
     public stati
@@ -4124,32 +4124,49 @@
 Declaration decl
+, Backend backend
 ) %7B%0A        Stri
@@ -4169,22 +4169,17 @@
 String b
-ackend
+e
  = getNa
@@ -4206,22 +4206,17 @@
 return b
-ackend
+e
  == null
@@ -4224,22 +4224,17 @@
 %7C%7C b
-ackend
+e
 .equals(
 Back
@@ -4233,21 +4233,16 @@
 als(
-B
+b
 ackend.
-Java.
 nati
@@ -4329,32 +4329,49 @@
 Declaration decl
+, Backend backend
 ) %7B%0A        retu
@@ -4414,32 +4414,41 @@
 clarationModel()
+, backend
 );%0A    %7D%0A    %0A  
@@ -4502,32 +4502,49 @@
 Declaration decl
+, Backend backend
 ) %7B%0A        retu
@@ -4629,20 +4629,15 @@
 cl, 
-B
+b
 ackend
-.Java
 ) ==

make these methods multi-backend