// Path: autoadapter/src/main/java/com/benny/library/autoadapter/factory/Factory.java
// public interface Factory<T> {
//     T create();
// }
// 
// Path: autoadapter/src/main/java/com/benny/library/autoadapter/factory/MockViewHolerFactory.java
// public class MockViewHolerFactory<T> implements Factory<IViewHolder<T>> {
//     @Override
//     public IViewHolder<T> create() {
//         return new MockViewHolder<T>();
//     }
// }
// 
// Path: autoadapter/src/main/java/com/benny/library/autoadapter/utils/Func3.java
// public interface Func3<T1, T2, T3, R> {
//     R call(T1 t1, T2 t2, T3 t3);
// }
// 
// Path: autoadapter/src/main/java/com/benny/library/autoadapter/viewholder/MockViewHolder.java
// public class MockViewHolder<T> implements IViewHolder<T> {
//     @Override
//     public void bind(View view) {
//     }
// 
//     @Override
//     public void onDataChange(DataGetter<T> getter, int position) {
//     }
// }
// 
// Path: autoadapter/src/main/java/com/benny/library/autoadapter/viewholder/IViewHolder.java
// public interface IViewHolder<T> {
//     void bind(View view);
//     void onDataChange(DataGetter<T> getter, int position);
// }


import android.view.View;
import android.view.ViewGroup;
import com.benny.library.autoadapter.factory.Factory;
import com.benny.library.autoadapter.factory.MockViewHolerFactory;
import com.benny.library.autoadapter.utils.Func3;
import com.benny.library.autoadapter.viewholder.MockViewHolder;
import com.benny.library.autoadapter.viewholder.IViewHolder;
import java.util.ArrayList;
import java.util.List;
package com.benny.library.autoadapter.viewcreator;




/**
 * Created by benny on 2/15/16.
 */

public class ViewCreatorCollection<T> implements IViewCreator<T> {
    private int lastViewType = -1;
    private List<ViewTypeFilter<T> > viewTypeFilters = new ArrayList<ViewTypeFilter<T>>();

    protected ViewCreatorCollection(List<ViewTypeFilter<T> > viewTypeFilters) {
        this.viewTypeFilters = viewTypeFilters;
    }

    private ViewTypeFilter<T> filter(T data, int position, int itemCount) {
        for(ViewTypeFilter<T> viewTypeFilter : viewTypeFilters) {
            if(viewTypeFilter.canProcess(data, position, itemCount)) return viewTypeFilter;
        }
        throw new RuntimeException("can not process view type for: " + data.toString());
    }

    @Override
    public View view(ViewGroup container) {
        int index = (lastViewType != -1) ? lastViewType : viewTypeFilters.size() - 1;
        return viewTypeFilters.get(index).creator.view(container);
    }

    @Override
    public int viewTypeFor(T data, int position, int itemCount) {
        lastViewType = filter(data, position, itemCount).viewType;
        return lastViewType;
    }

    @Override
    public int viewTypeCount() {
        return viewTypeFilters.size();
    }


    public static class ViewTypeFilter<T> {
// Path: autoadapter/src/main/java/com/benny/library/autoadapter/factory/Factory.java
// public interface Factory<T> {
//     T create();
// }
// 
// Path: autoadapter/src/main/java/com/benny/library/autoadapter/factory/MockViewHolerFactory.java
// public class MockViewHolerFactory<T> implements Factory<IViewHolder<T>> {
//     @Override
//     public IViewHolder<T> create() {
//         return new MockViewHolder<T>();
//     }
// }
// 
// Path: autoadapter/src/main/java/com/benny/library/autoadapter/utils/Func3.java
// public interface Func3<T1, T2, T3, R> {
//     R call(T1 t1, T2 t2, T3 t3);
// }
// 
// Path: autoadapter/src/main/java/com/benny/library/autoadapter/viewholder/MockViewHolder.java
// public class MockViewHolder<T> implements IViewHolder<T> {
//     @Override
//     public void bind(View view) {
//     }
// 
//     @Override
//     public void onDataChange(DataGetter<T> getter, int position) {
//     }
// }
// 
// Path: autoadapter/src/main/java/com/benny/library/autoadapter/viewholder/IViewHolder.java
// public interface IViewHolder<T> {
//     void bind(View view);
//     void onDataChange(DataGetter<T> getter, int position);
// }


// Path: autoadapter/src/main/java/com/benny/library/autoadapter/viewcreator/ViewCreatorCollection.java
import android.view.View;
import android.view.ViewGroup;
import com.benny.library.autoadapter.factory.Factory;
import com.benny.library.autoadapter.factory.MockViewHolerFactory;
import com.benny.library.autoadapter.utils.Func3;
import com.benny.library.autoadapter.viewholder.MockViewHolder;
import com.benny.library.autoadapter.viewholder.IViewHolder;
import java.util.ArrayList;
import java.util.List;

package com.benny.library.autoadapter.viewcreator;




/**
 * Created by benny on 2/15/16.
 */

public class ViewCreatorCollection<T> implements IViewCreator<T> {
    private int lastViewType = -1;
    private List<ViewTypeFilter<T> > viewTypeFilters = new ArrayList<ViewTypeFilter<T>>();

    protected ViewCreatorCollection(List<ViewTypeFilter<T> > viewTypeFilters) {
        this.viewTypeFilters = viewTypeFilters;
    }

    private ViewTypeFilter<T> filter(T data, int position, int itemCount) {
        for(ViewTypeFilter<T> viewTypeFilter : viewTypeFilters) {
            if(viewTypeFilter.canProcess(data, position, itemCount)) return viewTypeFilter;
        }
        throw new RuntimeException("can not process view type for: " + data.toString());
    }

    @Override
    public View view(ViewGroup container) {
        int index = (lastViewType != -1) ? lastViewType : viewTypeFilters.size() - 1;
        return viewTypeFilters.get(index).creator.view(container);
    }

    @Override
    public int viewTypeFor(T data, int position, int itemCount) {
        lastViewType = filter(data, position, itemCount).viewType;
        return lastViewType;
    }

    @Override
    public int viewTypeCount() {
        return viewTypeFilters.size();
    }


    public static class ViewTypeFilter<T> {
        public Func3<T, Integer, Integer, Boolean> filter;