package com.leroymerlin.pandroid.ui;

import android.content.Context;
import android.support.v4.view.LayoutInflaterCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import com.leroymerlin.pandroid.log.LogcatLogger;

import java.util.List;

/**
 * Created by florian on 03/01/2018.
 */

public class PandroidViewFactory implements LayoutInflater.Factory2 {


    private static final String TAG = "PandroidLayoutInflater";

    private List<LayoutInflater.Factory2> factories;
    private AppCompatDelegate appDelegate;

    private PandroidViewFactory(AppCompatDelegate delegate, List<LayoutInflater.Factory2> factories) {
        this.factories = factories;
        this.appDelegate = delegate;
    }

    public static void installPandroidViewFactory(AppCompatActivity compatActivity) {
        if (compatActivity instanceof PandroidFactoryProvider) {
            List<LayoutInflater.Factory2> factories = ((PandroidFactoryProvider) compatActivity).getLayoutInflaterFactories();
            if (factories != null && !factories.isEmpty()) {
                LayoutInflater inflater = LayoutInflater.from(compatActivity);
                if (inflater.getFactory2() == null) {
                    PandroidViewFactory factory = new PandroidViewFactory(compatActivity.getDelegate(), factories);
                    LayoutInflaterCompat.setFactory2(inflater, factory);
                } else {
                    LogcatLogger.getInstance().w(TAG, "can't set layout inflater factory");
                }
            }
        } else {
            LogcatLogger.getInstance().w(TAG, "Your activity should implement PandroidFactoryProvider to install PandroidLayoutInflaterFactory");

        }
    }

    @Override
    public View onCreateView(View parent, String name, Context context, AttributeSet attrs) {
        for (LayoutInflater.Factory2 factory2 : factories) {
            View result = factory2.onCreateView(parent, name, context, attrs);
            if (result != null)
                return result;
        }
        return appDelegate.createView(parent, name, context, attrs);
    }

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        return onCreateView(null, name, context, attrs);
    }
}

@@ -362,16 +362,44 @@
 ogger;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -975,116 +975,345 @@
     
-if (compatActivity instanceof PandroidFactoryProvider) %7B%0A            List%3CLayoutInflater.Factory2%3E f
+List%3CLayoutInflater.Factory2%3E factories = new ArrayList%3C%3E();%0A        if (compatActivity instanceof PandroidFactoryProvider) %7B%0A            addProviderFactories((PandroidFactoryProvider) compatActivity, factories);%0A        %7D%0A        if (compatActivity.getApplication() instanceof PandroidFactoryProvider) %7B%0A            addProviderF
 actories
  = (
@@ -1308,19 +1308,16 @@
 actories
- = 
 ((Pandro
@@ -1353,36 +1353,36 @@
 vity
-)
 .get
-LayoutInflaterF
+Application(), f
 actories
 ();%0A
@@ -1377,17 +1377,16 @@
 actories
-(
 );%0A     
@@ -1392,37 +1392,22 @@
     
+%7D%0A
     
+    
 if (
-factories != null && 
 !fac
@@ -1430,36 +1430,32 @@
 ) %7B%0A            
-   
- 
 LayoutInflater i
@@ -1509,28 +1509,24 @@
             
-    
 if (inflater
@@ -1567,20 +1567,16 @@
         
- 
-   
 Pandroid
@@ -1671,36 +1671,32 @@
                 
-  
-  
 LayoutInflaterCo
@@ -1740,28 +1740,24 @@
             
-    
 %7D else %7B%0A   
@@ -1745,36 +1745,32 @@
        %7D else %7B%0A
-    
                 
@@ -1845,26 +1845,8 @@
 %22);%0A
-                %7D%0A
     
@@ -1933,16 +1933,31 @@
 ctivity 
+or application 
 should i
@@ -2033,16 +2033,354 @@
 tory%22);%0A
+        %7D%0A%0A    %7D%0A%0A    private static void addProviderFactories(PandroidFactoryProvider compatActivity, List%3CLayoutInflater.Factory2%3E factories) %7B%0A        List%3CLayoutInflater.Factory2%3E activityFactories = compatActivity.getLayoutInflaterFactories();%0A        if (activityFactories != null) %7B%0A            factories.addAll(activityFactories);
 %0A       

refactor (PandroidViewFactory) : check application as a provider