package com.bustiblelemons.adapters.viewpager;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.SparseArray;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by bhm on 18.07.14.
 */
public abstract class AbsFragmentPagerAdapter<T, F extends Fragment>
        extends FragmentStatePagerAdapter {

    private SparseArray<F> mFragments = new SparseArray<F>();
    private List<T>        mData      = new ArrayList<T>();

    public AbsFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    public AbsFragmentPagerAdapter(FragmentManager fm, List<T> data) {
        super(fm);
        this.mData = data;
    }

    public void setFragments(SparseArray<F> fragments) {
        this.mFragments = fragments;
        notifyDataSetChanged();
    }

    public boolean hasFragment(int atPos) {
        return mFragments != null ? mFragments.get(atPos) != null : false;
    }

    public void addData(T... data) {
        for (T item : data) {
            mData.add(item);
            int pos = mData.size();
            F f = newInstance(item);
            mFragments.put(pos, f);
        }
        notifyDataSetChanged();
    }

    public void addData(List<T> data) {
        for (int i = 0; i < data.size(); i++) {
            T item = data.get(i);
            F f = newInstance(item);
            mData.add(item);
            mFragments.put(i, f);
        }
        notifyDataSetChanged();
    }

    public boolean addData(T item) {
        boolean r = mData.add(item);
        int pos = mData.size();
        F f = newInstance(item);
        mFragments.put(pos, f);
        notifyDataSetChanged();
        return r;
    }

    public abstract F newInstance(T item);

    @Override
    public F getItem(int position) {
        if (mFragments.get(position) == null) {
            T item = mData.get(position);
            F f = newInstance(item);
            mFragments.put(position, f);
        }
        return mFragments.get(position);
    }

    public T getItemObject(int position) {
        return mData != null && mData.size() > 0 ? mData.get(position) : null;
    }


    public void removeAll() {
        mData.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return this.mData.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return String.valueOf(position);
    }
}

@@ -247,16 +247,46 @@
 ayList;%0A
+import java.util.Collections;%0A
 import j
@@ -1092,26 +1092,25 @@
     
-for (T item : data
+if (mData == null
 ) %7B%0A
@@ -1130,52 +1130,27 @@
 Data
-.add(item);%0A            int pos = mData.size
+ = new ArrayList%3CT%3E
 ();%0A
@@ -1161,82 +1161,50 @@
     
+%7D%0A
     
-F f = newInstance(item);%0A            mFragments.put(pos, f);%0A        %7D
+    Collections.addAll(mData, data);
 %0A   
@@ -1291,123 +1291,79 @@
     
-for (int i = 0; i %3C data.size(); i++) %7B%0A            T item = data.get(i);%0A            F f = newInstance(item
+if (mData == null) %7B%0A            mData = new ArrayList%3CT%3E(
 );%0A    
+    %7D%0A
     
@@ -1379,59 +1379,19 @@
 .add
-(item);%0A            mFragments.put(i, f);%0A        %7D
+All(data);%0A
 %0A   
@@ -1504,105 +1504,8 @@
 m);%0A
-        int pos = mData.size();%0A        F f = newInstance(item);%0A        mFragments.put(pos, f);%0A
     
@@ -2157,21 +2157,16 @@
  return 
-this.
 mData.si

fix to the data to fragment differences