package moe.banana.jsonapi;

import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.Set;

public class JsonApiAdapterFactory implements JsonAdapter.Factory {

    JsonAdapter.Factory[] mFactoryArray = new JsonAdapter.Factory[] {
            new JsonAdapter.Factory() {
                @Override
                public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
                    if (type == Document.class) {
                        return new DocumentJsonAdapter(moshi);
                    }
                    if (type == Link.class) {
                        return new LinkJsonAdapter(moshi);
                    }
                    for (Annotation annotation : annotations) {
                        if (annotation.annotationType() == Implicit.class) {
                            if (type == Link.class) {
                                return new LinkJsonAdapter(moshi);
                            }
                        }
                        if (annotation.annotationType() == OneOrMany.class) {
                            if (type == Resource.class) {
                                return new OneOrManyJsonAdapter<>(Resource.class, new OneOrManyJsonAdapter.ContainerFactory<Resource>() {
                                    @Override
                                    public Resource createContainer() {
                                        return new Resources();
                                    }
                                }, moshi);
                            }
                            if (type == ResourceLinkage.class) {
                                return new OneOrManyJsonAdapter<>(ResourceLinkage.class, new OneOrManyJsonAdapter.ContainerFactory<ResourceLinkage>() {
                                    @Override
                                    public ResourceLinkage createContainer() {
                                        return new ResourceLinkages();
                                    }
                                }, moshi);
                            }
                        }
                    }
                    return null;
                }
            },
            AutoValue_Document_Links.typeAdapterFactory(),
            AutoValue_Error.typeAdapterFactory(),
            AutoValue_Error_Links.typeAdapterFactory(),
            AutoValue_Error_Source.typeAdapterFactory(),
            AutoValue_JsonApi.typeAdapterFactory(),
            AutoValue_Link.typeAdapterFactory(),
            AutoValue_Links.typeAdapterFactory(),
            AutoValue_Relationship.typeAdapterFactory(),
            AutoValue_Relationship_Links.typeAdapterFactory(),
            AutoValue_ResourceLinkage.typeAdapterFactory(),
    };

    @Override
    public JsonAdapter<?> create(Type type, Set<? extends Annotation> annotations, Moshi moshi) {
        for (JsonAdapter.Factory factory: mFactoryArray) {
            JsonAdapter<?> adapter = factory.create(type, annotations, moshi);
            if (adapter != null) {
                return adapter;
            }
        }
        return null;
    }

}

@@ -340,32 +340,316 @@
         
-new JsonAdapter.
+AutoValue_Document_Links.typeAdapterFactory(),%0A            AutoValue_Error.typeAdapterFactory(),%0A            AutoValue_Error_Links.typeAdapterFactory(),%0A            AutoValue_Error_Source.typeAdapterFactory(),%0A            AutoValue_JsonApi.typeAdapterFactory(),%0A            AutoValue_Link.typeAdapter
 Factory(
@@ -649,18 +649,17 @@
 actory()
- %7B
+,
 %0A       
@@ -655,32 +655,258 @@
 (),%0A            
+AutoValue_Links.typeAdapterFactory(),%0A            AutoValue_Relationship.typeAdapterFactory(),%0A            AutoValue_Relationship_Links.typeAdapterFactory(),%0A            AutoValue_ResourceLinkage.typeAdapterFactory(),%0A    %7D;%0A%0A
     @Override%0A  
@@ -899,36 +899,24 @@
   @Override%0A
-            
     public J
@@ -997,36 +997,24 @@
 hi moshi) %7B%0A
-            
         if (
@@ -1035,36 +1035,24 @@
 nt.class) %7B%0A
-            
             
@@ -1102,34 +1102,10 @@
     
-            %7D%0A            
+%7D%0A
     
@@ -1142,36 +1142,24 @@
             
- 
-           
 return new L
@@ -1193,34 +1193,10 @@
     
-            %7D%0A            
+%7D%0A
     
@@ -1251,36 +1251,24 @@
             
-   
-         
 if (annotati
@@ -1320,36 +1320,24 @@
             
-   
-         
 if (type == 
@@ -1346,36 +1346,24 @@
 nk.class) %7B%0A
-            
             
@@ -1413,37 +1413,39 @@
                 
-     
+%7D%0A     
        %7D%0A       
@@ -1441,40 +1441,8 @@
   %7D%0A
-                        %7D%0A      
     
@@ -1445,30 +1445,24 @@
             
-      
 if (annotati
@@ -1515,36 +1515,24 @@
             
-            
 if (type == 
@@ -1565,36 +1565,24 @@
             
-       
-     
 return new O
@@ -1695,36 +1695,24 @@
             
-            
 @Override%0A  
@@ -1729,36 +1729,24 @@
             
-        
-    
 public Resou
@@ -1793,36 +1793,24 @@
             
-            
 return new R
@@ -1849,34 +1849,10 @@
     
-          
-  %7D%0A            
+%7D%0A
     
@@ -1898,34 +1898,10 @@
     
-           
- 
 %7D%0A
-            
     
@@ -1945,36 +1945,24 @@
 ge.class) %7B%0A
-            
             
@@ -2117,42 +2117,18 @@
     
-            @Override%0A            
+@Override%0A
     
@@ -2214,36 +2214,24 @@
             
- 
-           
 return new R
@@ -2273,33 +2273,18 @@
         
-            %7D%0A   
+%7D%0A
         
@@ -2291,33 +2291,24 @@
             
-         
 %7D, moshi);%0A 
@@ -2326,808 +2326,33 @@
     
-            %7D%0A                        %7D%0A                    %7D%0A                    return null;%0A                %7D%0A            %7D,%0A            AutoValue_Document_Links.typeAdapterFactory(),%0A            AutoValue_Error.typeAdapterFactory(),%0A            AutoValue_Error_Links.typeAdapterFactory(),%0A            AutoValue_Error_Source.typeAdapterFactory(),%0A            AutoValue_JsonApi.typeAdapterFactory(),%0A            AutoValue_Link.typeAdapterFactory(),%0A            AutoValue_Links.typeAdapterFactory(),%0A            AutoValue_Relationship.typeAdapterFactory(),%0A            AutoValue_Relationship_Links.typeAdapterFactory(),%0A            AutoValue_ResourceLinkage.typeAdapterFactory(),%0A    %7D;%0A%0A    @Override%0A    public JsonAdapter%3C?%3E create(Type type, Set%3C? extends Annotation%3E annotations, Moshi moshi) %7B
+%7D%0A            %7D%0A        %7D
 %0A   

refactor JsonApiAdapterFactory