package com.bluelinelabs.conductor.support;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import com.bluelinelabs.conductor.Controller;
import com.bluelinelabs.conductor.Router;
import com.bluelinelabs.conductor.RouterTransaction;

import java.util.List;

/**
 * An adapter for ViewPagers that uses Routers as pages
 */
public abstract class RouterPagerAdapter extends PagerAdapter {

    private static final String KEY_SAVED_PAGES = "RouterPagerAdapter.savedStates";
    private static final String KEY_SAVES_STATE = "RouterPagerAdapter.savesState";

    private final Controller host;
    private boolean savesState;
    private SparseArray<Bundle> savedPages = new SparseArray<>();

    /**
     * Creates a new RouterPagerAdapter using the passed host.
     */
    public RouterPagerAdapter(Controller host, boolean saveRouterState) {
        this.host = host;
        savesState = saveRouterState;
    }

    /**
     * Called when a router is instantiated. Here the router's root should be set if needed.
     *
     * @param router   The router used for the page
     * @param position The page position to be instantiated.
     */
    public abstract void configureRouter(Router router, int position);

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        final String name = makeRouterName(container.getId(), getItemId(position));

        Router router = host.getChildRouter(container, name);
        if (savesState && !router.hasRootController()) {
            Bundle routerSavedState = savedPages.get(position);

            if (routerSavedState != null) {
                router.restoreInstanceState(routerSavedState);
            }
        }

        router.rebindIfNeeded();
        configureRouter(router, position);
        return router;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        Router router = (Router)object;

        if (savesState) {
            Bundle savedState = new Bundle();
            router.saveInstanceState(savedState);
            savedPages.put(position, savedState);
        }

        host.removeChildRouter(router);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        Router router = (Router)object;
        final List<RouterTransaction> backstack = router.getBackstack();
        for (RouterTransaction transaction : backstack) {
            if (transaction.controller().getView() == view) {
                return true;
            }
        }
        return false;
    }

    @Override
    public Parcelable saveState() {
        Bundle bundle = new Bundle();
        bundle.putBoolean(KEY_SAVES_STATE, savesState);
        bundle.putSparseParcelableArray(KEY_SAVED_PAGES, savedPages);
        return bundle;
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
        Bundle bundle = (Bundle)state;
        if (state != null) {
            savesState = bundle.getBoolean(KEY_SAVES_STATE, false);
            savedPages = bundle.getSparseParcelableArray(KEY_SAVED_PAGES);
        }
    }

    public long getItemId(int position) {
        return position;
    }

    private static String makeRouterName(int viewId, long id) {
        return viewId + ":" + id;
    }

}
@@ -612,91 +612,8 @@
 es%22;
-%0A    private static final String KEY_SAVES_STATE = %22RouterPagerAdapter.savesState%22;
 %0A%0A  
@@ -649,40 +649,8 @@
 st;%0A
-    private boolean savesState;%0A
     
@@ -840,33 +840,8 @@
 host
-, boolean saveRouterState
 ) %7B%0A
@@ -870,46 +870,8 @@
 st;%0A
-        savesState = saveRouterState;%0A
     
@@ -1422,22 +1422,8 @@
 if (
-savesState && 
 !rou
@@ -1891,38 +1891,8 @@
 t;%0A%0A
-        if (savesState) %7B%0A    
     
@@ -1925,28 +1925,24 @@
 w Bundle();%0A
-    
         rout
@@ -1975,36 +1975,32 @@
 State);%0A        
-  
-  
 savedPages.put(p
@@ -2020,26 +2020,16 @@
 dState);
-%0A        %7D
 %0A%0A      
@@ -2554,64 +2554,8 @@
 ();%0A
-        bundle.putBoolean(KEY_SAVES_STATE, savesState);%0A
     
@@ -2805,76 +2805,8 @@
 ) %7B%0A
-            savesState = bundle.getBoolean(KEY_SAVES_STATE, false);%0A
     

Remove saveState option for RouterPagerAdapters, as users can configure the page before display anyway.