package com.carrotcreative.recyclercore.util;

import android.view.View;

import com.carrotcreative.recyclercore.adapter.RecyclerCoreController;
import com.carrotcreative.recyclercore.adapter.RecyclerCoreModel;
import com.carrotcreative.recyclercore.inject.InjectController;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

public class InstantiationUtil
{
    private InstantiationUtil() {}

    /**
     *
     * @param modelClass The model class for which we need to instantiate an object.
     * @return
     */
    public static RecyclerCoreModel instantiateModel(Class<?> modelClass)
    {
        String className = modelClass.getCanonicalName();
        Constructor<?>[] consArray = modelClass.getConstructors();
        Constructor noArgConstructor = null;
        for (Constructor constructor : consArray)
        {
            int argCount = constructor.getParameterTypes().length;
            if (argCount == 0)
            {
                noArgConstructor = constructor;
                break;
            }
        }

        RecyclerCoreModel modelInstance = null;
        if (noArgConstructor != null)
        {
            try
            {
                modelInstance = (RecyclerCoreModel) noArgConstructor.newInstance();
            }
            catch (InstantiationException e)
            {
                e.printStackTrace();
            }
            catch (IllegalAccessException e)
            {
                e.printStackTrace();
            }
            catch (InvocationTargetException e)
            {
                e.printStackTrace();
            }
        }

        if(modelInstance == null)
        {
            throw new IllegalArgumentException("class " + className + " does not have a public no-argument constructor!");
        }

        return modelInstance;
    }

    public static RecyclerCoreController instantiateController(Class<?> controllerClass, View itemView)
    {
        String className = controllerClass.getCanonicalName();
        Constructor<?>[] consArray = controllerClass.getConstructors();
        Constructor defaultConstructor = null;
        for (Constructor constructor : consArray)
        {
            int argCount = constructor.getParameterTypes().length;
            if (argCount == 1)
            {
                defaultConstructor = constructor;
                break;
            }
        }

        RecyclerCoreController controllerInstance = null;
        if (defaultConstructor != null)
        {
            try
            {

                controllerInstance = (RecyclerCoreController) defaultConstructor.newInstance(itemView);
            }
            catch (InstantiationException e)
            {
                e.printStackTrace();
            }
            catch (IllegalAccessException e)
            {
                e.printStackTrace();
            }
            catch (InvocationTargetException e)
            {
                e.printStackTrace();
            }
        }

        if(controllerInstance == null)
        {
            throw new IllegalArgumentException("class " + className + " does not have a valid public constructor! " +
                    "The constructor should take only View as an argument.");
        }

        return controllerInstance;
    }

    public static InjectController getInjectedController(RecyclerCoreModel model)
    {
        InjectController injectedController = model.getClass().getAnnotation(InjectController.class);
        if(injectedController == null)
        {
            throw new IllegalStateException(" class " + model.getClass().getCanonicalName() + " " +
                    " does not implement InjectController annotation");
        }

        return injectedController;
    }
}

@@ -433,1432 +433,8 @@
 %7B%7D%0A%0A
-    /**%0A     *%0A     * @param modelClass The model class for which we need to instantiate an object.%0A     * @return%0A     */%0A    public static RecyclerCoreModel instantiateModel(Class%3C?%3E modelClass)%0A    %7B%0A        String className = modelClass.getCanonicalName();%0A        Constructor%3C?%3E%5B%5D consArray = modelClass.getConstructors();%0A        Constructor noArgConstructor = null;%0A        for (Constructor constructor : consArray)%0A        %7B%0A            int argCount = constructor.getParameterTypes().length;%0A            if (argCount == 0)%0A            %7B%0A                noArgConstructor = constructor;%0A                break;%0A            %7D%0A        %7D%0A%0A        RecyclerCoreModel modelInstance = null;%0A        if (noArgConstructor != null)%0A        %7B%0A            try%0A            %7B%0A                modelInstance = (RecyclerCoreModel) noArgConstructor.newInstance();%0A            %7D%0A            catch (InstantiationException e)%0A            %7B%0A                e.printStackTrace();%0A            %7D%0A            catch (IllegalAccessException e)%0A            %7B%0A                e.printStackTrace();%0A            %7D%0A            catch (InvocationTargetException e)%0A            %7B%0A                e.printStackTrace();%0A            %7D%0A        %7D%0A%0A        if(modelInstance == null)%0A        %7B%0A            throw new IllegalArgumentException(%22class %22 + className + %22 does not have a public no-argument constructor!%22);%0A        %7D%0A%0A        return modelInstance;%0A    %7D%0A%0A
     

remove instantiation model function