package com.expidev.gcmapp.support.v4.adapter;

import static com.expidev.gcmapp.model.measurement.MeasurementValue.ValueType;

import android.database.Cursor;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import com.expidev.gcmapp.db.Contract;
import com.expidev.gcmapp.model.Ministry;
import com.expidev.gcmapp.support.v4.fragment.measurement.MeasurementValueFragment;

import org.ccci.gto.android.common.util.CursorUtils;
import org.joda.time.YearMonth;

public class MeasurementPagerAdapter extends FragmentPagerAdapter {
    @ValueType
    private final int mType;
    @NonNull
    private final String mGuid;
    @NonNull
    private final String mMinistryId;
    @NonNull
    private final Ministry.Mcc mMcc;
    @NonNull
    private final YearMonth mPeriod;

    @Nullable
    private Cursor mCursor = null;

    public MeasurementPagerAdapter(@NonNull final FragmentManager fm, @ValueType final int type,
                                   @NonNull final String guid, @NonNull final String ministryId,
                                   @NonNull final Ministry.Mcc mcc, @NonNull final YearMonth period) {
        super(fm);
        mType = type;
        mGuid = guid;
        mMinistryId = ministryId;
        mMcc = mcc;
        mPeriod = period;
    }

    public void swapCursor(@Nullable final Cursor c) {
        mCursor = c;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mCursor != null ? mCursor.getCount() : 0;
    }

    @Override
    public Fragment getItem(final int position) {
        assert mCursor != null : "This should never be called unless we have items, which means we have a Cursor";

        mCursor.moveToPosition(position);

        return MeasurementValueFragment.newInstance(mType, mGuid, mMinistryId, mMcc, CursorUtils.getNonNullString(
                mCursor, Contract.MeasurementValue.COLUMN_PERM_LINK, ""), mPeriod);
    }

    @Override
    public long getItemId(final int position) {
        assert mCursor != null : "This should never be called unless we have items, which means we have a Cursor";

        mCursor.moveToPosition(position);
        return CursorUtils.getLong(mCursor, Contract.MeasurementValue.COLUMN_ROWID, super.getItemId(position));
    }
}

@@ -374,24 +374,55 @@
 agerAdapter;
+%0Aimport android.view.ViewGroup;
 %0A%0Aimport com
@@ -1682,32 +1682,457 @@
 t() : 0;%0A    %7D%0A%0A
+    @Override%0A    public Object instantiateItem(@NonNull final ViewGroup container, final int position) %7B%0A        final Object item = super.instantiateItem(container, position);%0A        // XXX: setUserVisibleHint(true) to work around https://code.google.com/p/android/issues/detail?id=69586%0A        if (item instanceof Fragment) %7B%0A            ((Fragment) item).setUserVisibleHint(true);%0A        %7D%0A        return item;%0A    %7D%0A%0A
     @Override%0A  

work around loaders not starting until the fragment is visible