package com.bunjlabs.fugaframework.dependency;

import com.bunjlabs.fugaframework.FugaApp;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class DependencyManager {

    private static final Logger log = LogManager.getLogger(DependencyManager.class);

    private final FugaApp app;
    private final Map<Class, Object> dependencies = new HashMap<>();

    public DependencyManager(FugaApp app) {
        this.app = app;
    }

    public void injectDependencies(Object injectable) throws InjectException {
        for (Field f : injectable.getClass().getDeclaredFields()) {
            if (f.isAnnotationPresent(Inject.class)) {
                try {
                    Class cls = f.getType();
                    Object injectOnject = null;
                    if (dependencies.containsKey(cls)) {
                        injectOnject = dependencies.get(cls);
                    } else {
                        for (Map.Entry<Class, Object> e : dependencies.entrySet()) {
                            if (e.getKey().isAssignableFrom(cls)) {
                                injectOnject = e.getValue();
                                break;
                            }
                        }
                    }
                    
                    if(injectOnject == null) {
                        throw new InjectException("No suitable dependency for " + cls.getName());
                    }

                    f.set(injectable, injectOnject);
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    throw new InjectException("Unable to inject field", e);
                }
            }
        }
    }

    public void addDependency(Object... objs) {
        for (Object obj : objs) {
            dependencies.put(obj.getClass(), obj);
        }
    }

    public void addDependency(Class... clss) {
        for (Class cls : clss) {
            try {
                dependencies.put(cls, cls.newInstance());
            } catch (InstantiationException | IllegalAccessException ex) {
                log.error("Unable to add dependency", ex);
            }
        }
    }
}

@@ -116,16 +116,68 @@
 .Field;%0A
+import java.lang.reflect.InvocationTargetException;%0A
 import j
@@ -912,13 +912,28 @@
 t = 
-null;
+getDependency(cls);%0A
 %0A   
@@ -957,100 +957,127 @@
 if (
-dependencies.containsKey(cls)) %7B%0A                        injectOnject = dependencies.get(cls
+injectOnject == null) %7B%0A                        throw new InjectException(%22No suitable dependency for %22 + cls.getName()
 );%0A 
@@ -1100,20 +1100,10 @@
    %7D
- else %7B%0A    
+%0A%0A
     
@@ -1123,200 +1123,196 @@
    f
-or (Map.Entry%3CClass, Object%3E e : dependencies.entrySet()) %7B%0A                            if (e.getKey().isAssignableFrom(cls)) %7B%0A                                injectOnject = e.getValue();%0A   
+.set(injectable, injectOnject);%0A                %7D catch (IllegalArgumentException %7C IllegalAccessException e) %7B%0A                    throw new InjectException(%22Unable to inject field%22, e);%0A
     
@@ -1315,33 +1315,34 @@
                 
- 
+%7D%0A
             brea
@@ -1341,27 +1341,18 @@
     
-break;%0A    
+%7D%0A
         
     
@@ -1351,238 +1351,334 @@
     
-    
+%7D%0A
     
+%7D%0A%0A
     
-    %7D%0A                        %7D%0A                    %7D%0A                    %0A                    if(injectOnject == null) %7B%0A                        throw new InjectException(%22No suitable dependency for %22 + cls.getNam
+public Object getDependency(Class cls) %7B%0A        if (dependencies.containsKey(cls)) %7B%0A            return dependencies.get(cls);%0A        %7D else %7B%0A            for (Map.Entry%3CClass, Object%3E e : dependencies.entrySet()) %7B%0A                if (e.getKey().isAssignableFrom(cls)) %7B%0A                    return e.getValu
 e()
-)
 ;%0A  
@@ -1679,32 +1679,52 @@
 ();%0A            
+    %7D%0A            %7D%0A
         %7D%0A%0A     
@@ -1718,33 +1718,38 @@
      %7D%0A%0A        
- 
+try %7B%0A
            f.set
@@ -1747,212 +1747,229 @@
     
-f.set(injectable, injectOnject);%0A                %7D catch (IllegalArgumentException %7C IllegalAccessException e) %7B%0A                    throw new InjectException(%22Unable to inject field%22, e);%0A       
+ return cls.getConstructor().newInstance();%0A        %7D catch (NoSuchMethodException %7C SecurityException %7C InstantiationException %7C IllegalAccessException %7C IllegalArgumentException %7C InvocationTargetException e) %7B%0A
         
  %7D%0A 
@@ -1956,34 +1956,34 @@
 on e) %7B%0A        
- 
 %7D
+%0A
 %0A            %7D%0A 
@@ -1971,39 +1971,36 @@
  %7D%0A%0A        
-    %7D%0A        %7D
+return null;
 %0A    %7D%0A%0A    

Add getDependency() method